2020-10-12 11:00:50 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-10-12 11:00:50 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-05-05 16:51:10 -06:00
|
|
|
defmodule Pleroma.Gun.ConnectionPool do
|
|
|
|
@registry __MODULE__
|
|
|
|
|
2020-05-06 14:14:24 -06:00
|
|
|
alias Pleroma.Gun.ConnectionPool.WorkerSupervisor
|
|
|
|
|
|
|
|
def children do
|
|
|
|
[
|
|
|
|
{Registry, keys: :unique, name: @registry},
|
|
|
|
Pleroma.Gun.ConnectionPool.WorkerSupervisor
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2020-08-01 08:59:50 -06:00
|
|
|
@spec get_conn(URI.t(), keyword()) :: {:ok, pid()} | {:error, term()}
|
2020-05-05 16:51:10 -06:00
|
|
|
def get_conn(uri, opts) do
|
2020-05-06 12:41:34 -06:00
|
|
|
key = "#{uri.scheme}:#{uri.host}:#{uri.port}"
|
2020-05-05 16:51:10 -06:00
|
|
|
|
2020-05-06 12:41:34 -06:00
|
|
|
case Registry.lookup(@registry, key) do
|
|
|
|
# The key has already been registered, but connection is not up yet
|
2020-07-15 06:24:47 -06:00
|
|
|
[{worker_pid, nil}] ->
|
2020-07-08 05:01:02 -06:00
|
|
|
get_gun_pid_from_worker(worker_pid, true)
|
2020-05-05 16:51:10 -06:00
|
|
|
|
2020-05-06 12:41:34 -06:00
|
|
|
[{worker_pid, {gun_pid, _used_by, _crf, _last_reference}}] ->
|
2020-07-28 11:49:48 -06:00
|
|
|
GenServer.call(worker_pid, :add_client)
|
2020-05-06 12:41:34 -06:00
|
|
|
{:ok, gun_pid}
|
2020-05-05 16:51:10 -06:00
|
|
|
|
2020-05-06 12:41:34 -06:00
|
|
|
[] ->
|
2020-05-06 14:14:24 -06: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 06:24:47 -06:00
|
|
|
case WorkerSupervisor.start_worker([key, uri, opts, self()]) do
|
2020-07-08 05:01:02 -06:00
|
|
|
{:ok, worker_pid} ->
|
|
|
|
get_gun_pid_from_worker(worker_pid, false)
|
2020-05-05 16:51:10 -06:00
|
|
|
|
2020-07-15 06:24:47 -06:00
|
|
|
{:error, {:already_started, worker_pid}} ->
|
2020-07-08 05:01:02 -06:00
|
|
|
get_gun_pid_from_worker(worker_pid, true)
|
2020-05-05 16:51:10 -06:00
|
|
|
|
2020-05-06 14:14:24 -06:00
|
|
|
err ->
|
|
|
|
err
|
|
|
|
end
|
2020-05-05 16:51:10 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-08 05:01:02 -06:00
|
|
|
defp get_gun_pid_from_worker(worker_pid, register) do
|
2020-05-05 16:51:10 -06:00
|
|
|
# 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)
|
2020-07-28 14:48:41 -06:00
|
|
|
if register, do: GenServer.cast(worker_pid, {:add_client, self()})
|
2020-05-05 16:51:10 -06:00
|
|
|
|
|
|
|
receive do
|
2020-07-08 05:01:02 -06:00
|
|
|
{:conn_pid, pid} ->
|
|
|
|
Process.demonitor(ref)
|
|
|
|
{:ok, pid}
|
|
|
|
|
|
|
|
{:DOWN, ^ref, :process, ^worker_pid, reason} ->
|
2020-07-15 06:58:08 -06:00
|
|
|
case reason do
|
2020-08-01 08:59:50 -06:00
|
|
|
{:shutdown, {:error, _} = error} -> error
|
|
|
|
{:shutdown, error} -> {:error, error}
|
2020-07-15 06:58:08 -06:00
|
|
|
_ -> {:error, reason}
|
|
|
|
end
|
2020-05-05 16:51:10 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-01 08:59:50 -06:00
|
|
|
@spec release_conn(pid()) :: :ok
|
2020-05-05 16:51:10 -06:00
|
|
|
def release_conn(conn_pid) do
|
2020-07-15 06:24:47 -06:00
|
|
|
# :ets.fun2ms(fn {_, {worker_pid, {gun_pid, _, _, _}}} when gun_pid == conn_pid ->
|
|
|
|
# worker_pid end)
|
2020-05-06 12:51:10 -06:00
|
|
|
query_result =
|
2020-05-05 16:51:10 -06:00
|
|
|
Registry.select(@registry, [
|
|
|
|
{{:_, :"$1", {:"$2", :_, :_, :_}}, [{:==, :"$2", conn_pid}], [:"$1"]}
|
|
|
|
])
|
|
|
|
|
2020-05-06 12:51:10 -06:00
|
|
|
case query_result do
|
|
|
|
[worker_pid] ->
|
2020-07-28 11:49:48 -06:00
|
|
|
GenServer.call(worker_pid, :remove_client)
|
2020-05-06 12:51:10 -06:00
|
|
|
|
|
|
|
[] ->
|
|
|
|
:ok
|
|
|
|
end
|
2020-05-05 16:51:10 -06:00
|
|
|
end
|
|
|
|
end
|