Class: Limiter::RateQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/limiter/rate_queue.rb

Constant Summary collapse

EPOCH =
0.0

Instance Method Summary collapse

Constructor Details

#initialize(size, interval: 60, balanced: false, &blk) ⇒ RateQueue

Returns a new instance of RateQueue.



7
8
9
10
11
12
13
14
15
# File 'lib/limiter/rate_queue.rb', line 7

def initialize(size, interval: 60, balanced: false, &blk)
  @size = size
  @interval = interval

  @ring = balanced ? balanced_ring : unbalanced_ring
  @head = 0
  @mutex = Mutex.new
  @blk = blk
end

Instance Method Details

#shiftObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/limiter/rate_queue.rb', line 17

def shift
  time = nil

  @mutex.synchronize do
    time = @ring[@head]

    sleep_until(time + @interval)

    @ring[@head] = clock.time
    @head = (@head + 1) % @size
  end

  time
end