aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/gun/connection_pool
diff options
context:
space:
mode:
authorrinpatch <rinpatch@sdf.org>2020-07-28 20:49:48 +0300
committerrinpatch <rinpatch@sdf.org>2020-07-28 20:49:48 +0300
commit4ce6179dc7843d99823cf41be86574973b66200f (patch)
tree88fa032c716fc002bd60a5365a0ca6c3bbc45585 /lib/pleroma/gun/connection_pool
parent165ea038bd7a057ce137cc6a7a3deee7c98753de (diff)
downloadpleroma-4ce6179dc7843d99823cf41be86574973b66200f.tar.gz
gun ConnectionPool: replace casts with calls
The slowdown from this is most likely immesurable, however it eliminates possible false positives when tracking dead clients.
Diffstat (limited to 'lib/pleroma/gun/connection_pool')
-rw-r--r--lib/pleroma/gun/connection_pool/worker.ex40
1 files changed, 19 insertions, 21 deletions
diff --git a/lib/pleroma/gun/connection_pool/worker.ex b/lib/pleroma/gun/connection_pool/worker.ex
index f33447cb6..a61892c60 100644
--- a/lib/pleroma/gun/connection_pool/worker.ex
+++ b/lib/pleroma/gun/connection_pool/worker.ex
@@ -36,7 +36,16 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do
end
@impl true
- def handle_cast({:add_client, client_pid, send_pid_back}, %{key: key} = state) do
+ def handle_cast({:add_client, client_pid, send}, state) do
+ case handle_call(:add_client, {client_pid, nil}, state) do
+ {:reply, conn_pid, state, :hibernate} ->
+ if send, do: send(client_pid, {:conn_pid, conn_pid})
+ {:noreply, state, :hibernate}
+ end
+ end
+
+ @impl true
+ def handle_call(:add_client, {client_pid, _}, %{key: key} = state) do
time = :erlang.monotonic_time(:millisecond)
{{conn_pid, _, _, _}, _} =
@@ -44,8 +53,6 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do
{conn_pid, [client_pid | used_by], crf(time - last_reference, crf), time}
end)
- if send_pid_back, do: send(client_pid, {:conn_pid, conn_pid})
-
state =
if state.timer != nil do
Process.cancel_timer(state[:timer])
@@ -57,11 +64,11 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do
ref = Process.monitor(client_pid)
state = put_in(state.client_monitors[client_pid], ref)
- {:noreply, state, :hibernate}
+ {:reply, conn_pid, state, :hibernate}
end
@impl true
- def handle_cast({:remove_client, client_pid}, %{key: key} = state) do
+ def handle_call(:remove_client, {client_pid, _}, %{key: key} = state) do
{{_conn_pid, used_by, _crf, _last_reference}, _} =
Registry.update_value(@registry, key, fn {conn_pid, used_by, crf, last_reference} ->
{conn_pid, List.delete(used_by, client_pid), crf, last_reference}
@@ -78,7 +85,7 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do
nil
end
- {:noreply, %{state | timer: timer}, :hibernate}
+ {:reply, :ok, %{state | timer: timer}, :hibernate}
end
@impl true
@@ -102,22 +109,13 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do
@impl true
def handle_info({:DOWN, _ref, :process, pid, reason}, state) do
- # Sometimes the client is dead before we demonitor it in :remove_client, so the message
- # arrives anyway
-
- case state.client_monitors[pid] do
- nil ->
- {:noreply, state, :hibernate}
+ :telemetry.execute(
+ [:pleroma, :connection_pool, :client_death],
+ %{client_pid: pid, reason: reason},
+ %{key: state.key}
+ )
- _ref ->
- :telemetry.execute(
- [:pleroma, :connection_pool, :client_death],
- %{client_pid: pid, reason: reason},
- %{key: state.key}
- )
-
- handle_cast({:remove_client, pid}, state)
- end
+ handle_cast({:remove_client, pid, false}, state)
end
# LRFU policy: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.55.1478