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

Code before:
      #       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_controller_url, alert: "Too many signups on domain!" }, only: :new
      #     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
      #       rate_limit to: 100, within: 5.minutes, scope: :api_global
      #     end
      #
      #     class SessionsController < ApplicationController
      #       rate_limit to: 3, within: 2.seconds, name: "short-term"
      #       rate_limit to: 10, within: 5.minutes, name: "long-term"
      #     end
      def rate_limit(to:, within:, by: -> { request.remote_ip }, with: -> { head :too_many_requests }, 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) }, **options
      end
    end

    private
      def rate_limiting(to:, within:, by:, with:, store:, name:, scope:)
        cache_key = ["rate-limit", scope || controller_path, name, instance_exec(&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) do
            instance_exec(&with)
          end
        end
      end
  end
end


Review comment (performance):
```suggestion
      def rate_limit(to:, within:, by: -> { request.remote_ip }, with: -> { head :too_many_requests }, store: cache_store, name: nil, scope: controller_path, **options)
        before_action -> { rate_limiting(to: to, within: within, by: by, with: with, store: store, name: name, scope: scope) }, **options
end
end

private
      def rate_limiting(to:, within:, by:, with:, store:, name:, scope:)
        cache_key = ["rate-limit", scope, name, instance_exec(&by)].compact.join(":")
```

Code after:
      #       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_controller_url, alert: "Too many signups on domain!" }, only: :new
      #     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
      #       rate_limit to: 100, within: 5.minutes, scope: :api_global
      #     end
      #
      #     class SessionsController < ApplicationController
      #       rate_limit to: 3, within: 2.seconds, name: "short-term"
      #       rate_limit to: 10, within: 5.minutes, name: "long-term"
      #     end
      def rate_limit(to:, within:, by: -> { request.remote_ip }, with: -> { head :too_many_requests }, 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 = 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
            instance_exec(&with)
          end
        end
      end
  end
end