From cdb8592ac14295c3508b6d9f8ccfbe80ead2d765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=C5=9Aled=C5=BA?= Date: Mon, 21 Oct 2024 09:18:36 +0200 Subject: [PATCH] Move PeerConnection initialization to handle_continue --- .credo.exs | 2 +- lib/ex_webrtc/peer_connection.ex | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.credo.exs b/.credo.exs index a5815995..328eccb2 100644 --- a/.credo.exs +++ b/.credo.exs @@ -89,7 +89,7 @@ # set this value to 0 (zero). # {Credo.Check.Design.TagTODO, [exit_status: 0]}, - {Credo.Check.Design.TagFIXME, []}, + {Credo.Check.Design.TagFIXME, [exit_status: 0]}, # ## Readability Checks diff --git a/lib/ex_webrtc/peer_connection.ex b/lib/ex_webrtc/peer_connection.ex index 065d7661..1178aaf6 100644 --- a/lib/ex_webrtc/peer_connection.ex +++ b/lib/ex_webrtc/peer_connection.ex @@ -479,6 +479,15 @@ defmodule ExWebRTC.PeerConnection do @impl true def init(config) do + # FIXME: Spawning a lot of peer connections simultaneously, often takes a lot of time. + # This does not happen when spawning a single peer connection. + # Moving actual initialization to handle_continue at least does not block + # supervisor/dynamic supervisor under which those peer connections are spawned. + {:ok, nil, {:continue, config}} + end + + @impl true + def handle_continue(config, _state) do {:ok, _} = Registry.register(ExWebRTC.Registry, self(), self()) ice_config = [ @@ -533,7 +542,7 @@ defmodule ExWebRTC.PeerConnection do notify(state.owner, {:connection_state_change, :new}) notify(state.owner, {:signaling_state_change, :stable}) - {:ok, state} + {:noreply, state} end @impl true @@ -1376,6 +1385,12 @@ defmodule ExWebRTC.PeerConnection do {:noreply, state} end + @impl true + def terminate(reason, nil) do + # we exit before finishing handle_continue + Logger.debug("Closing peer connection with reason: #{inspect(reason)}") + end + @impl true def terminate(reason, state) do Logger.debug("Closing peer connection with reason: #{inspect(reason)}")