1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Federator.RetryQueue do
11 queue_table = :ets.new(:pleroma_retry_queue, [:bag, :protected])
13 {:ok, %{args | queue_table: queue_table, running_jobs: :sets.new()}}
18 if Pleroma.Config.get(:env) == :test,
20 else: Pleroma.Config.get([__MODULE__, :enabled], false)
23 Logger.info("Starting retry queue")
28 %{delivered: 0, dropped: 0, queue_table: nil, running_jobs: nil},
35 Logger.info("Retry queue disabled")
40 def enqueue(data, transport, retries \\ 0) do
41 GenServer.cast(__MODULE__, {:maybe_enqueue, data, transport, retries + 1})
45 GenServer.call(__MODULE__, :get_stats)
49 GenServer.call(__MODULE__, :reset_stats)
52 def get_retry_params(retries) do
53 if retries > Pleroma.Config.get([__MODULE__, :max_retries]) do
54 {:drop, "Max retries reached"}
56 {:retry, growth_function(retries)}
60 def get_retry_timer_interval do
61 Pleroma.Config.get([:retry_queue, :interval], 1000)
64 defp ets_count_expires(table, current_time) do
70 [{:"=<", :"$1", {:const, current_time}}],
77 defp ets_pop_n_expired(table, current_time, desired) do
78 {popped, _continuation} =
84 [{:"=<", :"$1", {:const, current_time}}],
93 :ets.delete_object(table, e)
99 def maybe_start_job(running_jobs, queue_table) do
100 # we don't want to hit the ets or the DateTime more times than we have to
101 # could optimize slightly further by not using the count, and instead grabbing
102 # up to N objects early...
103 current_time = DateTime.to_unix(DateTime.utc_now())
104 n_running_jobs = :sets.size(running_jobs)
106 if n_running_jobs < Pleroma.Config.get([__MODULE__, :max_jobs]) do
107 n_ready_jobs = ets_count_expires(queue_table, current_time)
109 if n_ready_jobs > 0 do
110 # figure out how many we could start
111 available_job_slots = Pleroma.Config.get([__MODULE__, :max_jobs]) - n_running_jobs
112 start_n_jobs(running_jobs, queue_table, current_time, available_job_slots)
121 defp start_n_jobs(running_jobs, _queue_table, _current_time, 0) do
125 defp start_n_jobs(running_jobs, queue_table, current_time, available_job_slots)
126 when available_job_slots > 0 do
127 candidates = ets_pop_n_expired(queue_table, current_time, available_job_slots)
130 |> List.foldl(running_jobs, fn {_, e}, rj ->
131 {:ok, pid} = Task.start(fn -> worker(e) end)
132 mref = Process.monitor(pid)
133 :sets.add_element(mref, rj)
137 def worker({:send, data, transport, retries}) do
138 case transport.publish_one(data) do
140 GenServer.cast(__MODULE__, :inc_delivered)
144 enqueue(data, transport, retries)
149 def handle_call(:get_stats, _from, %{delivered: delivery_count, dropped: drop_count} = state) do
150 {:reply, %{delivered: delivery_count, dropped: drop_count}, state}
153 def handle_call(:reset_stats, _from, %{delivered: delivery_count, dropped: drop_count} = state) do
154 {:reply, %{delivered: delivery_count, dropped: drop_count},
155 %{state | delivered: 0, dropped: 0}}
158 def handle_cast(:reset_stats, state) do
159 {:noreply, %{state | delivered: 0, dropped: 0}}
163 {:maybe_enqueue, data, transport, retries},
164 %{dropped: drop_count, queue_table: queue_table, running_jobs: running_jobs} = state
166 case get_retry_params(retries) do
168 :ets.insert(queue_table, {timeout, {:send, data, transport, retries}})
169 running_jobs = maybe_start_job(running_jobs, queue_table)
170 {:noreply, %{state | running_jobs: running_jobs}}
173 Logger.debug(message)
174 {:noreply, %{state | dropped: drop_count + 1}}
178 def handle_cast(:kickoff_timer, state) do
179 retry_interval = get_retry_timer_interval()
180 Process.send_after(__MODULE__, :retry_timer_run, retry_interval)
184 def handle_cast(:inc_delivered, %{delivered: delivery_count} = state) do
185 {:noreply, %{state | delivered: delivery_count + 1}}
188 def handle_cast(:inc_dropped, %{dropped: drop_count} = state) do
189 {:noreply, %{state | dropped: drop_count + 1}}
192 def handle_info({:send, data, transport, retries}, %{delivered: delivery_count} = state) do
193 case transport.publish_one(data) do
195 {:noreply, %{state | delivered: delivery_count + 1}}
198 enqueue(data, transport, retries)
205 %{queue_table: queue_table, running_jobs: running_jobs} = state
207 maybe_kickoff_timer()
208 running_jobs = maybe_start_job(running_jobs, queue_table)
209 {:noreply, %{state | running_jobs: running_jobs}}
212 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
213 %{running_jobs: running_jobs, queue_table: queue_table} = state
214 running_jobs = :sets.del_element(ref, running_jobs)
215 running_jobs = maybe_start_job(running_jobs, queue_table)
216 {:noreply, %{state | running_jobs: running_jobs}}
219 def handle_info(unknown, state) do
220 Logger.debug("RetryQueue: don't know what to do with #{inspect(unknown)}, ignoring")
224 if Pleroma.Config.get(:env) == :test do
225 defp growth_function(_retries) do
226 _shutit = Pleroma.Config.get([__MODULE__, :initial_timeout])
227 DateTime.to_unix(DateTime.utc_now()) - 1
230 defp growth_function(retries) do
231 round(Pleroma.Config.get([__MODULE__, :initial_timeout]) * :math.pow(retries, 3)) +
232 DateTime.to_unix(DateTime.utc_now())
236 defp maybe_kickoff_timer do
237 GenServer.cast(__MODULE__, :kickoff_timer)