https://github.com/evuez/valve
🍶 An Elixir Plug with pluggable backends to rate-limit requests to your web app.
https://github.com/evuez/valve
Last synced: 15 days ago
JSON representation
🍶 An Elixir Plug with pluggable backends to rate-limit requests to your web app.
- Host: GitHub
- URL: https://github.com/evuez/valve
- Owner: evuez
- License: mit
- Created: 2017-09-25T13:42:25.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-26T08:09:18.000Z (about 7 years ago)
- Last Synced: 2025-04-10T23:12:57.551Z (15 days ago)
- Language: Elixir
- Homepage: https://hex.pm/packages/valve
- Size: 6.84 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Valve
An Elixir Plug to rate-limit requests to your web app.
## Installation
Add `valve` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:valve, "~> 0.2.0"}]
end
```Then plug it in your pipeline:
```elixir
defmodule MyApp.Router do
use MyApp, :router# ...
pipeline :api do
plug Valve# ...
end
```The default behavior when reaching the maximum number of requests allowed is to return an [`HTTP 429 (Too Many Requests)`](https://httpstatuses.com/429) with a `Retry-After` header.
This can be overridden by passing an `on_flood` option to the plug:```elixir
plug Valve, on_flood: fn (conn, retry_after) -> put_status(conn, 418) |> halt() end
```## Configuration
Put this in your config:
```elixir
config :valve,
rate: {10, 60}, # Give 10 tokens every 60 second
max_tokens: 20, # Give at most 20 tokens (also give 20 tokens initially)
pool_size: 8, # Number of Valve.Store processes (how many requests can be handled simultaneously)
store: Valve.Store.ETS # The storage adapter used to store buckets (you'll need to recompile the library if you change this)
```## Storage adapters
For now there's only an ETS backend (`Valve.Store.ETS`), contributions are welcome!
### ETS
```elixir
config :valve, Valve.Store.ETS,
table: :my_table, # Defaults to `:valve`
ttl: 48 * 3_600, # How much time (in seconds) before an entry should get swept (defaults to a day)
sweep_interval: 7_200 * 1000 # How often (in milliseconds) should stale entries be cleaned (defaults to an hour)
```