Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the maximum message size configurable #15

Merged
merged 1 commit into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions msgio.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,29 @@ func NewReader(r io.Reader) ReadCloser {
return NewReaderWithPool(r, pool.GlobalPool)
}

// NewReaderWithPool wraps an io.Reader with a msgio framed reader. The msgio.Reader
// will read whole messages at a time (using the length). Assumes an equivalent
// writer on the other side. It uses a given pool.BufferPool
// NewReaderSize is equivalent to NewReader but allows one to
// specify a max message size.
func NewReaderSize(r io.Reader, maxMessageSize int) ReadCloser {
return NewReaderSizeWithPool(r, maxMessageSize, pool.GlobalPool)
}

// NewReaderWithPool is the same as NewReader but allows one to specify a buffer
// pool.
func NewReaderWithPool(r io.Reader, p *pool.BufferPool) ReadCloser {
return NewReaderSizeWithPool(r, defaultMaxSize, p)
}

// NewReaderWithPool is the same as NewReader but allows one to specify a buffer
// pool and a max message size.
func NewReaderSizeWithPool(r io.Reader, maxMessageSize int, p *pool.BufferPool) ReadCloser {
if p == nil {
panic("nil pool")
}
return &reader{
R: r,
next: -1,
pool: p,
max: defaultMaxSize,
max: maxMessageSize,
}
}

Expand Down
23 changes: 16 additions & 7 deletions varint.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,24 @@ type varintReader struct {
// Varints read according to https://golang.org/pkg/encoding/binary/#ReadUvarint
// Assumes an equivalent writer on the other side.
func NewVarintReader(r io.Reader) ReadCloser {
return NewVarintReaderWithPool(r, pool.GlobalPool)
return NewVarintReaderSize(r, defaultMaxSize)
}

// NewVarintReaderWithPool wraps an io.Reader with a varint msgio framed reader.
// The msgio.Reader will read whole messages at a time (using the length).
// Varints read according to https://golang.org/pkg/encoding/binary/#ReadUvarint
// Assumes an equivalent writer on the other side. It uses a given
// pool.BufferPool.
// NewVarintReaderSize is equivalent to NewVarintReader but allows one to
// specify a max message size.
func NewVarintReaderSize(r io.Reader, maxMessageSize int) ReadCloser {
return NewVarintReaderSizeWithPool(r, maxMessageSize, pool.GlobalPool)
}

// NewVarintReaderWithPool is the same as NewVarintReader but allows one to
// specify a buffer pool.
func NewVarintReaderWithPool(r io.Reader, p *pool.BufferPool) ReadCloser {
return NewVarintReaderSizeWithPool(r, defaultMaxSize, p)
}

// NewVarintReaderWithPool is the same as NewVarintReader but allows one to
// specify a buffer pool and a max message size.
func NewVarintReaderSizeWithPool(r io.Reader, maxMessageSize int, p *pool.BufferPool) ReadCloser {
if p == nil {
panic("nil pool")
}
Expand All @@ -91,7 +100,7 @@ func NewVarintReaderWithPool(r io.Reader, p *pool.BufferPool) ReadCloser {
br: &simpleByteReader{R: r},
next: -1,
pool: p,
max: defaultMaxSize,
max: maxMessageSize,
}
}

Expand Down