class Berater::StaticLimiter

Constants

LUA_SCRIPT

Public Instance Methods

to_s() click to toggle source
# File lib/berater/static_limiter.rb, line 44
def to_s
  "#<#{self.class}(#{key}: #{capacity})>"
end

Protected Instance Methods

acquire_lock(capacity, cost) click to toggle source
# File lib/berater/static_limiter.rb, line 21
          def acquire_lock(capacity, cost)
  if cost == 0
    # utilization check
    count = redis.get(cache_key) || "0"
    allowed = true
  else
    count, allowed = LUA_SCRIPT.eval(
      redis, [ cache_key ], [ capacity, cost ],
    )
  end

  # Redis returns Floats as strings to maintain precision
  count = count.include?('.') ? count.to_f : count.to_i

  raise Overloaded unless allowed

  release_fn = if cost > 0
    proc { redis.incrbyfloat(cache_key, -cost) }
  end

  Lock.new(capacity, count, release_fn)
end