Skip to content

Commit 9f97a8e

Browse files
committed
Update README.md to include the weight for throttle
1 parent 1ba3c5f commit 9f97a8e

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ Throttle state is stored in a [configurable cache](#cache-store-configuration) (
244244

245245
#### `throttle(name, options, &block)`
246246

247-
Name your custom throttle, provide `limit` and `period` as options, and make your ruby-block argument return the __discriminator__. This discriminator is how you tell rack-attack whether you're limiting per IP address, per user email or any other.
247+
Name your custom throttle, provide `limit`, `period` and `weight` as options, and make your ruby-block argument return the __discriminator__. This discriminator is how you tell rack-attack whether you're limiting per IP address, per user email or any other.
248248

249249
The request object is a [Rack::Request](http://www.rubydoc.info/gems/rack/Rack/Request).
250250

@@ -275,6 +275,14 @@ period_proc = proc { |req| req.env["REMOTE_USER"] == "admin" ? 1 : 60 }
275275
Rack::Attack.throttle('request per ip', limit: limit_proc, period: period_proc) do |request|
276276
request.ip
277277
end
278+
279+
# Weight can be used to make some requests cost more than others while
280+
# sharing the same limit.
281+
weight_proc = proc { |req| req.path.start_with?('/search') ? 10 : 1 }
282+
283+
Rack::Attack.throttle('request per ip', limit: 10, period: 1, weight: weight_proc) do |request|
284+
request.ip
285+
end
278286
```
279287

280288
### Tracks

0 commit comments

Comments
 (0)