lib/traffic_jam/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. Time of increment can be specified optionally with a keyword
# argument, which is useful for rolling back with a decrement.
#
# @param amount [Integer] amount to increment by
# @param time [Time] time when increment occurs
# @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. Time of increment can be specified optionally with a keyword
argument, which is useful for rolling back with a decrement.
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 . Time of increment can be specified optionally with a keyword argument which is useful for rolling back with a decrement .
def increment(amount = 1, time: Time.now)
      return amount <= 0 if max.zero?
      if amount != amount.to_i
        raise ArgumentError.new("Amount must be an integer")
      end
      timestamp = (time.to_f * 1000).to_i
      argv = [timestamp, amount.to_i, max, period * 1000]
      result =
        begin
          redis.evalsha(
            Scripts::INCREMENT_SCRIPT_HASH, keys: [key], argv: argv)
        rescue Redis::CommandError
          redis.eval(Scripts::INCREMENT_SCRIPT, keys: [key], argv: argv)
        end
      !!result
    end
def increment ( amount = 1 , time : Time . now ) return amount <= 0 if max . zero? if amount != amount . to_i raise ArgumentError . new ( "Amount must be an integer" ) end timestamp = ( time . to_f * 1000 ) . to_i argv = [ timestamp , amount . to_i , max , period * 1000 ] result = begin redis . evalsha ( Scripts :: INCREMENT_SCRIPT_HASH , keys : [ key ] , argv : argv ) rescue Redis :: CommandError redis . eval ( Scripts :: INCREMENT_SCRIPT , keys : [ key ] , argv : argv ) end ! ! result 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 when increment occurs
time when increment occurs
now
<not_specific>