lib/traffic_jam/simple_limit.rb
# Increment the amount used by the given number. Does not perform increment
# if the operation would exceed the limit. Returns whether the operation was
# successful.
#
# @param amount [Integer] amount to increment by
# @param time [Time] time is ignored
# @return [Boolean] true if increment succeded and false if incrementing
#   would exceed the limit
Increment the amount used by the given number. Does not perform increment
if the operation would exceed the limit. Returns whether the operation was
successful.
Increment the amount used by the given number . Does not perform increment if the operation would exceed the limit . Returns whether the operation was successful .
def increment(amount = 1, time: Time.now)
      return true if amount == 0
      return false if max == 0
      raise ArgumentError.new("Amount must be positive") if amount < 0
      if amount != amount.to_i
        raise ArgumentError.new("Amount must be an integer")
      end
      return false if amount > max
      incrby = (period * 1000 * amount / max).to_i
      argv = [incrby, period * 1000]
      result =
        begin
          redis.evalsha(
            Scripts::INCREMENT_SIMPLE_HASH, keys: [key], argv: argv)
        rescue Redis::CommandError
          redis.eval(Scripts::INCREMENT_SIMPLE, keys: [key], argv: argv)
        end
      case result
      when 0
        return true
      when -1
        raise Errors::InvalidKeyError, "Redis key #{key} has no expire time set"
      when -2
        return false
      else
        raise Errors::UnknownReturnValue,
              "Received unexpected return value #{result} from " \
              "increment_simple eval"
      end
    end
def increment ( amount = 1 , time : Time . now ) return true if amount == 0 return false if max == 0 raise ArgumentError . new ( "Amount must be positive" ) if amount < 0 if amount != amount . to_i raise ArgumentError . new ( "Amount must be an integer" ) end return false if amount > max incrby = ( period * 1000 * amount / max ) . to_i argv = [ incrby , period * 1000 ] result = begin redis . evalsha ( Scripts :: INCREMENT_SIMPLE_HASH , keys : [ key ] , argv : argv ) rescue Redis :: CommandError redis . eval ( Scripts :: INCREMENT_SIMPLE , keys : [ key ] , argv : argv ) end case result when 0 return true when - 1 raise Errors :: InvalidKeyError , "Redis key #{key} has no expire time set" when - 2 return false else raise Errors :: UnknownReturnValue , "Received unexpected return value #{result} from " "increment_simple eval" end end
Increment the amount used by the given number.
Increment the amount used by the given number .
true if increment succeded and false if incrementing would exceed the limit
true if increment succeded and false if incrementing would exceed the limit
Boolean
amount
amount to increment by
amount to increment by
time
time is ignored
time is ignored
now
<not_specific>