Class: Legion::API::Middleware::RateLimit::MemoryStore
- Inherits:
-
Object
- Object
- Legion::API::Middleware::RateLimit::MemoryStore
- Defined in:
- lib/legion/api/middleware/rate_limit.rb
Instance Method Summary collapse
- #count(key, window) ⇒ Object
- #increment(key, window) ⇒ Object
-
#initialize ⇒ MemoryStore
constructor
A new instance of MemoryStore.
- #reap! ⇒ Object
Constructor Details
#initialize ⇒ MemoryStore
Returns a new instance of MemoryStore.
14 15 16 |
# File 'lib/legion/api/middleware/rate_limit.rb', line 14 def initialize @counters = Concurrent::Hash.new end |
Instance Method Details
#count(key, window) ⇒ Object
23 24 25 |
# File 'lib/legion/api/middleware/rate_limit.rb', line 23 def count(key, window) @counters["#{key}:#{window}"] || 0 end |
#increment(key, window) ⇒ Object
18 19 20 21 |
# File 'lib/legion/api/middleware/rate_limit.rb', line 18 def increment(key, window) composite = "#{key}:#{window}" @counters[composite] = (@counters[composite] || 0) + 1 end |
#reap! ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/legion/api/middleware/rate_limit.rb', line 27 def reap! cutoff = (Time.now.to_i / WINDOW_SIZE * WINDOW_SIZE) - (WINDOW_SIZE * 2) @counters.each_key do |k| window = k.split(':').last.to_i @counters.delete(k) if window < cutoff end end |