Class: Honeybadger::Agent::MeteredQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/honeybadger/agent/metered_queue.rb

Overview

Internal: A thread-safe first-in-first-out queue. Values are pushed onto the queue and released at a defined interval.

Instance Method Summary collapse

Constructor Details

#initialize(interval = 1, max = 1000, now = now()) ⇒ MeteredQueue

Returns a new instance of MeteredQueue.



6
7
8
9
10
11
12
13
# File 'lib/honeybadger/agent/metered_queue.rb', line 6

def initialize(interval = 1, max = 1000, now = now())
  @interval = interval
  @max = max
  @values = Array.new
  @throttles = Array.new
  @future = calculate_future(now, interval)
  @mutex = Mutex.new
end

Instance Method Details

#popObject



25
26
27
28
29
30
31
32
# File 'lib/honeybadger/agent/metered_queue.rb', line 25

def pop
  if now.to_i >= future
    mutex.synchronize do
      @future = calculate_future
      values.shift
    end
  end
end

#pop!Object



34
35
36
# File 'lib/honeybadger/agent/metered_queue.rb', line 34

def pop!
  mutex.synchronize { values.shift }
end

#push(value) ⇒ Object



19
20
21
22
23
# File 'lib/honeybadger/agent/metered_queue.rb', line 19

def push(value)
  unless values.size == max
    mutex.synchronize { values.push(value) }
  end
end

#sizeObject



15
16
17
# File 'lib/honeybadger/agent/metered_queue.rb', line 15

def size
  mutex.synchronize { values.size }
end

#throttle(throttle) ⇒ Object

Applies a new throttle to this queue and adjusts the future.

Returns nothing



41
42
43
44
45
46
47
# File 'lib/honeybadger/agent/metered_queue.rb', line 41

def throttle(throttle)
  mutex.synchronize do
    old_interval = throttled_interval
    throttles << throttle
    @future += (throttled_interval - old_interval)
  end
end

#unthrottleObject

Removes the last throttle from this queue and adjusts the future.

Returns Float throttle



52
53
54
55
56
57
58
59
60
61
# File 'lib/honeybadger/agent/metered_queue.rb', line 52

def unthrottle
  mutex.synchronize do
    old_interval = throttled_interval
    throttles.pop.tap do |throttle|
      if throttle
        @future -= (old_interval - throttled_interval)
      end
    end
  end
end