Class: Rollie::RateLimiter
- Inherits:
-
Object
- Object
- Rollie::RateLimiter
- Defined in:
- lib/rollie/rate_limiter.rb
Instance Method Summary collapse
-
#count ⇒ Integer
The current count of this RateLimiter.
-
#initialize(key, options = {}) ⇒ RateLimiter
constructor
Create a new RateLimiter instance.
-
#within_limit ⇒ Status
Executes a block as long as the current rate is within the limit.
Constructor Details
#initialize(key, options = {}) ⇒ RateLimiter
Create a new RateLimiter instance.
15 16 17 18 19 20 |
# File 'lib/rollie/rate_limiter.rb', line 15 def initialize(key, = {}) @key = "#{[:namespace]}#{key}" @limit = [:limit] || 25 @interval = ([:interval] || 1000) * 1000 @count_blocked = .key?(:count_blocked) ? [:count_blocked] : false end |
Instance Method Details
#count ⇒ Integer
Returns The current count of this RateLimiter.
38 39 40 41 42 43 |
# File 'lib/rollie/rate_limiter.rb', line 38 def count Rollie.redis do |conn| range = conn.zrange(@key, 0, -1) range.length end end |
#within_limit ⇒ Status
Executes a block as long as the current rate is within the limit.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rollie/rate_limiter.rb', line 25 def within_limit raise ArgumentError, 'requires a block' unless block_given? Rollie.redis do |conn| status = inc(conn) unless status.exceeded? yield end status end end |