refactoring for gun api modules
[akkoma] / lib / pleroma / pool / connections.ex
index 84617815fa2c3580c3d099d6dccd46cc8d79ad84..92179fbfc1e968ee788f5bab0fedadbdcfbd6cfa 100644 (file)
@@ -19,7 +19,7 @@ defmodule Pleroma.Pool.Connections do
 
   defstruct conns: %{}, opts: []
 
-  alias Pleroma.Gun.API
+  alias Pleroma.Gun
 
   @spec start_link({atom(), keyword()}) :: {:ok, pid()}
   def start_link({name, opts}) do
@@ -34,19 +34,18 @@ defmodule Pleroma.Pool.Connections do
   def checkin(url, name) when is_binary(url), do: checkin(URI.parse(url), name)
 
   def checkin(%URI{} = uri, name) do
-    timeout = Config.get([:connections_pool, :receive_connection_timeout], 250)
+    timeout = Config.get([:connections_pool, :checkin_timeout], 250)
 
-    GenServer.call(
-      name,
-      {:checkin, uri},
-      timeout
-    )
+    GenServer.call(name, {:checkin, uri}, timeout)
   end
 
   @spec alive?(atom()) :: boolean()
   def alive?(name) do
-    pid = Process.whereis(name)
-    if pid, do: Process.alive?(pid), else: false
+    if pid = Process.whereis(name) do
+      Process.alive?(pid)
+    else
+      false
+    end
   end
 
   @spec get_state(atom()) :: t()
@@ -131,19 +130,20 @@ defmodule Pleroma.Pool.Connections do
       %{conn: conn, gun_state: :up} = current_conn ->
         Logger.debug("reusing conn #{key}")
 
-        with time <- :os.system_time(:second),
-             last_reference <- time - current_conn.last_reference,
-             current_crf <- crf(last_reference, 100, current_conn.crf),
-             state <-
-               put_in(state.conns[key], %{
-                 current_conn
-                 | last_reference: time,
-                   crf: current_crf,
-                   conn_state: :active,
-                   used_by: [from | current_conn.used_by]
-               }) do
-          {:reply, conn, state}
-        end
+        time = :os.system_time(:second)
+        last_reference = time - current_conn.last_reference
+        current_crf = crf(last_reference, 100, current_conn.crf)
+
+        state =
+          put_in(state.conns[key], %{
+            current_conn
+            | last_reference: time,
+              crf: current_crf,
+              conn_state: :active,
+              used_by: [from | current_conn.used_by]
+          })
+
+        {:reply, conn, state}
 
       %{gun_state: :down} ->
         {:reply, nil, state}
@@ -184,6 +184,7 @@ defmodule Pleroma.Pool.Connections do
         time = :os.system_time(:second)
         last_reference = time - conn.last_reference
         current_crf = crf(last_reference, 100, conn.crf)
+
         put_in(state.conns[key], %{
           conn
           | gun_state: :up,
@@ -208,7 +209,7 @@ defmodule Pleroma.Pool.Connections do
         nil ->
           Logger.debug(":gun_up message for conn which is not found in state")
 
-          :ok = API.close(conn_pid)
+          :ok = Gun.close(conn_pid)
 
           state
       end
@@ -218,14 +219,14 @@ defmodule Pleroma.Pool.Connections do
 
   @impl true
   def handle_info({:gun_down, conn_pid, _protocol, _reason, _killed}, state) do
-    retries = Config.get([:connections_pool, :retry], 0)
+    retries = Config.get([:connections_pool, :retry], 1)
     # we can't get info on this pid, because pid is dead
     state =
       with {key, conn} <- find_conn(state.conns, conn_pid),
            {true, key} <- {Process.alive?(conn_pid), key} do
         if conn.retries == retries do
           Logger.debug("closing conn if retries is eq  #{inspect(conn_pid)}")
-          :ok = API.close(conn.conn)
+          :ok = Gun.close(conn.conn)
 
           put_in(
             state.conns,
@@ -251,7 +252,7 @@ defmodule Pleroma.Pool.Connections do
         nil ->
           Logger.debug(":gun_down message for conn which is not found in state")
 
-          :ok = API.close(conn_pid)
+          :ok = Gun.close(conn_pid)
 
           state
       end
@@ -286,7 +287,7 @@ defmodule Pleroma.Pool.Connections do
   defp compose_key_gun_info(pid) do
     try do
       # sometimes :gun.info can raise MatchError, which lead to pool terminate
-      %{origin_host: origin_host, origin_scheme: scheme, origin_port: port} = API.info(pid)
+      %{origin_host: origin_host, origin_scheme: scheme, origin_port: port} = Gun.info(pid)
 
       host =
         case :inet.ntoa(origin_host) do