From 844f6937a462684f105cd94a16f0e12b30618843 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 3 Dec 2018 12:40:38 +0100 Subject: [PATCH] reduce memory consumption on readOne --- common/buf/reader.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/common/buf/reader.go b/common/buf/reader.go index 1d21b8a87..24b578d4c 100644 --- a/common/buf/reader.go +++ b/common/buf/reader.go @@ -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() { @@ -20,6 +30,7 @@ func readOne(r io.Reader) (*Buffer, error) { } } + b.Release() return nil, newError("Reader returns too many empty payloads.") }