mirror of
https://github.com/benbusby/farside.git
synced 2025-03-26 08:38:18 -04:00
22 lines
462 B
Elixir
22 lines
462 B
Elixir
defmodule Farside.Throttle do
|
|
import Plug.Conn
|
|
use PlugAttack
|
|
|
|
rule "throttle per ip", conn do
|
|
# throttle to 1 request per second
|
|
throttle(conn.remote_ip,
|
|
period: 1_000,
|
|
limit: 1,
|
|
storage: {PlugAttack.Storage.Ets, Farside.Throttle.Storage}
|
|
)
|
|
end
|
|
|
|
def allow_action(conn, _data, _opts), do: conn
|
|
|
|
def block_action(conn, _data, _opts) do
|
|
conn
|
|
|> send_resp(:forbidden, "Exceeded rate limit\n")
|
|
|> halt
|
|
end
|
|
end
|