Class: Redis::AdequateRateLimiter
- Inherits:
-
Object
- Object
- Redis::AdequateRateLimiter
- Defined in:
- lib/redis/adequate_rate_limiter.rb
Overview
Wrapper for a Lua script that provides smooth, configurable, space-efficient & blazing fast rate limiting.
Usage:
require ‘redis’ require ‘redis/adequate_rate_limiter’
r = Redis.new rate_limiter = Redis::AdequateRateLimiter.new® rate_limiter.configure(r, event_type, max_allowed, over_interval, lockout_interval)
… if rate_limiter.allow?(r, event_type, actor)
# Count this action and check if it is allowed.
...
end …
If ‘allow?` is invoked on an event type that has not been configured, a ConfigNotDefinedError exception will be raised.
Defined Under Namespace
Classes: ConfigNotDefinedError
Instance Attribute Summary collapse
-
#sha1_digest ⇒ String
readonly
Lua script SHA1 digest.
Instance Method Summary collapse
-
#allow?(redis, event_type, actor) ⇒ Boolean
Check if an actor is allowed to perform an action of event_type.
-
#configure(redis, event_type, max_allowed, over_interval, lockout_interval) ⇒ void
Configure rate limiting for an event type See README for more details.
-
#initialize(redis) ⇒ AdequateRateLimiter
constructor
Create a new AdequateRateLimter instance.
- #namespaced_key(key) ⇒ Object
- #peek(redis, event_type, actor) ⇒ Object
- #peek_config(redis, event_type) ⇒ Object
Constructor Details
#initialize(redis) ⇒ AdequateRateLimiter
Create a new AdequateRateLimter instance
35 36 37 |
# File 'lib/redis/adequate_rate_limiter.rb', line 35 def initialize(redis) load_script(redis) end |
Instance Attribute Details
#sha1_digest ⇒ String (readonly)
Lua script SHA1 digest
31 32 33 |
# File 'lib/redis/adequate_rate_limiter.rb', line 31 def sha1_digest @sha1_digest end |
Instance Method Details
#allow?(redis, event_type, actor) ⇒ Boolean
Check if an actor is allowed to perform an action of event_type.
60 61 62 63 |
# File 'lib/redis/adequate_rate_limiter.rb', line 60 def allow?(redis, event_type, actor) q = available_quota(redis, event_type, actor) q.positive? end |
#configure(redis, event_type, max_allowed, over_interval, lockout_interval) ⇒ void
This method returns an undefined value.
Configure rate limiting for an event type See README for more details.
47 48 49 50 51 52 53 |
# File 'lib/redis/adequate_rate_limiter.rb', line 47 def configure(redis, event_type, max_allowed, over_interval, lockout_interval) key = namespaced_key(event_type) redis.del(key) redis.rpush(key, max_allowed) redis.rpush(key, over_interval) redis.rpush(key, lockout_interval) end |
#namespaced_key(key) ⇒ Object
73 74 75 |
# File 'lib/redis/adequate_rate_limiter.rb', line 73 def namespaced_key(key) "arl:#{key}" end |
#peek(redis, event_type, actor) ⇒ Object
65 66 67 |
# File 'lib/redis/adequate_rate_limiter.rb', line 65 def peek(redis, event_type, actor) redis.lrange(namespaced_key("#{event_type}:#{actor}"), 0, -1) end |
#peek_config(redis, event_type) ⇒ Object
69 70 71 |
# File 'lib/redis/adequate_rate_limiter.rb', line 69 def peek_config(redis, event_type) redis.lrange(namespaced_key(event_type), 0, -1) end |