don't bump ratelimit after unsuccessful attempts

This commit is contained in:
byte[] 2020-09-14 20:52:02 -04:00
parent aefc79eb8e
commit 6a045ce88e

View file

@ -31,12 +31,9 @@ defmodule PhilomenaWeb.LimitPlug do
] ]
key = "rl-#{Enum.join(data, "")}" key = "rl-#{Enum.join(data, "")}"
amt = Redix.command!(:redix, ["GET", key]) || 0
[amt, _] = conn = increment_after_post(conn, key, time)
Redix.pipeline!(:redix, [
["INCR", key],
["EXPIRE", key, time]
])
cond do cond do
amt <= limit -> amt <= limit ->
@ -86,4 +83,18 @@ defmodule PhilomenaWeb.LimitPlug do
_ -> false _ -> false
end end
end end
defp increment_after_post(conn, key, time) do
Conn.register_before_send(conn, fn conn ->
# Phoenix status returns 200 for form validation errors
if conn.status != 200 do
Redix.pipeline!(:redix, [
["INCR", key],
["EXPIRE", key, time]
])
end
conn
end)
end
end end