Class: RateLimiter::Storage::Memory
- Defined in:
- lib/rate_limiter/storage/memory.rb
Instance Method Summary collapse
- #add_request(client_id) ⇒ Object
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
- #requests_in_last_minute(client_id) ⇒ Object
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
4 5 6 7 |
# File 'lib/rate_limiter/storage/memory.rb', line 4 def initialize @store = Hash.new { |hash, key| hash[key] = [] } @mutex = Mutex.new end |
Instance Method Details
#add_request(client_id) ⇒ Object
9 10 11 12 13 |
# File 'lib/rate_limiter/storage/memory.rb', line 9 def add_request(client_id) @mutex.synchronize do @store[client_id] << Time.now end end |
#requests_in_last_minute(client_id) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/rate_limiter/storage/memory.rb', line 15 def requests_in_last_minute(client_id) @mutex.synchronize do one_minute_ago = Time.now - 60 @store[client_id].select { || > one_minute_ago } end end |