Class: Rack::AI::Features::RateLimiter
- Inherits:
-
Object
- Object
- Rack::AI::Features::RateLimiter
- Defined in:
- lib/rack/ai/features/rate_limiter.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #enabled? ⇒ Boolean
-
#initialize(config) ⇒ RateLimiter
constructor
A new instance of RateLimiter.
- #process_request(env) ⇒ Object
- #process_response? ⇒ Boolean
Constructor Details
#initialize(config) ⇒ RateLimiter
Returns a new instance of RateLimiter.
11 12 13 14 15 16 17 |
# File 'lib/rack/ai/features/rate_limiter.rb', line 11 def initialize(config) @name = :rate_limiter @config = config @cache = {} @cleanup_interval = 300 # 5 minutes @last_cleanup = Time.now end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/rack/ai/features/rate_limiter.rb', line 9 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/rack/ai/features/rate_limiter.rb', line 9 def name @name end |
Instance Method Details
#enabled? ⇒ Boolean
19 20 21 |
# File 'lib/rack/ai/features/rate_limiter.rb', line 19 def enabled? @config.feature_enabled?(:rate_limiter) end |
#process_request(env) ⇒ Object
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 |
# File 'lib/rack/ai/features/rate_limiter.rb', line 27 def process_request(env) return { processed: false, reason: "disabled" } unless enabled? client_id = extract_client_id(env) current_time = Time.now cleanup_expired_entries if should_cleanup? # Get or initialize client data client_data = @cache[client_id] ||= { requests: [], blocked_until: nil } # Check if client is currently blocked if client_data[:blocked_until] && current_time < client_data[:blocked_until] return { processed: true, action: :block, reason: "rate_limited", blocked_until: client_data[:blocked_until], feature: @name, timestamp: current_time.iso8601 } end # Clean old requests window_start = current_time - rate_limit_window client_data[:requests].reject! { |req_time| req_time < window_start } # Check rate limit if client_data[:requests].size >= max_requests_per_window # Block client for penalty duration client_data[:blocked_until] = current_time + penalty_duration return { processed: true, action: :block, reason: "rate_limit_exceeded", requests_count: client_data[:requests].size, blocked_until: client_data[:blocked_until], feature: @name, timestamp: current_time.iso8601 } end # Record this request client_data[:requests] << current_time { processed: true, action: :allow, requests_count: client_data[:requests].size, remaining_requests: max_requests_per_window - client_data[:requests].size, feature: @name, timestamp: current_time.iso8601 } end |
#process_response? ⇒ Boolean
23 24 25 |
# File 'lib/rack/ai/features/rate_limiter.rb', line 23 def process_response? false end |