Merge pull request #50788 from Shopify/cleanup-file-store

`RateLimiting` Cleanups
This commit is contained in:
Jean Boussier 2024-01-18 17:54:44 +01:00 committed by GitHub
commit 8e0a702bf3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 4 deletions

View File

@ -4,13 +4,13 @@ require "abstract_unit"
class RateLimitedController < ActionController::Base
self.cache_store = ActiveSupport::Cache::MemoryStore.new
rate_limit to: 2, within: 2.seconds, by: -> { Thread.current[:redis_test_seggregation] }, only: :limited_to_two
rate_limit to: 2, within: 2.seconds, only: :limited_to_two
def limited_to_two
head :ok
end
rate_limit to: 2, within: 2.seconds, by: -> { Thread.current[:redis_test_seggregation] }, with: -> { head :forbidden }, only: :limited_with
rate_limit to: 2, within: 2.seconds, by: -> { params[:rate_limit_key] }, with: -> { head :forbidden }, only: :limited_with
def limited_with
head :ok
end
@ -44,6 +44,16 @@ class RateLimitingTest < ActionController::TestCase
end
end
test "limit by" do
get :limited_with
get :limited_with
get :limited_with
assert_response :forbidden
get :limited_with, params: { rate_limit_key: "other" }
get :limited_with
end
test "limited with" do
get :limited_with
get :limited_with

View File

@ -210,13 +210,12 @@ module ActiveSupport
# Modifies the amount of an integer value that is stored in the cache.
# If the key is not found it is created and set to +amount+.
def modify_value(name, amount, options)
file_name = normalize_key(name, options)
options = merged_options(options)
key = normalize_key(name, options)
version = normalize_version(name, options)
amount = Integer(amount)
lock_file(file_name) do
lock_file(key) do
entry = read_entry(key, **options)
if !entry || entry.expired? || entry.mismatched?(version)