Skip to content

Commit

Permalink
webrtc: handshake more connections in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Nov 13, 2024
1 parent 7268c98 commit 0f586ed
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions p2p/transport/webrtc/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ func (c *connMultiaddrs) LocalMultiaddr() ma.Multiaddr { return c.local }
func (c *connMultiaddrs) RemoteMultiaddr() ma.Multiaddr { return c.remote }

const (
candidateSetupTimeout = 20 * time.Second
DefaultMaxInFlightConnections = 10
candidateSetupTimeout = 10 * time.Second
// This is higher than other transports(64) as there's no way to detect a peer that has gone away after
// sending the initial connection request message(STUN Binding request). Such peers take up a goroutine
// till connection timeout. As the number of handshakes in parallel is still guarded by the resource
// manager, this higher number is okay.
DefaultMaxInFlightConnections = 128
)

type listener struct {
Expand Down Expand Up @@ -325,26 +329,20 @@ 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
// * receives an error when the state changes to Failed or Closed or Disconnected
func addOnConnectionStateChangeCallback(pc *webrtc.PeerConnection) <-chan error {
errC := make(chan error, 1)
var once sync.Once
pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
switch pc.ConnectionState() {
case webrtc.PeerConnectionStateConnected:
once.Do(func() { close(errC) })
case webrtc.PeerConnectionStateFailed:
// TODO: confirm when exactly PeerConnectionStateClosed happens.
case webrtc.PeerConnectionStateFailed, webrtc.PeerConnectionStateDisconnected, webrtc.PeerConnectionStateClosed:
once.Do(func() {
errC <- errors.New("peerconnection failed")
close(errC)
})
case webrtc.PeerConnectionStateDisconnected:
// the connection can move to a disconnected state and back to a connected state without ICE renegotiation.
// This could happen when underlying UDP packets are lost, and therefore the connection moves to the disconnected state.
// 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")
}
})
return errC
Expand Down

0 comments on commit 0f586ed

Please sign in to comment.