2020-05-06 00:51:10 +02:00
|
|
|
defmodule Pleroma.Gun.ConnectionPool do
|
|
|
|
@registry __MODULE__
|
|
|
|
|
2020-05-06 22:14:24 +02:00
|
|
|
alias Pleroma.Gun.ConnectionPool.WorkerSupervisor
|
|
|
|
|
|
|
|
def children do
|
|
|
|
[
|
|
|
|
{Registry, keys: :unique, name: @registry},
|
|
|
|
Pleroma.Gun.ConnectionPool.WorkerSupervisor
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2020-05-06 00:51:10 +02:00
|
|
|
def get_conn(uri, opts) do
|
2020-05-06 20:41:34 +02:00
|
|
|
key = "#{uri.scheme}:#{uri.host}:#{uri.port}"
|
2020-05-06 00:51:10 +02:00
|
|
|
|
2020-05-06 20:41:34 +02:00
|
|
|
case Registry.lookup(@registry, key) do
|
|
|
|
# The key has already been registered, but connection is not up yet
|
2020-07-15 14:24:47 +02:00
|
|
|
[{worker_pid, nil}] ->
|
2020-05-06 20:41:34 +02:00
|
|
|
get_gun_pid_from_worker(worker_pid)
|
2020-05-06 00:51:10 +02:00
|
|
|
|
2020-05-06 20:41:34 +02:00
|
|
|
[{worker_pid, {gun_pid, _used_by, _crf, _last_reference}}] ->
|
|
|
|
GenServer.cast(worker_pid, {:add_client, self(), false})
|
|
|
|
{:ok, gun_pid}
|
2020-05-06 00:51:10 +02:00
|
|
|
|
2020-05-06 20:41:34 +02:00
|
|
|
[] ->
|
2020-05-06 22:14:24 +02:00
|
|
|
# :gun.set_owner fails in :connected state for whatevever reason,
|
|
|
|
# so we open the connection in the process directly and send it's pid back
|
|
|
|
# We trust gun to handle timeouts by itself
|
2020-07-15 14:24:47 +02:00
|
|
|
case WorkerSupervisor.start_worker([key, uri, opts, self()]) do
|
2020-05-06 22:14:24 +02:00
|
|
|
{:ok, _worker_pid} ->
|
|
|
|
receive do
|
|
|
|
{:conn_pid, pid} -> {:ok, pid}
|
2020-05-06 00:51:10 +02:00
|
|
|
end
|
|
|
|
|
2020-07-15 14:24:47 +02:00
|
|
|
{:error, {:already_started, worker_pid}} ->
|
2020-05-06 22:14:24 +02:00
|
|
|
get_gun_pid_from_worker(worker_pid)
|
2020-05-06 00:51:10 +02:00
|
|
|
|
2020-05-06 22:14:24 +02:00
|
|
|
err ->
|
|
|
|
err
|
|
|
|
end
|
2020-05-06 00:51:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_gun_pid_from_worker(worker_pid) 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})
|
|
|
|
|
|
|
|
receive do
|
|
|
|
{:conn_pid, pid} -> {:ok, pid}
|
|
|
|
{:DOWN, ^ref, :process, ^worker_pid, reason} -> reason
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def release_conn(conn_pid) do
|
2020-07-15 14:24:47 +02:00
|
|
|
# :ets.fun2ms(fn {_, {worker_pid, {gun_pid, _, _, _}}} when gun_pid == conn_pid ->
|
|
|
|
# worker_pid end)
|
2020-05-06 20:51:10 +02:00
|
|
|
query_result =
|
2020-05-06 00:51:10 +02:00
|
|
|
Registry.select(@registry, [
|
|
|
|
{{:_, :"$1", {:"$2", :_, :_, :_}}, [{:==, :"$2", conn_pid}], [:"$1"]}
|
|
|
|
])
|
|
|
|
|
2020-05-06 20:51:10 +02:00
|
|
|
case query_result do
|
|
|
|
[worker_pid] ->
|
|
|
|
GenServer.cast(worker_pid, {:remove_client, self()})
|
|
|
|
|
|
|
|
[] ->
|
|
|
|
:ok
|
|
|
|
end
|
2020-05-06 00:51:10 +02:00
|
|
|
end
|
|
|
|
end
|