1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Gun.ConnectionPool.Worker do
7 use GenServer, restart: :temporary
9 defp registry, do: Pleroma.Gun.ConnectionPool
11 def start_link([key | _] = opts) do
12 GenServer.start_link(__MODULE__, opts, name: {:via, Registry, {registry(), key}})
16 def init([_key, _uri, _opts, _client_pid] = opts) do
17 {:ok, nil, {:continue, {:connect, opts}}}
21 def handle_continue({:connect, [key, uri, opts, client_pid]}, _) do
22 with {:ok, conn_pid, protocol} <- Gun.Conn.open(uri, opts),
23 Process.link(conn_pid) do
24 time = :erlang.monotonic_time(:millisecond)
27 Registry.update_value(registry(), key, fn _ ->
28 {conn_pid, [client_pid], 1, time}
31 send(client_pid, {:conn_pid, conn_pid})
37 client_monitors: %{client_pid => Process.monitor(client_pid)},
42 {:stop, {:shutdown, err}, nil}
47 def handle_cast({:add_client, client_pid}, state) do
48 case handle_call(:add_client, {client_pid, nil}, state) do
49 {:reply, conn_pid, state, :hibernate} ->
50 send(client_pid, {:conn_pid, conn_pid})
51 {:noreply, state, :hibernate}
56 def handle_cast({:remove_client, client_pid}, state) do
57 case handle_call(:remove_client, {client_pid, nil}, state) do
58 {:reply, _, state, :hibernate} ->
59 {:noreply, state, :hibernate}
64 def handle_call(:add_client, {client_pid, _}, %{key: key, protocol: protocol} = state) do
65 time = :erlang.monotonic_time(:millisecond)
67 {{conn_pid, used_by, _, _}, _} =
68 Registry.update_value(registry(), key, fn {conn_pid, used_by, crf, last_reference} ->
69 {conn_pid, [client_pid | used_by], crf(time - last_reference, crf), time}
73 [:pleroma, :connection_pool, :client, :add],
74 %{client_pid: client_pid, clients: used_by},
75 %{key: state.key, protocol: protocol}
79 if state.timer != nil do
80 Process.cancel_timer(state[:timer])
86 ref = Process.monitor(client_pid)
88 state = put_in(state.client_monitors[client_pid], ref)
89 {:reply, conn_pid, state, :hibernate}
93 def handle_call(:remove_client, {client_pid, _}, %{key: key} = state) do
94 {{_conn_pid, used_by, _crf, _last_reference}, _} =
95 Registry.update_value(registry(), key, fn {conn_pid, used_by, crf, last_reference} ->
96 {conn_pid, List.delete(used_by, client_pid), crf, last_reference}
99 {ref, state} = pop_in(state.client_monitors[client_pid])
101 Process.demonitor(ref, [:flush])
105 max_idle = Pleroma.Config.get([:connections_pool, :max_idle_time], 30_000)
106 Process.send_after(self(), :idle_close, max_idle)
111 {:reply, :ok, %{state | timer: timer}, :hibernate}
115 def handle_info(:idle_close, state) do
116 # Gun monitors the owner process, and will close the connection automatically
117 # when it's terminated
118 {:stop, :normal, state}
122 def handle_info({:gun_up, _pid, _protocol}, state) do
123 {:noreply, state, :hibernate}
126 # Gracefully shutdown if the connection got closed without any streams left
128 def handle_info({:gun_down, _pid, _protocol, _reason, []}, state) do
129 {:stop, :normal, state}
132 # Otherwise, wait for retry
134 def handle_info({:gun_down, _pid, _protocol, _reason, _killed_streams}, state) do
135 {:noreply, state, :hibernate}
139 def handle_info({:DOWN, _ref, :process, pid, reason}, state) do
141 [:pleroma, :connection_pool, :client, :dead],
142 %{client_pid: pid, reason: reason},
146 handle_cast({:remove_client, pid}, state)
149 # LRFU policy: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.55.1478
150 defp crf(time_delta, prev_crf) do
151 1 + :math.pow(0.5, 0.0001 * time_delta) * prev_crf