Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
webrtc: don't close stream, but limit to 512 streams per direction
Browse files Browse the repository at this point in the history
marten-seemann committed Sep 12, 2023
1 parent 2f62254 commit 603793d
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion p2p/test/transport/transport_test.go
Original file line number Diff line number Diff line change
@@ -247,7 +247,7 @@ func TestLotsOfDataManyStreams(t *testing.T) {
// 64k buffer
const bufSize = 64 << 10
sendBuf := [bufSize]byte{}
const totalStreams = 512
const totalStreams = 500
const parallel = 8
// Total sends are > 20MiB
require.Greater(t, len(sendBuf)*totalStreams, 20<<20)
14 changes: 14 additions & 0 deletions p2p/transport/webrtc/connection.go
Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@ var _ tpt.CapableConn = &connection{}

const maxAcceptQueueLen = 10

const maxDataChannelID = 1 << 10

type errConnectionTimeout struct{}

var _ net.Error = &errConnectionTimeout{}
@@ -108,6 +110,11 @@ func newConnection(
if c.IsClosed() {
return
}
// Limit the number of streams, since we're not able to actually properly close them.
// See https://github.com/libp2p/specs/issues/575 for details.
if *dc.ID() > maxDataChannelID {
c.Close()
}
dc.OnOpen(func() {
rwc, err := dc.Detach()
if err != nil {
@@ -166,6 +173,13 @@ func (c *connection) OpenStream(ctx context.Context) (network.MuxedStream, error
if id > math.MaxUint16 {
return nil, errors.New("exhausted stream ID space")
}
// Limit the number of streams, since we're not able to actually properly close them.
// See https://github.com/libp2p/specs/issues/575 for details.
if id > maxDataChannelID {
c.Close()
return c.OpenStream(ctx)
}

streamID := uint16(id)
dc, err := c.pc.CreateDataChannel("", &webrtc.DataChannelInit{ID: &streamID})
if err != nil {
4 changes: 3 additions & 1 deletion p2p/transport/webrtc/stream.go
Original file line number Diff line number Diff line change
@@ -192,7 +192,9 @@ func (s *stream) maybeDeclareStreamDone() {
(s.receiveState == receiveStateReset || s.receiveState == receiveStateDataRead) &&
len(s.controlMsgQueue) == 0 {
_ = s.SetReadDeadline(time.Now().Add(-1 * time.Hour)) // pion ignores zero times
_ = s.dataChannel.Close()
// TODO: we should be closing the underlying datachannel, but this resets the stream
// See https://github.com/libp2p/specs/issues/575 for details.
// _ = s.dataChannel.Close()
// TODO: write for the spawned reader to return
s.onDone()
}

0 comments on commit 603793d

Please sign in to comment.