Class: Legion::Gaia::NotificationGate::DelayQueue
- Inherits:
-
Object
- Object
- Legion::Gaia::NotificationGate::DelayQueue
- Defined in:
- lib/legion/gaia/notification_gate/delay_queue.rb
Instance Attribute Summary collapse
-
#max_delay ⇒ Object
readonly
Returns the value of attribute max_delay.
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
- #clear ⇒ Object
- #drain_expired ⇒ Object
- #enqueue(frame) ⇒ Object
- #flush ⇒ Object
-
#initialize(max_size: 100, max_delay: 14_400) ⇒ DelayQueue
constructor
A new instance of DelayQueue.
- #pending ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(max_size: 100, max_delay: 14_400) ⇒ DelayQueue
Returns a new instance of DelayQueue.
9 10 11 12 13 14 |
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 9 def initialize(max_size: 100, max_delay: 14_400) @max_size = max_size @max_delay = max_delay @entries = [] @mutex = Mutex.new end |
Instance Attribute Details
#max_delay ⇒ Object (readonly)
Returns the value of attribute max_delay.
7 8 9 |
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 7 def max_delay @max_delay end |
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
7 8 9 |
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 7 def max_size @max_size end |
Instance Method Details
#clear ⇒ Object
49 50 51 |
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 49 def clear @mutex.synchronize { @entries.clear } end |
#drain_expired ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 33 def drain_expired @mutex.synchronize do cutoff = Time.now.utc - @max_delay expired, @entries = @entries.partition { |e| e[:queued_at] < cutoff } expired end end |
#enqueue(frame) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 16 def enqueue(frame) @mutex.synchronize do evicted = nil evicted = @entries.shift if @entries.size >= @max_size @entries << { frame: frame, queued_at: Time.now.utc, retry_count: 0 } evicted end end |
#flush ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 41 def flush @mutex.synchronize do all = @entries.dup @entries.clear all end end |
#pending ⇒ Object
29 30 31 |
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 29 def pending @mutex.synchronize { @entries.dup } end |
#size ⇒ Object
25 26 27 |
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 25 def size @mutex.synchronize { @entries.size } end |