1
0
Fork 0
mirror of https://github.com/benbusby/farside.git synced 2025-05-10 20:40:02 +00:00
farside/lib/farside/throttle.ex
2021-11-12 14:40:05 -07:00

21 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