3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +01:00

allow resizing the ws read buffer

This commit is contained in:
Shivaram Lingamneni 2023-01-21 19:10:25 -05:00
parent 5eaf7b37e5
commit 9439e9b9e1

View File

@ -102,7 +102,7 @@ func NewIRCWSConn(conn *websocket.Conn) *IRCWSConn {
return &IRCWSConn{ return &IRCWSConn{
conn: conn, conn: conn,
binary: conn.Subprotocol() == "binary.ircv3.net", 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 return line, err
} }
n, err := io.ReadFull(reader, wc.buf) line, err = wc.readFull(reader)
line = wc.buf[:n]
switch err { switch err {
case io.ErrUnexpectedEOF, io.EOF: case io.ErrUnexpectedEOF, io.EOF:
// these are OK. io.ErrUnexpectedEOF is the good case: // 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) { func (wc *IRCWSConn) Close() (err error) {
return wc.conn.Close() return wc.conn.Close()
} }