@@ -15,22 +15,9 @@ import (
1515)
1616
1717const (
18- // maxMessageSize is the maximum message size of the Protobuf message we send / receive.
19- maxMessageSize = 16384
20- // maxSendBuffer is the maximum data we enqueue on the underlying data channel for writes.
21- // The underlying SCTP layer has an unbounded buffer for writes. We limit the amount enqueued
22- // per stream is limited to avoid a single stream monopolizing the entire connection.
23- maxSendBuffer = 2 * maxMessageSize
24- // sendBufferLowThreshold is the threshold below which we write more data on the underlying
25- // data channel. We want a notification as soon as we can write 1 full sized message.
26- sendBufferLowThreshold = maxSendBuffer - maxMessageSize
27- // maxTotalControlMessagesSize is the maximum total size of all control messages we will
28- // write on this stream.
29- // 4 control messages of size 10 bytes + 10 bytes buffer. This number doesn't need to be
30- // exact. In the worst case, we enqueue these many bytes more in the webrtc peer connection
31- // send queue.
32- maxTotalControlMessagesSize = 50
33-
18+ // maxSendMessageSize is the maximum message size of the Protobuf message we send / receive.
19+ // NOTE: Change `varintOverhead` if you change this.
20+ maxSendMessageSize = 16384
3421 // Proto overhead assumption is 5 bytes
3522 protoOverhead = 5
3623 // Varint overhead is assumed to be 2 bytes. This is safe since
@@ -40,9 +27,20 @@ const (
4027 // is less than or equal to 2 ^ 14, the varint will not be more than
4128 // 2 bytes in length.
4229 varintOverhead = 2
30+
31+ // maxTotalControlMessagesSize is the maximum total size of all control messages we will
32+ // write on this stream.
33+ // 4 control messages of size 10 bytes + 10 bytes buffer. This number doesn't need to be
34+ // exact. In the worst case, we enqueue these many bytes more in the webrtc peer connection
35+ // send queue.
36+ maxTotalControlMessagesSize = 50
37+
4338 // maxFINACKWait is the maximum amount of time a stream will wait to read
4439 // FIN_ACK before closing the data channel
4540 maxFINACKWait = 10 * time .Second
41+
42+ // maxReceiveMessageSize is the maximum message size of the Protobuf message we receive.
43+ maxReceiveMessageSize = 256 << 10 + 1 << 10 // 1kB buffer
4644)
4745
4846type receiveState uint8
@@ -79,11 +77,12 @@ type stream struct {
7977 nextMessage * pb.Message
8078 receiveState receiveState
8179
82- writer pbio.Writer // concurrent writes prevented by mx
83- writeStateChanged chan struct {}
84- sendState sendState
85- writeDeadline time.Time
86- writeError error
80+ writer pbio.Writer // concurrent writes prevented by mx
81+ writeStateChanged chan struct {}
82+ sendState sendState
83+ writeDeadline time.Time
84+ writeError error
85+ maxSendMessageSize int
8786
8887 controlMessageReaderOnce sync.Once
8988 // controlMessageReaderEndTime is the end time for reading FIN_ACK from the control
@@ -105,20 +104,21 @@ var _ network.MuxedStream = &stream{}
105104func newStream (
106105 channel * webrtc.DataChannel ,
107106 rwc datachannel.ReadWriteCloser ,
107+ maxSendMessageSize int ,
108108 onDone func (),
109109) * stream {
110110 s := & stream {
111- reader : pbio .NewDelimitedReader (rwc , maxMessageSize ),
112- writer : pbio .NewDelimitedWriter (rwc ),
113- writeStateChanged : make (chan struct {}, 1 ),
114- id : * channel .ID (),
115- dataChannel : rwc .(* datachannel.DataChannel ),
116- onDone : onDone ,
111+ reader : pbio .NewDelimitedReader (rwc , maxReceiveMessageSize ),
112+ writer : pbio .NewDelimitedWriter (rwc ),
113+ writeStateChanged : make (chan struct {}, 1 ),
114+ id : * channel .ID (),
115+ dataChannel : rwc .(* datachannel.DataChannel ),
116+ onDone : onDone ,
117+ maxSendMessageSize : maxSendMessageSize ,
117118 }
118- s .dataChannel .SetBufferedAmountLowThreshold (sendBufferLowThreshold )
119+ s .dataChannel .SetBufferedAmountLowThreshold (uint64 ( s . sendBufferLowThreshold ()) )
119120 s .dataChannel .OnBufferedAmountLow (func () {
120121 s .notifyWriteStateChanged ()
121-
122122 })
123123 return s
124124}
0 commit comments