logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

worker.ex (4434B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Gun.ConnectionPool.Worker do
  5. alias Pleroma.Gun
  6. use GenServer, restart: :temporary
  7. defp registry, do: Pleroma.Gun.ConnectionPool
  8. def start_link([key | _] = opts) do
  9. GenServer.start_link(__MODULE__, opts, name: {:via, Registry, {registry(), key}})
  10. end
  11. @impl true
  12. def init([_key, _uri, _opts, _client_pid] = opts) do
  13. {:ok, nil, {:continue, {:connect, opts}}}
  14. end
  15. @impl true
  16. def handle_continue({:connect, [key, uri, opts, client_pid]}, _) do
  17. with {:ok, conn_pid, protocol} <- Gun.Conn.open(uri, opts),
  18. Process.link(conn_pid) do
  19. time = :erlang.monotonic_time(:millisecond)
  20. {_, _} =
  21. Registry.update_value(registry(), key, fn _ ->
  22. {conn_pid, [client_pid], 1, time}
  23. end)
  24. send(client_pid, {:conn_pid, conn_pid})
  25. {:noreply,
  26. %{
  27. key: key,
  28. timer: nil,
  29. client_monitors: %{client_pid => Process.monitor(client_pid)},
  30. protocol: protocol
  31. }, :hibernate}
  32. else
  33. err ->
  34. {:stop, {:shutdown, err}, nil}
  35. end
  36. end
  37. @impl true
  38. def handle_cast({:add_client, client_pid}, state) do
  39. case handle_call(:add_client, {client_pid, nil}, state) do
  40. {:reply, conn_pid, state, :hibernate} ->
  41. send(client_pid, {:conn_pid, conn_pid})
  42. {:noreply, state, :hibernate}
  43. end
  44. end
  45. @impl true
  46. def handle_cast({:remove_client, client_pid}, state) do
  47. case handle_call(:remove_client, {client_pid, nil}, state) do
  48. {:reply, _, state, :hibernate} ->
  49. {:noreply, state, :hibernate}
  50. end
  51. end
  52. @impl true
  53. def handle_call(:add_client, {client_pid, _}, %{key: key, protocol: protocol} = state) do
  54. time = :erlang.monotonic_time(:millisecond)
  55. {{conn_pid, used_by, _, _}, _} =
  56. Registry.update_value(registry(), key, fn {conn_pid, used_by, crf, last_reference} ->
  57. {conn_pid, [client_pid | used_by], crf(time - last_reference, crf), time}
  58. end)
  59. :telemetry.execute(
  60. [:pleroma, :connection_pool, :client, :add],
  61. %{client_pid: client_pid, clients: used_by},
  62. %{key: state.key, protocol: protocol}
  63. )
  64. state =
  65. if state.timer != nil do
  66. Process.cancel_timer(state[:timer])
  67. %{state | timer: nil}
  68. else
  69. state
  70. end
  71. ref = Process.monitor(client_pid)
  72. state = put_in(state.client_monitors[client_pid], ref)
  73. {:reply, conn_pid, state, :hibernate}
  74. end
  75. @impl true
  76. def handle_call(:remove_client, {client_pid, _}, %{key: key} = state) do
  77. {{_conn_pid, used_by, _crf, _last_reference}, _} =
  78. Registry.update_value(registry(), key, fn {conn_pid, used_by, crf, last_reference} ->
  79. {conn_pid, List.delete(used_by, client_pid), crf, last_reference}
  80. end)
  81. {ref, state} = pop_in(state.client_monitors[client_pid])
  82. Process.demonitor(ref, [:flush])
  83. timer =
  84. if used_by == [] do
  85. max_idle = Pleroma.Config.get([:connections_pool, :max_idle_time], 30_000)
  86. Process.send_after(self(), :idle_close, max_idle)
  87. else
  88. nil
  89. end
  90. {:reply, :ok, %{state | timer: timer}, :hibernate}
  91. end
  92. @impl true
  93. def handle_info(:idle_close, state) do
  94. # Gun monitors the owner process, and will close the connection automatically
  95. # when it's terminated
  96. {:stop, :normal, state}
  97. end
  98. @impl true
  99. def handle_info({:gun_up, _pid, _protocol}, state) do
  100. {:noreply, state, :hibernate}
  101. end
  102. # Gracefully shutdown if the connection got closed without any streams left
  103. @impl true
  104. def handle_info({:gun_down, _pid, _protocol, _reason, []}, state) do
  105. {:stop, :normal, state}
  106. end
  107. # Otherwise, wait for retry
  108. @impl true
  109. def handle_info({:gun_down, _pid, _protocol, _reason, _killed_streams}, state) do
  110. {:noreply, state, :hibernate}
  111. end
  112. @impl true
  113. def handle_info({:DOWN, _ref, :process, pid, reason}, state) do
  114. :telemetry.execute(
  115. [:pleroma, :connection_pool, :client, :dead],
  116. %{client_pid: pid, reason: reason},
  117. %{key: state.key}
  118. )
  119. handle_cast({:remove_client, pid}, state)
  120. end
  121. # LRFU policy: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.55.1478
  122. defp crf(time_delta, prev_crf) do
  123. 1 + :math.pow(0.5, 0.0001 * time_delta) * prev_crf
  124. end
  125. end