@@ -479,6 +479,15 @@ defmodule ExWebRTC.PeerConnection do
479479
480480 @ impl true
481481 def init ( config ) do
482+ # FIXME: Spawning a lot of peer connections simultaneously, often takes a lot of time.
483+ # This does not happen when spawning a single peer connection.
484+ # Moving actual initialization to handle_continue at least does not block
485+ # supervisor/dynamic supervisor under which those peer connections are spawned.
486+ { :ok , nil , { :continue , config } }
487+ end
488+
489+ @ impl true
490+ def handle_continue ( config , _state ) do
482491 { :ok , _ } = Registry . register ( ExWebRTC.Registry , self ( ) , self ( ) )
483492
484493 ice_config = [
@@ -533,7 +542,7 @@ defmodule ExWebRTC.PeerConnection do
533542 notify ( state . owner , { :connection_state_change , :new } )
534543 notify ( state . owner , { :signaling_state_change , :stable } )
535544
536- { :ok , state }
545+ { :noreply , state }
537546 end
538547
539548 @ impl true
@@ -1376,6 +1385,12 @@ defmodule ExWebRTC.PeerConnection do
13761385 { :noreply , state }
13771386 end
13781387
1388+ @ impl true
1389+ def terminate ( reason , nil ) do
1390+ # we exit before finishing handle_continue
1391+ Logger . debug ( "Closing peer connection with reason: #{ inspect ( reason ) } " )
1392+ end
1393+
13791394 @ impl true
13801395 def terminate ( reason , state ) do
13811396 Logger . debug ( "Closing peer connection with reason: #{ inspect ( reason ) } " )
0 commit comments