Class: ReeLogger::RateLimiter

Inherits:
Object
  • Object
show all
Defined in:
lib/ree_lib/packages/ree_logger/package/ree_logger/rate_limiter.rb

Instance Method Summary collapse

Constructor Details

#initialize(interval, max_rate) ⇒ RateLimiter

Returns a new instance of RateLimiter.



3
4
5
6
7
# File 'lib/ree_lib/packages/ree_logger/package/ree_logger/rate_limiter.rb', line 3

def initialize(interval, max_rate)
  @max_rate = max_rate
  @interval = interval
  @appends = []
end

Instance Method Details

#call(&block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ree_lib/packages/ree_logger/package/ree_logger/rate_limiter.rb', line 9

def call(&block)
  tick = Time.now.to_i
  @appends.push(tick)
  min = tick - @interval

  loop do
    if @appends.first < min
      @appends.shift
    else
      break
    end
  end

  if @appends.size < @max_rate
    block.call
  end
end