Deallocate huge read buffers (#18813)

If a message buffer is excessively huge, release it back so it isn't kept around forever.
This commit is contained in:
Klaus Post 2024-01-17 11:47:42 -08:00 committed by GitHub
parent 8cd967803c
commit 479940b7d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -899,7 +899,6 @@ func (c *Connection) handleMessages(ctx context.Context, conn net.Conn) {
} }
continue continue
} }
if int64(cap(dst)) < hdr.Length+1 { if int64(cap(dst)) < hdr.Length+1 {
dst = make([]byte, 0, hdr.Length+hdr.Length>>3) dst = make([]byte, 0, hdr.Length+hdr.Length>>3)
} }
@ -914,6 +913,10 @@ func (c *Connection) handleMessages(ctx context.Context, conn net.Conn) {
cancel(ErrDisconnected) cancel(ErrDisconnected)
return return
} }
if cap(msg) > readBufferSize*8 {
// Don't keep too much memory around.
msg = nil
}
var err error var err error
msg, err = readDataInto(msg, conn, c.side, ws.OpBinary) msg, err = readDataInto(msg, conn, c.side, ws.OpBinary)