Clean captchas up periodically, not schedule it after theyre created

This commit is contained in:
Ekaterina Vaartis 2018-12-16 23:41:11 +03:00
parent 73576ab64e
commit ef6829382a
4 changed files with 30 additions and 20 deletions

View file

@ -14,6 +14,10 @@ defmodule Pleroma.Captcha do
ets_name = Module.concat(method(), Ets) ets_name = Module.concat(method(), Ets)
^ets_name = :ets.new(Module.concat(method(), Ets), @ets_options) ^ets_name = :ets.new(Module.concat(method(), Ets), @ets_options)
# Clean up old captchas every few minutes
seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
Process.send_after(self(), :cleanup, 1000 * seconds_retained)
{:ok, nil} {:ok, nil}
end end
@ -38,13 +42,7 @@ defmodule Pleroma.Captcha do
if !enabled do if !enabled do
{:reply, %{type: :none}, state} {:reply, %{type: :none}, state}
else else
new_captcha = method().new() {:reply, method().new(), state}
seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
# Wait several minutes and if the captcha is still there, delete it
Process.send_after(self(), {:cleanup, new_captcha.token}, 1000 * seconds_retained)
{:reply, new_captcha, state}
end end
end end
@ -54,8 +52,12 @@ defmodule Pleroma.Captcha do
end end
@doc false @doc false
def handle_info({:cleanup, token}, state) do def handle_info(:cleanup, state) do
method().cleanup(token) :ok = method().cleanup()
seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
# Schedule the next clenup
Process.send_after(self(), :cleanup, 1000 * seconds_retained)
{:noreply, state} {:noreply, state}
end end

View file

@ -24,5 +24,5 @@ defmodule Pleroma.Captcha.Service do
@doc """ @doc """
This function is called periodically to clean up old captchas This function is called periodically to clean up old captchas
""" """
@callback cleanup(token :: String.t()) :: :ok @callback cleanup() :: :ok
end end

View file

@ -1,4 +1,6 @@
defmodule Pleroma.Captcha.Kocaptcha do defmodule Pleroma.Captcha.Kocaptcha do
alias Calendar.DateTime
alias Pleroma.Captcha.Service alias Pleroma.Captcha.Service
@behaviour Service @behaviour Service
@ -17,7 +19,7 @@ defmodule Pleroma.Captcha.Kocaptcha do
token = json_resp["token"] token = json_resp["token"]
true = :ets.insert(@ets, {token, json_resp["md5"]}) true = :ets.insert(@ets, {token, json_resp["md5"], DateTime.now_utc()})
%{type: :kocaptcha, token: token, url: endpoint <> json_resp["url"]} %{type: :kocaptcha, token: token, url: endpoint <> json_resp["url"]}
end end
@ -26,10 +28,10 @@ defmodule Pleroma.Captcha.Kocaptcha do
@impl Service @impl Service
def validate(token, captcha) do def validate(token, captcha) do
with false <- is_nil(captcha), with false <- is_nil(captcha),
[{^token, saved_md5}] <- :ets.lookup(@ets, token), [{^token, saved_md5, _}] <- :ets.lookup(@ets, token),
true <- :crypto.hash(:md5, captcha) |> Base.encode16() == String.upcase(saved_md5) do true <- :crypto.hash(:md5, captcha) |> Base.encode16() == String.upcase(saved_md5) do
# Clear the saved value # Clear the saved value
cleanup(token) :ets.delete(@ets, token)
true true
else else
@ -38,11 +40,17 @@ defmodule Pleroma.Captcha.Kocaptcha do
end end
@impl Service @impl Service
def cleanup(token) do def cleanup() do
# Only delete the entry if it exists in the table, because ets:delete raises an exception if it does not seconds_retained = Pleroma.Config.get!([Pleroma.Captcha, :seconds_retained])
case :ets.lookup(@ets, token) do
[{^token, _}] -> :ets.delete(@ets, token) # Go through captchas and remove expired ones
_ -> true :ets.tab2list(@ets)
end |> Enum.each(fn {token, _, time_inserted} ->
# time created + expiration time = time when the captcha should be removed
remove_time = DateTime.add!(time_inserted, seconds_retained)
if DateTime.after?(DateTime.now_utc(), remove_time), do: :ets.delete(@ets, token)
end)
:ok
end end
end end

View file

@ -9,5 +9,5 @@ defmodule Pleroma.Captcha.Mock do
def validate(_token, _captcha), do: true def validate(_token, _captcha), do: true
@impl Service @impl Service
def cleanup(_token), do: true def cleanup(), do: :ok
end end