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

Code before:
      #
      #     class SessionsController < ApplicationController
      #       rate_limit to: 10, within: 3.minutes, only: :create
      #     end
      #
      #     class SignupsController < ApplicationController
      #       rate_limit to: 1000, within: 10.seconds,
      #         by: -> { request.domain }, with: :redirect_to_busy, only: :new
      #
      #       private
      #         def redirect_to_busy
      #           redirect_to busy_controller_url, alert: "Too many signups on domain!"
      #         end
      #     end
      #
      #     class APIController < ApplicationController
      #       RATE_LIMIT_STORE = ActiveSupport::Cache::RedisCacheStore.new(url: ENV["REDIS_URL"])
      #       rate_limit to: 10, within: 3.minutes, store: RATE_LIMIT_STORE
      #     end
      #
      #     class SessionsController < ApplicationController
      #       rate_limit to: 3.times, within: 2.seconds, name: "short-term"
      #       rate_limit to: 10.times, within: 5.minutes, name: "long-term"
      #     end
      def rate_limit(to:, within:, by: :rate_limit_by, with: :rate_limit_with, store: cache_store, name: nil, **options)
        to = to.size if to.is_a?(Enumerator)

        before_action -> { rate_limiting(to: to, within: within, by: by, with: with, store: store, name: name) }, **options
      end
    end

    private
      def rate_limiting(to:, within:, by:, with:, store:, name:)
        callable = ->(c) { c.is_a?(Symbol) ? send(c) : instance_exec(&c) }

        cache_key = ["rate-limit", controller_path, name, callable.call(by)].compact.join(":")
        count = store.increment(cache_key, 1, expires_in: within)
        if count && count > to
          request.rate_limit = RateLimit.new(name: name, count: to, retry_after: within.from_now)

          ActiveSupport::Notifications.instrument("rate_limit.action_controller", request: request) do
            callable.call(with)
          end
        end
      end

      def rate_limit_by
        request.remote_ip
      end

      def rate_limit_with

Review comment (suggestion):
This change isn't necessary and that API is worse. In no world `rate_limit to: 3.times` is better than `rate_limit to: 3`

Code after:
      #     end
      def rate_limit(to:, within:, by: -> { request.remote_ip }, with: -> { raise TooManyRequests }, store: cache_store, name: nil, scope: nil, **options)
        before_action -> { rate_limiting(to: to, within: within, by: by, with: with, store: store, name: name, scope: scope || controller_path) }, **options
      end
    end

    private
      def rate_limiting(to:, within:, by:, with:, store:, name:, scope:)
        by = by.is_a?(Symbol) ? send(by) : instance_exec(&by)

        cache_key = ["rate-limit", scope, name, by].compact.join(":")
        count = store.increment(cache_key, 1, expires_in: within)
        if count && count > to
          ActiveSupport::Notifications.instrument("rate_limit.action_controller",
              request: request,
              count: count,
              to: to,
              within: within,
              by: by,
              name: name,
              scope: scope,
              cache_key: cache_key) do
            with.is_a?(Symbol) ? send(with) : instance_exec(&with)
          end
        end
      end
  end
end