class Jaeger::RateLimiter

RateLimiter is based on leaky bucket algorithm, formulated in terms of a credits balance that is replenished every time check_credit() method is called (tick) by the amount proportional to the time elapsed since the last tick, up to the max_balance. A call to check_credit() takes a cost of an item we want to pay with the balance. If the balance exceeds the cost of the item, the item is “purchased” and the balance reduced, indicated by returned value of true. Otherwise the balance is unchanged and return false.

This can be used to limit a rate of messages emitted by a service by instantiating the Rate Limiter with the max number of messages a service is allowed to emit per second, and calling check_credit(1.0) for each message to determine if the message is within the rate limit.

It can also be used to limit the rate of traffic in bytes, by setting credits_per_second to desired throughput as bytes/second, and calling check_credit() with the actual message size.

Public Class Methods

new(credits_per_second:, max_balance:) click to toggle source
# File lib/jaeger/rate_limiter.rb, line 22
def initialize(credits_per_second:, max_balance:)
  @credits_per_second = credits_per_second
  @max_balance = max_balance
  @balance = max_balance
  @last_tick = Time.now
end

Public Instance Methods

check_credit(item_cost) click to toggle source
# File lib/jaeger/rate_limiter.rb, line 29
def check_credit(item_cost)
  update_balance

  return false if @balance < item_cost

  @balance -= item_cost
  true
end
update(credits_per_second:, max_balance:) click to toggle source
# File lib/jaeger/rate_limiter.rb, line 38
def update(credits_per_second:, max_balance:)
  update_balance

  @credits_per_second = credits_per_second

  # The new balance should be proportional to the old balance
  @balance = max_balance * @balance / @max_balance
  @max_balance = max_balance
end

Private Instance Methods

update_balance() click to toggle source
# File lib/jaeger/rate_limiter.rb, line 50
def update_balance
  current_time = Time.now
  elapsed_time = current_time - @last_tick
  @last_tick = current_time

  @balance += elapsed_time * @credits_per_second
  return if @balance <= @max_balance

  @balance = @max_balance
end