Class: Rack::AI::Features::RateLimiting
- Inherits:
-
Object
- Object
- Rack::AI::Features::RateLimiting
- Defined in:
- lib/rack/ai/features/rate_limiting.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Instance Method Summary collapse
- #enabled? ⇒ Boolean
-
#initialize(provider, config) ⇒ RateLimiting
constructor
A new instance of RateLimiting.
- #process_request(env) ⇒ Object
- #process_response? ⇒ Boolean
Constructor Details
#initialize(provider, config) ⇒ RateLimiting
Returns a new instance of RateLimiting.
9 10 11 12 13 14 15 |
# File 'lib/rack/ai/features/rate_limiting.rb', line 9 def initialize(provider, config) @name = :rate_limiting @provider = provider @config = config @rate_store = {} @cleanup_timer = Time.now end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/rack/ai/features/rate_limiting.rb', line 7 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/rack/ai/features/rate_limiting.rb', line 7 def name @name end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
7 8 9 |
# File 'lib/rack/ai/features/rate_limiting.rb', line 7 def provider @provider end |
Instance Method Details
#enabled? ⇒ Boolean
17 18 19 |
# File 'lib/rack/ai/features/rate_limiting.rb', line 17 def enabled? @config.feature_enabled?(:rate_limiting) end |
#process_request(env) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rack/ai/features/rate_limiting.rb', line 25 def process_request(env) client_ip = get_client_ip(env) current_time = Time.now # Cleanup old entries every 5 minutes cleanup_old_entries if current_time - @cleanup_timer > 300 # Get rate limit settings window_size = @config.rate_limiting.window_size || 3600 # 1 hour default max_requests = @config.rate_limiting.max_requests || 1000 # 1000 requests default # Initialize client data if not exists @rate_store[client_ip] ||= { requests: [], blocked_until: nil } client_data = @rate_store[client_ip] # Check if client is currently blocked if client_data[:blocked_until] && current_time < client_data[:blocked_until] return { action: :block, reason: "Rate limit exceeded - blocked until #{client_data[:blocked_until]}", status: 429, feature: :rate_limiting, timestamp: current_time.iso8601, retry_after: (client_data[:blocked_until] - current_time).to_i } end # Remove old requests outside the window window_start = current_time - window_size client_data[:requests].reject! { || < window_start } # Check if rate limit is exceeded if client_data[:requests].length >= max_requests block_duration = @config.rate_limiting.block_duration || 3600 # 1 hour default client_data[:blocked_until] = current_time + block_duration return { action: :block, reason: "Rate limit exceeded: #{client_data[:requests].length}/#{max_requests} requests in #{window_size}s", status: 429, feature: :rate_limiting, timestamp: current_time.iso8601, retry_after: block_duration, requests_count: client_data[:requests].length, window_size: window_size } end # Add current request client_data[:requests] << current_time { action: :allow, feature: :rate_limiting, timestamp: current_time.iso8601, requests_count: client_data[:requests].length, requests_remaining: max_requests - client_data[:requests].length, window_size: window_size, reset_time: (window_start + window_size).iso8601 } end |
#process_response? ⇒ Boolean
21 22 23 |
# File 'lib/rack/ai/features/rate_limiting.rb', line 21 def process_response? false end |