Repository: rails/rails
File: actionpack/lib/action_controller/metal/rate_limiting.rb (Ruby)

Code before:
# frozen_string_literal: true

# :markup: markdown

module ActionController # :nodoc:
  module RateLimiting
    extend ActiveSupport::Concern

    RateLimit = Struct.new(:name, :count, :retry_after) # :nodoc:

    module ClassMethods
      # Applies a rate limit to all actions or those specified by the normal
      # `before_action` filters with `only:` and `except:`.
      #
      # The maximum number of requests allowed is specified `to:` and constrained to
      # the window of time given by `within:`.
      #
      # Rate limits are unique to the IP address making the request, by default.
      # You can provide your own identity function by passing a method name or a callable in the `by:`
      # parameter. Callables are evaluated within the context of the controller processing the
      # request.
      #
      # Requests that exceed the rate limit will raise an `ActionController::TooManyRequests`
      # error. By default, Action Dispatch will rescue from the error and refuse the request
      # with a `429 Too Many Requests` response.
      #
      # You can specialize this by passing either a method name or a callable in the `with:`
      # parameter. Callables are evaluated within the context of the controller processing the
      # request.
      #
      # Rate limiting relies on a backing `ActiveSupport::Cache` store and defaults to
      # `config.action_controller.cache_store`, which itself defaults to the global
      # `config.cache_store`. If you don't want to store rate limits in the same
      # datastore as your general caches, you can pass a custom store in the `store`

Review comment (suggestion):
This should be documented, no? Otherwise people will not know there is a name, count and retry_after methods in the request `rate_limit`. We should also document the methods in the request and response

Code after:
# frozen_string_literal: true

# :markup: markdown

module ActionController # :nodoc:
  module RateLimiting
    extend ActiveSupport::Concern

    module ClassMethods
      # Applies a rate limit to all actions or those specified by the normal
      # `before_action` filters with `only:` and `except:`.
      #
      # The maximum number of requests allowed is specified `to:` and constrained to
      # the window of time given by `within:`.
      #
      # Rate limits are by default unique to the ip address making the request, but
      # you can provide your own identity function by passing a callable in the `by:`
      # parameter. It's evaluated within the context of the controller processing the
      # request.
      #
      # By default, rate limits are scoped to the controller's path. If you want to
      # share rate limits across multiple controllers, you can provide your own scope,
      # by passing value in the `scope:` parameter.
      #
      # Requests that exceed the rate limit will raise an `ActionController::TooManyRequests`
      # error. By default, Action Dispatch will rescue from the error and refuse the request
      # with a `429 Too Many Requests` response. You can specialize this by passing a callable in the `with:`
      # parameter. It's evaluated within the context of the controller processing the
      # request.
      #
      # Rate limiting relies on a backing `ActiveSupport::Cache` store and defaults to
      # `config.action_controller.cache_store`, which itself defaults to the global
      # `config.cache_store`. If you don't want to store rate limits in the same
      # datastore as your general caches, you can pass a custom store in the `store`