X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fgun%2Fconnection_pool.ex;h=8b41a668c0268c504ec55ddc05bbb7545aedcffe;hb=6107440ea0da3a9e59576a86a9dab50acd83936e;hp=e951872fea422da0df3b16338e69926aa3ed0d8f;hpb=0ffde499b8a8f31c82183253bdd692c75733ca2f;p=akkoma diff --git a/lib/pleroma/gun/connection_pool.ex b/lib/pleroma/gun/connection_pool.ex index e951872fe..8b41a668c 100644 --- a/lib/pleroma/gun/connection_pool.ex +++ b/lib/pleroma/gun/connection_pool.ex @@ -16,7 +16,7 @@ defmodule Pleroma.Gun.ConnectionPool do case Registry.lookup(@registry, key) do # The key has already been registered, but connection is not up yet [{worker_pid, nil}] -> - get_gun_pid_from_worker(worker_pid) + get_gun_pid_from_worker(worker_pid, true) [{worker_pid, {gun_pid, _used_by, _crf, _last_reference}}] -> GenServer.cast(worker_pid, {:add_client, self(), false}) @@ -27,13 +27,11 @@ defmodule Pleroma.Gun.ConnectionPool do # so we open the connection in the process directly and send it's pid back # We trust gun to handle timeouts by itself case WorkerSupervisor.start_worker([key, uri, opts, self()]) do - {:ok, _worker_pid} -> - receive do - {:conn_pid, pid} -> {:ok, pid} - end + {:ok, worker_pid} -> + get_gun_pid_from_worker(worker_pid, false) {:error, {:already_started, worker_pid}} -> - get_gun_pid_from_worker(worker_pid) + get_gun_pid_from_worker(worker_pid, true) err -> err @@ -41,17 +39,24 @@ defmodule Pleroma.Gun.ConnectionPool do end end - defp get_gun_pid_from_worker(worker_pid) do + defp get_gun_pid_from_worker(worker_pid, register) do # GenServer.call will block the process for timeout length if # the server crashes on startup (which will happen if gun fails to connect) # so instead we use cast + monitor ref = Process.monitor(worker_pid) - GenServer.cast(worker_pid, {:add_client, self(), true}) + if register, do: GenServer.cast(worker_pid, {:add_client, self(), true}) receive do - {:conn_pid, pid} -> {:ok, pid} - {:DOWN, ^ref, :process, ^worker_pid, reason} -> reason + {:conn_pid, pid} -> + Process.demonitor(ref) + {:ok, pid} + + {:DOWN, ^ref, :process, ^worker_pid, reason} -> + case reason do + {:shutdown, error} -> error + _ -> {:error, reason} + end end end