Pleroma/lib/pleroma/web/federator/retry_queue.ex

72 lines
1.9 KiB
Elixir
Raw Normal View History

2018-08-26 20:17:13 +02:00
defmodule Pleroma.Web.Federator.RetryQueue do
use GenServer
alias Pleroma.Web.{WebFinger, Websub}
alias Pleroma.Web.ActivityPub.ActivityPub
require Logger
@websub Application.get_env(:pleroma, :websub)
@ostatus Application.get_env(:pleroma, :websub)
@httpoison Application.get_env(:pleroma, :websub)
@instance Application.get_env(:pleroma, :websub)
# initial timeout, 5 min
@initial_timeout 30_000
@max_retries 5
def init(args) do
{:ok, args}
end
def start_link() do
2018-11-19 17:08:41 +01:00
GenServer.start_link(__MODULE__, %{delivered: 0, dropped: 0}, name: __MODULE__)
2018-08-26 20:17:13 +02:00
end
def enqueue(data, transport, retries \\ 0) do
GenServer.cast(__MODULE__, {:maybe_enqueue, data, transport, retries + 1})
end
2018-11-19 17:08:41 +01:00
def get_retry_params(retries) do
2018-08-26 20:17:13 +02:00
if retries > @max_retries do
2018-11-19 17:08:41 +01:00
{:drop, "Max retries reached"}
2018-08-26 20:17:13 +02:00
else
2018-11-19 17:08:41 +01:00
{:retry, growth_function(retries)}
2018-08-26 20:17:13 +02:00
end
end
2018-11-19 17:08:41 +01:00
def handle_cast({:maybe_enqueue, data, transport, retries}, %{dropped: drop_count} = state) do
case get_retry_params(retries) do
{:retry, timeout} ->
Process.send_after(
__MODULE__,
{:send, data, transport, retries},
growth_function(retries)
)
2018-08-26 20:17:13 +02:00
{:noreply, state}
2018-11-19 17:08:41 +01:00
{:drop, message} ->
Logger.debug(message)
{:noreply, %{state | dropped: drop_count + 1}}
2018-08-26 20:17:13 +02:00
end
end
2018-11-19 17:08:41 +01:00
def handle_info({:send, data, transport, retries}, %{delivered: delivery_count} = state) do
case transport.publish_one(data) do
2018-08-26 20:17:13 +02:00
{:ok, _} ->
2018-11-19 17:08:41 +01:00
{:noreply, %{state | delivered: delivery_count + 1}}
2018-08-26 20:17:13 +02:00
{:error, reason} ->
2018-11-19 17:08:41 +01:00
enqueue(data, transport, retries)
2018-08-26 20:17:13 +02:00
{:noreply, state}
end
end
def handle_info(unknown, state) do
Logger.debug("RetryQueue: don't know what to do with #{inspect(unknown)}, ignoring")
{:noreply, state}
end
defp growth_function(retries) do
round(@initial_timeout * :math.pow(retries, 3))
end
end