ping.ex 905 Bytes
Newer Older
Steven Fuchs's avatar
Steven Fuchs committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
defmodule Pleroma.Web.Streamer.Ping do
  use GenServer
  require Logger

  alias Pleroma.Web.Streamer.State
  alias Pleroma.Web.Streamer.StreamerSocket

  @keepalive_interval :timer.seconds(30)

  def start_link(opts) do
    ping_interval = Keyword.get(opts, :ping_interval, @keepalive_interval)
    GenServer.start_link(__MODULE__, %{ping_interval: ping_interval}, name: __MODULE__)
  end

  def init(%{ping_interval: ping_interval} = args) do
    Process.send_after(self(), :ping, ping_interval)
    {:ok, args}
  end

  def handle_info(:ping, %{ping_interval: ping_interval} = state) do
    State.get_sockets()
    |> Map.values()
    |> List.flatten()
    |> Enum.each(fn %StreamerSocket{transport_pid: transport_pid} ->
      Logger.debug("Sending keepalive ping")
      send(transport_pid, {:text, ""})
    end)

    Process.send_after(self(), :ping, ping_interval)

    {:noreply, state}
  end
end