webrtc icon indicating copy to clipboard operation
webrtc copied to clipboard

PeerConnection should drop reference to datachannel when it's closed

Open sukunrt opened this issue 2 years ago • 0 comments

Your environment.

  • Version: 3.x

What did you do?

libp2p is a peer to peer networking library used in IPFS among other things. We are using webrtc in go-libp2p to provide connectivity to nodes running in the browser. For our use, we create a peer connection and open lots of data channels over the life time of the connection.

Currently the reference to datachannels is not dropped when a datachannel is closed. PeerConnection.sctpTransport.dataChannels always has a reference to the existing data channels which has a reference to the sctp stream. Over the life time of the connection this leads to a lot of memory usage.

What did you expect?

I expected the memory used by the datachannel to be garbage collected.

Proposed fix

Datachannel has an OnClose handler. This handler should remove the datachannel from the sctpTransport To preserve existing behaviour we should:

  1. Only keep the reference to the ID that was used on the datachannel
  2. Create a DataChannelStats object for the stats on the datachannel to preserve collectStats behaviour.

sukunrt avatar Feb 09 '24 07:02 sukunrt