From b20bca0c7783949c080bd21cce227d7c82df6e21 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 8 Nov 2024 15:31:30 +0000 Subject: [PATCH] Request now uses reusable buffer --- internal/packet/request.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/packet/request.go b/internal/packet/request.go index 557c94f..58f93a6 100644 --- a/internal/packet/request.go +++ b/internal/packet/request.go @@ -1,16 +1,23 @@ package packet -import "fmt" +import ( + "bytes" + "fmt" +) + +const bufSz = 512 type Request struct { magic []byte password string + buf *bytes.Buffer } func NewRequest(password string) Request { return Request{ magic: []byte{'\xff', '\xff', '\xff', '\xff'}, password: password, + buf: bytes.NewBuffer(make([]byte, bufSz)), } } @@ -19,7 +26,8 @@ func (r Request) Header() []byte { } func (r Request) Encode(cmd string) []byte { - datagram := r.Header() - datagram = append(datagram, fmt.Sprintf(" %s %s", r.password, cmd)...) - return datagram + r.buf.Reset() + r.buf.Write(r.Header()) + r.buf.WriteString(fmt.Sprintf(" %s %s", r.password, cmd)) + return r.buf.Bytes() }