1
0
Fork 0
mirror of https://github.com/benbusby/farside.git synced 2025-04-20 19:08:42 +00:00
farside/lib/farside/throttle.ex
Ben Busby 22e9135e0c
Reuse previous instance within rate-limit interval
Rather than blocking <1s back-to-back queries from the same IP, Farside
will now re-use the previously selected instance.

Fixes #20
2022-03-18 13:51:37 -06:00

20 lines
434 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 = assign(conn, :throttle, 1)
conn
end
end