Skip to content

Commit

Permalink
Don't reuse buffer on Write() (#1)
Browse files Browse the repository at this point in the history
* drpcwire: expose buffer size on Reader (storj#30)

The maximum was hardcoded at 4MB. This allows it to
be configurable for larger payloads. An option is
exposed in `dprcmanager.Options`.

* regen docs

Change-Id: I04ac7c0c5bea05a95ee38cd0584ca624ce60f077

* Don't reuse buffer after Write()

Signed-off-by: Spike Curtis <[email protected]>

Co-authored-by: Kyle Carberry <[email protected]>
Co-authored-by: Jeff Wendling <[email protected]>
  • Loading branch information
3 people authored Jun 21, 2022
1 parent d6d4c88 commit bf08aad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions drpcmanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type Options struct {
// flushing. Normal writes to streams typically issue a flush explicitly.
WriterBufferSize int

// Reader are passed to any readers the manager creates.
Reader drpcwire.ReaderOptions

// Stream are passed to any streams the manager creates.
Stream drpcstream.Options

Expand Down
20 changes: 20 additions & 0 deletions drpcwire/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ func NewReader(r io.Reader) *Reader
```
NewReader constructs a Reader to read Packets from the io.Reader.

#### func NewReaderWithOptions

```go
func NewReaderWithOptions(r io.Reader, opts ReaderOptions) *Reader
```
NewReaderWithOptions constructs a Reader to read Packets from the io.Reader. It
uses the provided options to manage buffering.

#### func (*Reader) ReadPacket

```go
Expand All @@ -230,6 +238,18 @@ becomes too large, an error is returned. The returned packet's Data field is
constructed by appending to the provided buf after it has been resliced to be
zero length.

#### type ReaderOptions

```go
type ReaderOptions struct {
// MaximumBufferSize controls the maximum size of buffered
// packet data.
MaximumBufferSize int
}
```

ReaderOptions controls configuration settings for a reader.

#### type Writer

```go
Expand Down
6 changes: 3 additions & 3 deletions drpcwire/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (b *Writer) Empty() bool {
// Reset clears any pending data in the buffer.
func (b *Writer) Reset() *Writer {
b.mu.Lock()
b.buf = b.buf[:0]
b.buf = make([]byte, 0, b.size)
b.mu.Unlock()
return b
}
Expand All @@ -70,7 +70,7 @@ func (b *Writer) WriteFrame(fr Frame) (err error) {
b.buf = AppendFrame(b.buf, fr)
if len(b.buf) >= b.size {
_, err = b.w.Write(b.buf)
b.buf = b.buf[:0]
b.buf = make([]byte, 0, b.size)
atomic.StoreUint32(&b.empty, 0)
}
b.mu.Unlock()
Expand All @@ -83,7 +83,7 @@ func (b *Writer) Flush() (err error) {
b.mu.Lock()
if len(b.buf) > 0 {
_, err = b.w.Write(b.buf)
b.buf = b.buf[:0]
b.buf = make([]byte, 0, b.size)
atomic.StoreUint32(&b.empty, 0)
}
b.mu.Unlock()
Expand Down

0 comments on commit bf08aad

Please sign in to comment.