mirror of
https://github.com/discourse/discourse.git
synced 2025-01-01 16:06:19 +08:00
859d61003e
This commit adds the `add_request_rate_limiter` plugin API which allows plugins to add custom rate limiters on top of the default rate limiters which requests by a user's id or the request's IP address. Example to add a rate limiter that rate limits all requests from Googlebot under the same rate limit bucket: ``` add_request_rate_limiter( identifier: :country, key: ->(request) { "country/#{DiscourseIpInfo.get(request.ip)[:country]}" }, activate_when: ->(request) { DiscourseIpInfo.get(request.ip)[:country].present? }, ) ```
21 lines
420 B
Ruby
21 lines
420 B
Ruby
# frozen_string_literal: true
|
|
|
|
module RequestTracker
|
|
module RateLimiters
|
|
class User < Base
|
|
def rate_limit_key
|
|
"user/#{@cookie[:user_id]}"
|
|
end
|
|
|
|
def rate_limit_globally?
|
|
false
|
|
end
|
|
|
|
def active?
|
|
@cookie && @cookie[:user_id] && @cookie[:trust_level] &&
|
|
@cookie[:trust_level] >= GlobalSetting.skip_per_ip_rate_limit_trust_level
|
|
end
|
|
end
|
|
end
|
|
end
|