Class: RedisThrottle::RateLimit

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_throttle/rate_limit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, limit:, period:) ⇒ RateLimit

Returns a new instance of RateLimit.

Parameters:

  • bucket (#to_s)

    Throttling group name

  • limit (#to_i)

    Max allowed units per ‘period`

  • period (#to_i)

    Period in seconds



20
21
22
23
24
# File 'lib/redis_throttle/rate_limit.rb', line 20

def initialize(bucket, limit:, period:)
  @bucket = -bucket.to_s
  @limit  = limit.to_i
  @period = period.to_i
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



7
8
9
# File 'lib/redis_throttle/rate_limit.rb', line 7

def bucket
  @bucket
end

#limitObject (readonly)

Returns the value of attribute limit.



11
12
13
# File 'lib/redis_throttle/rate_limit.rb', line 11

def limit
  @limit
end

#periodObject (readonly)

Returns the value of attribute period.



15
16
17
# File 'lib/redis_throttle/rate_limit.rb', line 15

def period
  @period
end

Instance Method Details

#<=>(other) ⇒ -1, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compare ‘self` with `other` strategy:

Returns:

  • (-1, 0, 1, nil)


52
53
54
# File 'lib/redis_throttle/rate_limit.rb', line 52

def <=>(other)
  complexity <=> other.complexity if other.respond_to? :complexity
end

#==(other) ⇒ Boolean Also known as: eql?

Returns ‘true` if `other` is a RedisThrottle::RateLimit instance with the same #bucket, #limit, and #period.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

See Also:



32
33
34
35
36
37
# File 'lib/redis_throttle/rate_limit.rb', line 32

def ==(other)
  return true  if equal? other
  return false unless other.is_a?(self.class)

  @bucket == other.bucket && @limit == other.limit && @period == other.period
end

#complexityArray(Integer, Integer)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Strategy complexity pseudo-score.

Returns:

  • (Array(Integer, Integer))

    Strategy complexity pseudo-score



69
70
71
# File 'lib/redis_throttle/rate_limit.rb', line 69

def complexity
  [0, @limit]
end

#hashInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates an Integer hash value for this object.



62
63
64
# File 'lib/redis_throttle/rate_limit.rb', line 62

def hash
  @hash ||= [@bucket, @limit, @period].hash
end