Ensure connections error get known by the caller
authorhref <href@random.sh>
Wed, 8 Jul 2020 11:01:02 +0000 (13:01 +0200)
committerrinpatch <rinpatch@sdf.org>
Wed, 15 Jul 2020 12:26:35 +0000 (15:26 +0300)
lib/pleroma/gun/connection_pool.ex
lib/pleroma/gun/connection_pool/worker.ex
lib/pleroma/http/adapter_helper/gun.ex

index e951872fea422da0df3b16338e69926aa3ed0d8f..d3eead7d8be841b0dc15e9640404b022bdfe1f92 100644 (file)
@@ -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,21 @@ 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} ->
+        {:error, reason}
     end
   end
 
index 6ee622fb047ec985b8d96613cd84380ea62fed56..16a508ad96b32277e3834ddec46a0ec7092a4769 100644 (file)
@@ -30,7 +30,8 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do
        %{key: key, timer: nil, client_monitors: %{client_pid => Process.monitor(client_pid)}},
        :hibernate}
     else
-      err -> {:stop, err}
+      err ->
+        {:stop, err, nil}
     end
   end
 
index b8c4cc59c9b738bebe5008cce448ac8d7e98042d..74677ddb54852031b978c2cc78d86b6471ca3812 100644 (file)
@@ -14,7 +14,7 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
     connect_timeout: 5_000,
     domain_lookup_timeout: 5_000,
     tls_handshake_timeout: 5_000,
-    retry: 1,
+    retry: 0,
     retry_timeout: 1000,
     await_up_timeout: 5_000
   ]