Skip to content

Commit

Permalink
webrtc: handshake more connections in parallel (#3040)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt authored Nov 18, 2024
1 parent 7268c98 commit dc97b73
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 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,23 @@ 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:
// PeerConnectionStateFailed happens when we fail to negotiate the connection.
// PeerConnectionStateDisconnected happens when we disconnect immediately after connecting.
// PeerConnectionStateClosed happens when we close the peer connection locally, not when remote closes. We don't need
// to error in this case, but it's a no-op, so it doesn't hurt.
case webrtc.PeerConnectionStateFailed, webrtc.PeerConnectionStateClosed, webrtc.PeerConnectionStateDisconnected:
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 dc97b73

Please sign in to comment.