diff --git a/p2p/transport/webrtc/listener.go b/p2p/transport/webrtc/listener.go index cbf9f8fb8b..d4e5412235 100644 --- a/p2p/transport/webrtc/listener.go +++ b/p2p/transport/webrtc/listener.go @@ -318,14 +318,13 @@ func (l *listener) Multiaddr() ma.Multiaddr { } // addOnConnectionStateChangeCallback adds the OnConnectionStateChange to the PeerConnection. -// The channel returned here: -// * is closed when the state changes to Connection -// * receives an error when the state changes to Failed -// * doesn't receive anything (nor is closed) when the state changes to Disconnected +// If the connection establishment errors, an error is written to the channel before closing. +// If the connection establishment is successful, the channel is closed without writing anything. func addOnConnectionStateChangeCallback(pc *webrtc.PeerConnection) <-chan error { errC := make(chan error, 1) var once sync.Once pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { + fmt.Println("connection state: ", state) switch state { case webrtc.PeerConnectionStateConnected: once.Do(func() { close(errC) }) @@ -340,6 +339,13 @@ func addOnConnectionStateChangeCallback(pc *webrtc.PeerConnection) <-chan error // If the connection then receives packets on the connection, it can move back to the connected state. // If no packets are received until the failed timeout is triggered, the connection moves to the failed state. log.Warn("peerconnection disconnected") + case webrtc.PeerConnectionStateClosed: + // ConnectionStateClosed is a terminal state. This happens when the peer closes the PeerConnection before + // connection establishment. + once.Do(func() { + errC <- errors.New("peerconnection closed") + close(errC) + }) } }) return errC diff --git a/p2p/transport/webrtc/transport.go b/p2p/transport/webrtc/transport.go index 20bfdf98eb..2c6197c1f0 100644 --- a/p2p/transport/webrtc/transport.go +++ b/p2p/transport/webrtc/transport.go @@ -78,7 +78,7 @@ const ( const ( DefaultDisconnectedTimeout = 20 * time.Second DefaultFailedTimeout = 30 * time.Second - DefaultKeepaliveTimeout = 15 * time.Second + DefaultKeepaliveTimeout = 5 * time.Second ) type WebRTCTransport struct { diff --git a/p2p/transport/webrtc/transport_test.go b/p2p/transport/webrtc/transport_test.go index e5ef3ca1d0..e0cc5ce6d4 100644 --- a/p2p/transport/webrtc/transport_test.go +++ b/p2p/transport/webrtc/transport_test.go @@ -110,14 +110,16 @@ func TestTransportWebRTC_CanListenSingle(t *testing.T) { done := make(chan struct{}) go func() { - _, err := tr1.Dial(context.Background(), listener.Multiaddr(), listeningPeer) + conn, err := tr1.Dial(context.Background(), listener.Multiaddr(), listeningPeer) assert.NoError(t, err) + t.Cleanup(func() { conn.Close() }) close(done) }() conn, err := listener.Accept() require.NoError(t, err) require.NotNil(t, conn) + defer conn.Close() require.Equal(t, connectingPeer, conn.RemotePeer()) select { @@ -170,12 +172,14 @@ func TestTransportWebRTC_CanListenMultiple(t *testing.T) { defer wg.Done() ctr, _ := getTransport(t) conn, err := ctr.Dial(ctx, listener.Multiaddr(), listeningPeer) + if conn != nil { + t.Cleanup(func() { conn.Close() }) + } select { case <-ctx.Done(): default: assert.NoError(t, err) assert.NotNil(t, conn) - t.Cleanup(func() { conn.Close() }) } }() }