Skip to content

Commit

Permalink
reduce memory consumption on readOne
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Dec 3, 2018
1 parent c5a3841 commit 844f693
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions common/buf/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ import (
)

func readOne(r io.Reader) (*Buffer, error) {
// Use an one-byte buffer to wait for incoming payload.
var firstByte [1]byte
nBytes, err := r.Read(firstByte[:])
if err != nil {
return nil, err
}

b := New()
if nBytes > 0 {
copy(b.Extend(int32(nBytes)), firstByte[:])
}
for i := 0; i < 64; i++ {
_, err := b.ReadFrom(r)
if !b.IsEmpty() {
Expand All @@ -20,6 +30,7 @@ func readOne(r io.Reader) (*Buffer, error) {
}
}

b.Release()
return nil, newError("Reader returns too many empty payloads.")
}

Expand Down

0 comments on commit 844f693

Please sign in to comment.