From 9439e9b9e14c566bda4111ce62109dacd6453ecf Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Sat, 21 Jan 2023 19:10:25 -0500 Subject: [PATCH] allow resizing the ws read buffer --- irc/ircconn.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/irc/ircconn.go b/irc/ircconn.go index f0fe947a..8deb2a81 100644 --- a/irc/ircconn.go +++ b/irc/ircconn.go @@ -102,7 +102,7 @@ func NewIRCWSConn(conn *websocket.Conn) *IRCWSConn { return &IRCWSConn{ conn: conn, binary: conn.Subprotocol() == "binary.ircv3.net", - buf: make([]byte, maxReadQBytes()), + buf: make([]byte, initialBufferSize), } } @@ -143,8 +143,7 @@ func (wc *IRCWSConn) ReadLine() (line []byte, err error) { return line, err } - n, err := io.ReadFull(reader, wc.buf) - line = wc.buf[:n] + line, err = wc.readFull(reader) switch err { case io.ErrUnexpectedEOF, io.EOF: // these are OK. io.ErrUnexpectedEOF is the good case: @@ -161,6 +160,19 @@ func (wc *IRCWSConn) ReadLine() (line []byte, err error) { } } +func (wc *IRCWSConn) readFull(reader io.Reader) (line []byte, err error) { + // XXX this is io.ReadFull with a single attempt to resize upwards + n, err := io.ReadFull(reader, wc.buf) + if err == nil && len(wc.buf) < maxReadQBytes() { + newBuf := make([]byte, maxReadQBytes()) + copy(newBuf, wc.buf[:n]) + wc.buf = newBuf + n2, err := io.ReadFull(reader, wc.buf[n:]) + return wc.buf[:n+n2], err + } + return wc.buf[:n], err +} + func (wc *IRCWSConn) Close() (err error) { return wc.conn.Close() }