Class: Chronos::Internal::MemoryBacklog
- Inherits:
-
Object
- Object
- Chronos::Internal::MemoryBacklog
- Defined in:
- lib/chronos/internal/memory_backlog.rb
Overview
Fixed-capacity retry storage for sanitized serialized events.
Instance Attribute Summary collapse
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(capacity) ⇒ MemoryBacklog
constructor
A new instance of MemoryBacklog.
- #push(event) ⇒ Object
- #shift ⇒ Object
- #size ⇒ Object
- #stats ⇒ Object
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
#capacity ⇒ Object (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
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 |
#shift ⇒ Object
46 47 48 |
# File 'lib/chronos/internal/memory_backlog.rb', line 46 def shift @mutex.synchronize { @items.shift } end |
#size ⇒ Object
50 51 52 |
# File 'lib/chronos/internal/memory_backlog.rb', line 50 def size @mutex.synchronize { @items.size } end |
#stats ⇒ Object
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 |