using atom keys in search params
[akkoma] / lib / pleroma / gun / connection_pool / worker.ex
index 418cb18c19642f36d35cae0d9d284aa2436d5ed1..f33447cb6352c137f88e2b15757c1145bd9445b0 100644 (file)
@@ -9,10 +9,15 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do
   end
 
   @impl true
-  def init([key, uri, opts, client_pid]) do
+  def init([_key, _uri, _opts, _client_pid] = opts) do
+    {:ok, nil, {:continue, {:connect, opts}}}
+  end
+
+  @impl true
+  def handle_continue({:connect, [key, uri, opts, client_pid]}, _) do
     with {:ok, conn_pid} <- Gun.Conn.open(uri, opts),
          Process.link(conn_pid) do
-      time = :erlang.monotonic_time()
+      time = :erlang.monotonic_time(:millisecond)
 
       {_, _} =
         Registry.update_value(@registry, key, fn _ ->
@@ -21,17 +26,18 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do
 
       send(client_pid, {:conn_pid, conn_pid})
 
-      {:ok,
+      {:noreply,
        %{key: key, timer: nil, client_monitors: %{client_pid => Process.monitor(client_pid)}},
        :hibernate}
     else
-      err -> {:stop, err}
+      err ->
+        {:stop, {:shutdown, err}, nil}
     end
   end
 
   @impl true
   def handle_cast({:add_client, client_pid, send_pid_back}, %{key: key} = state) do
-    time = :erlang.monotonic_time()
+    time = :erlang.monotonic_time(:millisecond)
 
     {{conn_pid, _, _, _}, _} =
       Registry.update_value(@registry, key, fn {conn_pid, used_by, crf, last_reference} ->
@@ -116,6 +122,6 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do
 
   # LRFU policy: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.55.1478
   defp crf(time_delta, prev_crf) do
-    1 + :math.pow(0.5, time_delta / 100) * prev_crf
+    1 + :math.pow(0.5, 0.0001 * time_delta) * prev_crf
   end
 end