diff --git a/internal/multiplex/datagramBufferedPipe.go b/internal/multiplex/datagramBufferedPipe.go index 0846849..51a8fe7 100644 --- a/internal/multiplex/datagramBufferedPipe.go +++ b/internal/multiplex/datagramBufferedPipe.go @@ -13,7 +13,8 @@ import ( // instead of byte-oriented. The integrity of datagrams written into this buffer is preserved. // it won't get chopped up into individual bytes type datagramBufferedPipe struct { - pLens []int + pLens []int + // lazily allocated buf *bytes.Buffer closed bool rwCond *sync.Cond @@ -114,7 +115,7 @@ func (d *datagramBufferedPipe) Write(f Frame) (toBeClosed bool, err error) { if d.closed { return true, io.ErrClosedPipe } - if d.buf.Len() <= BUF_SIZE_LIMIT { + if d.buf.Len() <= recvBufferSizeLimit { // if d.buf gets too large, write() will panic. We don't want this to happen break } diff --git a/internal/multiplex/recvBuffer.go b/internal/multiplex/recvBuffer.go index d951682..0797daf 100644 --- a/internal/multiplex/recvBuffer.go +++ b/internal/multiplex/recvBuffer.go @@ -21,3 +21,8 @@ type recvBuffer interface { // has been written for a while. After that duration it should return ErrTimeout SetWriteToTimeout(d time.Duration) } + +// size we want the amount of unread data in buffer to grow before recvBuffer.Write blocks. +// If the buffer grows larger than what the system's memory can offer at the time of recvBuffer.Write, +// a panic will happen. +const recvBufferSizeLimit = defaultSendRecvBufSize << 12 diff --git a/internal/multiplex/streamBufferedPipe.go b/internal/multiplex/streamBufferedPipe.go index 89a58a6..47ee101 100644 --- a/internal/multiplex/streamBufferedPipe.go +++ b/internal/multiplex/streamBufferedPipe.go @@ -9,8 +9,6 @@ import ( "time" ) -const BUF_SIZE_LIMIT = 1 << 20 * 500 - // The point of a streamBufferedPipe is that Read() will block until data is available type streamBufferedPipe struct { // only alloc when on first Read or Write @@ -105,7 +103,7 @@ func (p *streamBufferedPipe) Write(input []byte) (int, error) { if p.closed { return 0, io.ErrClosedPipe } - if p.buf.Len() <= BUF_SIZE_LIMIT { + if p.buf.Len() <= recvBufferSizeLimit { // if p.buf gets too large, write() will panic. We don't want this to happen break }