mirror of
https://github.com/discourse/discourse.git
synced 2025-01-06 15:36: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? }, ) ```
41 lines
1.0 KiB
Ruby
41 lines
1.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module RequestTracker
|
|
module RateLimiters
|
|
class Base
|
|
# :nodoc:
|
|
def initialize(request, cookie)
|
|
@request = request
|
|
@cookie = cookie
|
|
end
|
|
|
|
# This method is meant to be implemented in subclasses.
|
|
#
|
|
# @return [String] The key used to identify the rate limiter.
|
|
def rate_limit_key
|
|
raise NotImplementedError
|
|
end
|
|
|
|
# :nodoc:
|
|
def error_code_identifier
|
|
self.class.name.underscore.split("/").last
|
|
end
|
|
|
|
# This method is meant to be implemented in subclasses.
|
|
#
|
|
# @return [Boolean] Indicates if the rate limiter should be used for the request.
|
|
def active?
|
|
raise NotImplementedError
|
|
end
|
|
|
|
# This method is meant to be implemented in subclasses.
|
|
#
|
|
# @return [Boolean] Indicates whether the rate limit applies globally across all sites in the cluster or just for
|
|
# the current site.
|
|
def rate_limit_globally?
|
|
raise NotImplementedError
|
|
end
|
|
end
|
|
end
|
|
end
|