-
Notifications
You must be signed in to change notification settings - Fork 337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow customization of weight per request for rate limiting #505
base: main
Are you sure you want to change the base?
Changes from 4 commits
3310261
1ba3c5f
f75d902
1c02f54
531679d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,79 @@ | |
end | ||
end | ||
|
||
it "supports a non-1 constant weight" do | ||
Rack::Attack.throttle("by ip", limit: 4, period: 60, weight: 2) do |request| | ||
request.ip | ||
end | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 200, last_response.status | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 200, last_response.status | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 429, last_response.status | ||
assert_nil last_response.headers["Retry-After"] | ||
assert_equal "Retry later\n", last_response.body | ||
|
||
get "/", {}, "REMOTE_ADDR" => "5.6.7.8" | ||
|
||
assert_equal 200, last_response.status | ||
|
||
Timecop.travel(60) do | ||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 200, last_response.status | ||
end | ||
end | ||
|
||
it "supports a dynamic weight" do | ||
weight_proc = lambda do |request| | ||
if request.env["X-APIKey"] == "private-secret" | ||
3 | ||
else | ||
2 | ||
end | ||
end | ||
Rack::Attack.throttle("by ip", limit: 4, period: 60, weight: weight_proc) do |request| | ||
request.ip | ||
end | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 200, last_response.status | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 200, last_response.status | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 429, last_response.status | ||
assert_nil last_response.headers["Retry-After"] | ||
assert_equal "Retry later\n", last_response.body | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's a good place to test that a weight of zero also returns a 429. (Add Test suggestion: jamiemccarthy@6ec96eb There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jamiemccarthy Thank you for the suggestion and sorry for the delay! I missed all these notifications. I'll take a look soon and get back! |
||
get "/", {}, "REMOTE_ADDR" => "5.6.7.8", "X-APIKey" => "private-secret" | ||
|
||
assert_equal 200, last_response.status | ||
|
||
get "/", {}, "REMOTE_ADDR" => "5.6.7.8", "X-APIKey" => "private-secret" | ||
|
||
assert_equal 429, last_response.status | ||
assert_nil last_response.headers["Retry-After"] | ||
assert_equal "Retry later\n", last_response.body | ||
|
||
Timecop.travel(60) do | ||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 200, last_response.status | ||
end | ||
end | ||
|
||
it "returns correct Retry-After header if enabled" do | ||
Rack::Attack.throttled_response_retry_after_header = true | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: may worth mention weight is an optional option