Class: RailsAi::Security::RateLimiter
- Inherits:
-
Object
- Object
- RailsAi::Security::RateLimiter
- Defined in:
- lib/rails_ai/security.rb
Instance Method Summary collapse
- #check_limit(identifier) ⇒ Object
-
#initialize(limit: 100, window: 3600) ⇒ RateLimiter
constructor
A new instance of RateLimiter.
Constructor Details
#initialize(limit: 100, window: 3600) ⇒ RateLimiter
Returns a new instance of RateLimiter.
17 18 19 20 21 |
# File 'lib/rails_ai/security.rb', line 17 def initialize(limit: 100, window: 3600) @limit = limit @window = window @requests = {} end |
Instance Method Details
#check_limit(identifier) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rails_ai/security.rb', line 23 def check_limit(identifier) now = Time.now.to_i window_start = now - @window @requests.delete_if { |_, | < window_start } current_requests = @requests.count { |_, | >= window_start } if current_requests >= @limit raise RateLimitError, "Rate limit exceeded: #{@limit} requests per #{@window} seconds" end @requests[identifier] = now true end |