Class: Legion::Gaia::NotificationGate::DelayQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/gaia/notification_gate/delay_queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_delayObject (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_sizeObject (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

#clearObject



49
50
51
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 49

def clear
  @mutex.synchronize { @entries.clear }
end

#drain_expiredObject



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

#flushObject



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

#pendingObject



29
30
31
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 29

def pending
  @mutex.synchronize { @entries.dup }
end

#sizeObject



25
26
27
# File 'lib/legion/gaia/notification_gate/delay_queue.rb', line 25

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