Reduce recvBuffer's implementation's buffer size limits

This commit is contained in:
Andy Wang 2020-10-18 14:56:58 +01:00
parent c7c3f7706d
commit acdb0d35fa
3 changed files with 9 additions and 5 deletions

View File

@ -14,6 +14,7 @@ import (
// it won't get chopped up into individual bytes // it won't get chopped up into individual bytes
type datagramBufferedPipe struct { type datagramBufferedPipe struct {
pLens []int pLens []int
// lazily allocated
buf *bytes.Buffer buf *bytes.Buffer
closed bool closed bool
rwCond *sync.Cond rwCond *sync.Cond
@ -114,7 +115,7 @@ func (d *datagramBufferedPipe) Write(f Frame) (toBeClosed bool, err error) {
if d.closed { if d.closed {
return true, io.ErrClosedPipe 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 // if d.buf gets too large, write() will panic. We don't want this to happen
break break
} }

View File

@ -21,3 +21,8 @@ type recvBuffer interface {
// has been written for a while. After that duration it should return ErrTimeout // has been written for a while. After that duration it should return ErrTimeout
SetWriteToTimeout(d time.Duration) 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

View File

@ -9,8 +9,6 @@ import (
"time" "time"
) )
const BUF_SIZE_LIMIT = 1 << 20 * 500
// The point of a streamBufferedPipe is that Read() will block until data is available // The point of a streamBufferedPipe is that Read() will block until data is available
type streamBufferedPipe struct { type streamBufferedPipe struct {
// only alloc when on first Read or Write // only alloc when on first Read or Write
@ -105,7 +103,7 @@ func (p *streamBufferedPipe) Write(input []byte) (int, error) {
if p.closed { if p.closed {
return 0, io.ErrClosedPipe 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 // if p.buf gets too large, write() will panic. We don't want this to happen
break break
} }