1 defmodule Pleroma.Gun.ConnectionPool.Worker do
3 use GenServer, restart: :temporary
5 @registry Pleroma.Gun.ConnectionPool
7 def start_link([key | _] = opts) do
8 GenServer.start_link(__MODULE__, opts, name: {:via, Registry, {@registry, key}})
12 def init([_key, _uri, _opts, _client_pid] = opts) do
13 {:ok, nil, {:continue, {:connect, opts}}}
17 def handle_continue({:connect, [key, uri, opts, client_pid]}, _) do
18 with {:ok, conn_pid, protocol} <- Gun.Conn.open(uri, opts),
19 Process.link(conn_pid) do
20 time = :erlang.monotonic_time(:millisecond)
23 Registry.update_value(@registry, key, fn _ ->
24 {conn_pid, [client_pid], 1, time}
27 send(client_pid, {:conn_pid, conn_pid})
33 client_monitors: %{client_pid => Process.monitor(client_pid)},
38 {:stop, {:shutdown, err}, nil}
43 def handle_cast({:add_client, client_pid}, state) do
44 case handle_call(:add_client, {client_pid, nil}, state) do
45 {:reply, conn_pid, state, :hibernate} ->
46 send(client_pid, {:conn_pid, conn_pid})
47 {:noreply, state, :hibernate}
52 def handle_cast({:remove_client, client_pid}, state) do
53 case handle_call(:remove_client, {client_pid, nil}, state) do
54 {:reply, _, state, :hibernate} ->
55 {:noreply, state, :hibernate}
60 def handle_call(:add_client, {client_pid, _}, %{key: key, protocol: protocol} = state) do
61 time = :erlang.monotonic_time(:millisecond)
63 {{conn_pid, used_by, _, _}, _} =
64 Registry.update_value(@registry, key, fn {conn_pid, used_by, crf, last_reference} ->
65 {conn_pid, [client_pid | used_by], crf(time - last_reference, crf), time}
69 [:pleroma, :connection_pool, :client, :add],
70 %{client_pid: client_pid, clients: used_by},
71 %{key: state.key, protocol: protocol}
75 if state.timer != nil do
76 Process.cancel_timer(state[:timer])
82 ref = Process.monitor(client_pid)
84 state = put_in(state.client_monitors[client_pid], ref)
85 {:reply, conn_pid, state, :hibernate}
89 def handle_call(:remove_client, {client_pid, _}, %{key: key} = state) do
90 {{_conn_pid, used_by, _crf, _last_reference}, _} =
91 Registry.update_value(@registry, key, fn {conn_pid, used_by, crf, last_reference} ->
92 {conn_pid, List.delete(used_by, client_pid), crf, last_reference}
95 {ref, state} = pop_in(state.client_monitors[client_pid])
97 Process.demonitor(ref, [:flush])
101 max_idle = Pleroma.Config.get([:connections_pool, :max_idle_time], 30_000)
102 Process.send_after(self(), :idle_close, max_idle)
107 {:reply, :ok, %{state | timer: timer}, :hibernate}
111 def handle_info(:idle_close, state) do
112 # Gun monitors the owner process, and will close the connection automatically
113 # when it's terminated
114 {:stop, :normal, state}
118 def handle_info({:gun_up, _pid, _protocol}, state) do
119 {:noreply, state, :hibernate}
122 # Gracefully shutdown if the connection got closed without any streams left
124 def handle_info({:gun_down, _pid, _protocol, _reason, []}, state) do
125 {:stop, :normal, state}
128 # Otherwise, wait for retry
130 def handle_info({:gun_down, _pid, _protocol, _reason, _killed_streams}, state) do
131 {:noreply, state, :hibernate}
135 def handle_info({:DOWN, _ref, :process, pid, reason}, state) do
137 [:pleroma, :connection_pool, :client, :dead],
138 %{client_pid: pid, reason: reason},
142 handle_cast({:remove_client, pid}, state)
145 # LRFU policy: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.55.1478
146 defp crf(time_delta, prev_crf) do
147 1 + :math.pow(0.5, 0.0001 * time_delta) * prev_crf