class Praroter::FillyBucket::Bucket

Attributes

capacity[R]
fill_rate[R]
key[R]

Public Class Methods

new(key, fill_rate, capacity, creator) click to toggle source
# File lib/praroter/filly_bucket/bucket.rb, line 7
def initialize(key, fill_rate, capacity, creator)
  @key = key
  @fill_rate = fill_rate
  @capacity = capacity
  @creator = creator

  raise ArgumentError, "key must be a string" if @key.class != String
  raise ArgumentError, "fill_rate must be an integer" if @fill_rate.class != Integer
  raise ArgumentError, "capacity must be an integer" if @capacity.class != Integer
end

Public Instance Methods

drain(amount) click to toggle source
# File lib/praroter/filly_bucket/bucket.rb, line 39
def drain(amount)
  raise ArgumentError, "drain amount must be an integer" if amount.class != Integer
  raise ArgumentError, "drain amount must be a positive number" if amount < 0

  @creator.run_lua_bucket_script(self, amount)
end
drain_block() { || ... } click to toggle source
# File lib/praroter/filly_bucket/bucket.rb, line 46
def drain_block
  work_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  yield
  work_end = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  drain(((work_end - work_start) * 1000).to_i)
end
empty?() click to toggle source
# File lib/praroter/filly_bucket/bucket.rb, line 22
def empty?
  state.empty?
end
full?() click to toggle source
# File lib/praroter/filly_bucket/bucket.rb, line 26
def full?
  state.full?
end
last_updated_key() click to toggle source
# File lib/praroter/filly_bucket/bucket.rb, line 57
def last_updated_key
  "filly_bucket.#{key}.last_updated"
end
level_key() click to toggle source
# File lib/praroter/filly_bucket/bucket.rb, line 53
def level_key
  "filly_bucket.#{key}.bucket_level"
end
state() click to toggle source
# File lib/praroter/filly_bucket/bucket.rb, line 18
def state
  @creator.run_lua_bucket_script(self, 0)
end
throttle!() click to toggle source
# File lib/praroter/filly_bucket/bucket.rb, line 30
def throttle!
  bucket_state = state
  if bucket_state.empty?
    remaining_block_time = ((bucket_state.capacity - bucket_state.level).abs / bucket_state.fill_rate) + 3
    raise Praroter::Throttled.new(bucket_state, remaining_block_time)
  end
  bucket_state
end