From 0e992bbaa5448f921cc519950a953d305543376f Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 30 Mar 2013 21:47:27 -0500 Subject: [PATCH] Extract conn.getBuf --- conn.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/conn.go b/conn.go index c800adf6..33d96b76 100644 --- a/conn.go +++ b/conn.go @@ -94,12 +94,7 @@ func (c *conn) rxMsgHeader() (t byte, bodySize int32, err error) { } func (c *conn) rxMsgBody(bodySize int32) (buf []byte, err error) { - if int(bodySize) <= cap(c.buf) { - buf = c.buf[:bodySize] - } else { - buf = make([]byte, bodySize) - } - + buf = c.getBuf(int(bodySize)) _, err = io.ReadFull(c.conn, buf) return } @@ -146,3 +141,14 @@ func (c *conn) txStartupMessage(msg *startupMessage) (err error) { _, err = c.conn.Write(msg.Bytes()) return } + +// Gets a []byte of n length. If possible it will reuse the connection buffer +// otherwise it will allocate a new buffer +func (c *conn) getBuf(n int) (buf []byte) { + if n <= cap(c.buf) { + buf = c.buf[:n] + } else { + buf = make([]byte, n) + } + return +}