Skip to content

Commit

Permalink
Add a Rack example as well
Browse files Browse the repository at this point in the history
  • Loading branch information
julik authored Nov 14, 2023
1 parent 6942b78 commit 313124d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ rescue_from Pecorino::Throttle::Throttled do |e|
end
```

and in a Rack application you can rescue inline:

```ruby
def call(env)
# ...your code
rescue Pecorino::Throttle::Throttled => e
[429, {"Retry-After" => e.retry_after.to_s}, []]
end
```

The exception has an attribute called `retry_after` which you can use to render the appropriate 429 response.

Although this approach might be susceptible to race conditions, you can interrogate your throttle before potentially causing an exception - and display an appropriate error message if the throttle would trigger anyway:
Expand Down

0 comments on commit 313124d

Please sign in to comment.