mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 13:35:21 +02:00
Relax API rate limits for self-hosted deployments (#2465)
- Introduced NoopApiRateLimiter to effectively disable API rate limiting for self-hosted mode. - Updated ApiRateLimiter to delegate to NoopApiRateLimiter when running self-hosted. - Increased Rack::Attack throttle limits significantly for self-hosted deployments. - Added tests for NoopApiRateLimiter to ensure correct behavior. - This allows self-hosted users to make more API requests without restriction, while keeping stricter limits for SaaS deployments.
This commit is contained in:
parent
da2045dbd8
commit
3f92fe0f6f
5 changed files with 114 additions and 4 deletions
|
@ -98,7 +98,7 @@ class Api::V1::BaseController < ApplicationController
|
|||
@current_user = @api_key.user
|
||||
@api_key.update_last_used!
|
||||
@authentication_method = :api_key
|
||||
@rate_limiter = ApiRateLimiter.new(@api_key)
|
||||
@rate_limiter = ApiRateLimiter.limit(@api_key)
|
||||
setup_current_context_for_api
|
||||
true
|
||||
end
|
||||
|
|
|
@ -67,7 +67,17 @@ class ApiRateLimiter
|
|||
|
||||
# Class method to get usage for an API key without incrementing
|
||||
def self.usage_for(api_key)
|
||||
new(api_key).usage_info
|
||||
limit(api_key).usage_info
|
||||
end
|
||||
|
||||
def self.limit(api_key)
|
||||
if Rails.application.config.app_mode.self_hosted?
|
||||
# Use NoopApiRateLimiter for self-hosted mode
|
||||
# This means no rate limiting is applied
|
||||
NoopApiRateLimiter.new(api_key)
|
||||
else
|
||||
new(api_key)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
|
39
app/services/noop_api_rate_limiter.rb
Normal file
39
app/services/noop_api_rate_limiter.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
class NoopApiRateLimiter
|
||||
def initialize(api_key)
|
||||
@api_key = api_key
|
||||
end
|
||||
|
||||
def rate_limit_exceeded?
|
||||
false
|
||||
end
|
||||
|
||||
def increment_request_count!
|
||||
# No operation
|
||||
end
|
||||
|
||||
def current_count
|
||||
0
|
||||
end
|
||||
|
||||
def rate_limit
|
||||
Float::INFINITY
|
||||
end
|
||||
|
||||
def reset_time
|
||||
0
|
||||
end
|
||||
|
||||
def usage_info
|
||||
{
|
||||
current_count: 0,
|
||||
rate_limit: Float::INFINITY,
|
||||
remaining: Float::INFINITY,
|
||||
reset_time: 0,
|
||||
tier: :noop
|
||||
}
|
||||
end
|
||||
|
||||
def self.usage_for(api_key)
|
||||
new(api_key).usage_info
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue