Skip to content

Commit

Permalink
Update README.md to include the weight for throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
jiang925 committed Nov 18, 2020
1 parent 1ba3c5f commit 9f97a8e
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9f97a8e

Please sign in to comment.