From 9f97a8e265bd618333b98e2c8e35d14c8c57d7b1 Mon Sep 17 00:00:00 2001 From: Tian Jiang Date: Wed, 18 Nov 2020 14:49:53 -0500 Subject: [PATCH] Update README.md to include the weight for throttle --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b33c64a3..9db6af68 100644 --- a/README.md +++ b/README.md @@ -244,7 +244,7 @@ Throttle state is stored in a [configurable cache](#cache-store-configuration) ( #### `throttle(name, options, &block)` -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. +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. The request object is a [Rack::Request](http://www.rubydoc.info/gems/rack/Rack/Request). @@ -275,6 +275,14 @@ period_proc = proc { |req| req.env["REMOTE_USER"] == "admin" ? 1 : 60 } Rack::Attack.throttle('request per ip', limit: limit_proc, period: period_proc) do |request| request.ip end + +# Weight can be used to make some requests cost more than others while +# sharing the same limit. +weight_proc = proc { |req| req.path.start_with?('/search') ? 10 : 1 } + +Rack::Attack.throttle('request per ip', limit: 10, period: 1, weight: weight_proc) do |request| + request.ip +end ``` ### Tracks