Skip to content

Commit

Permalink
fix: create RTCPeerConnection after dialing remote peer
Browse files Browse the repository at this point in the history
Chrome limits how many RTCPeerConnections a given tab can instantiated
during it's lifetime - https://issues.chromium.org/issues/41378764

To delay hitting this limit, only create the dial-end RTCPeerConnection
once a relayed connection has successfully been opened to the dial
target, this prevents needlessly creating RTCPeerConnections when the
dial fails before they are actually used.

Fixes #2591
  • Loading branch information
achingbrain committed Jun 17, 2024
1 parent 94cac11 commit ee395a5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CodeError } from '@libp2p/interface'
import { peerIdFromString } from '@libp2p/peer-id'
import { pbStream } from 'it-protobuf-stream'
import { type RTCPeerConnection, RTCSessionDescription } from '../webrtc/index.js'
import { RTCPeerConnection, RTCSessionDescription } from '../webrtc/index.js'
import { Message } from './pb/message.js'
import { SIGNALING_PROTO_ID, splitAddr, type WebRTCTransportMetrics } from './transport.js'
import { readCandidatesUntilConnected } from './util.js'
Expand All @@ -17,7 +17,7 @@ export interface IncomingStreamOpts extends IncomingStreamData {
}

export interface ConnectOptions extends LoggerOptions {
peerConnection: RTCPeerConnection
rtcConfiguration?: RTCConfiguration
multiaddr: Multiaddr
connectionManager: ConnectionManager
transportManager: TransportManager
Expand All @@ -26,7 +26,7 @@ export interface ConnectOptions extends LoggerOptions {
metrics?: WebRTCTransportMetrics
}

export async function initiateConnection ({ peerConnection, signal, metrics, multiaddr: ma, connectionManager, transportManager, log }: ConnectOptions): Promise<{ remoteAddress: Multiaddr }> {
export async function initiateConnection ({ rtcConfiguration, signal, metrics, multiaddr: ma, connectionManager, transportManager, log }: ConnectOptions): Promise<{ remoteAddress: Multiaddr, peerConnection: RTCPeerConnection }> {
const { baseAddr } = splitAddr(ma)

metrics?.dialerEvents.increment({ open: true })
Expand Down Expand Up @@ -64,6 +64,7 @@ export async function initiateConnection ({ peerConnection, signal, metrics, mul
})

const messageStream = pbStream(stream).pb(Message)
const peerConnection = new RTCPeerConnection(rtcConfiguration)

try {
// we create the channel so that the RTCPeerConnection has a component for
Expand Down Expand Up @@ -150,7 +151,8 @@ export async function initiateConnection ({ peerConnection, signal, metrics, mul
log.trace('initiator connected to remote address %s', ma)

return {
remoteAddress: ma
remoteAddress: ma,
peerConnection
}
} catch (err: any) {
peerConnection.close()
Expand Down
14 changes: 6 additions & 8 deletions packages/transport-webrtc/src/private-to-private/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,7 @@ export class WebRTCTransport implements Transport, Startable {
async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
this.log.trace('dialing address: %a', ma)

const peerConnection = new RTCPeerConnection(this.init.rtcConfiguration)
const muxerFactory = new DataChannelMuxerFactory(this.components, {
peerConnection,
dataChannelOptions: this.init.dataChannel
})

const { remoteAddress } = await initiateConnection({
peerConnection,
const { remoteAddress, peerConnection } = await initiateConnection({
multiaddr: ma,
dataChannelOptions: this.init.dataChannel,
signal: options.signal,
Expand All @@ -156,6 +149,11 @@ export class WebRTCTransport implements Transport, Startable {
metrics: this.metrics?.dialerEvents
})

const muxerFactory = new DataChannelMuxerFactory(this.components, {
peerConnection,
dataChannelOptions: this.init.dataChannel
})

const connection = await options.upgrader.upgradeOutbound(webRTCConn, {
skipProtection: true,
skipEncryption: true,
Expand Down

0 comments on commit ee395a5

Please sign in to comment.