Class: Chronos::Internal::MemoryBacklog

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/internal/memory_backlog.rb

Overview

Fixed-capacity retry storage for sanitized serialized events.

Examples:

backlog.push(serialized_event)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ MemoryBacklog

Returns a new instance of MemoryBacklog.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/chronos/internal/memory_backlog.rb', line 18

def initialize(capacity)
  unless capacity.is_a?(Integer) && capacity >= 0
    raise ArgumentError, "capacity must be a non-negative integer"
  end

  @capacity = capacity
  @items = []
  @accepted = 0
  @dropped = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#capacityObject (readonly)

Returns the value of attribute capacity.



16
17
18
# File 'lib/chronos/internal/memory_backlog.rb', line 16

def capacity
  @capacity
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/chronos/internal/memory_backlog.rb', line 54

def empty?
  size.zero?
end

#push(event) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chronos/internal/memory_backlog.rb', line 30

def push(event)
  unless event.is_a?(Core::SerializedEvent)
    raise ArgumentError, "backlog accepts only sanitized serialized events"
  end

  @mutex.synchronize do
    if @items.length >= capacity
      @dropped += 1
      return false
    end
    @items << event
    @accepted += 1
    true
  end
end

#shiftObject



46
47
48
# File 'lib/chronos/internal/memory_backlog.rb', line 46

def shift
  @mutex.synchronize { @items.shift }
end

#sizeObject



50
51
52
# File 'lib/chronos/internal/memory_backlog.rb', line 50

def size
  @mutex.synchronize { @items.size }
end

#statsObject



58
59
60
61
62
# File 'lib/chronos/internal/memory_backlog.rb', line 58

def stats
  @mutex.synchronize do
    {:size => @items.size, :capacity => capacity, :accepted => @accepted, :dropped => @dropped}
  end
end