Skip to content

Commit

Permalink
[IMPROVED] Memory growth on compressed websocket connections. (#4620)
Browse files Browse the repository at this point in the history
Holding onto the compressor and not recycling the internal byte slice
could cause havoc with GC.

This needs to be improved but this at least should allow the GC to
cleanup more effectively.

Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison committed Oct 3, 2023
2 parents 195227e + 2f1a384 commit 21e2723
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions server/websocket.go
Expand Up @@ -115,7 +115,6 @@ type websocket struct {
nocompfrag bool // No fragment for compressed frames
maskread bool
maskwrite bool
compressor *flate.Writer
cookieJwt string
clientIP string
}
Expand Down Expand Up @@ -1295,15 +1294,8 @@ func (c *client) wsCollapsePtoNB() (net.Buffers, int64) {
if mfs > 0 && c.ws.nocompfrag {
mfs = 0
}
buf := &bytes.Buffer{}

cp := c.ws.compressor
if cp == nil {
c.ws.compressor, _ = flate.NewWriter(buf, flate.BestSpeed)
cp = c.ws.compressor
} else {
cp.Reset(buf)
}
buf := bytes.NewBuffer(nbPoolGet(usz))
cp, _ := flate.NewWriter(buf, flate.BestSpeed)
var csz int
for _, b := range nb {
cp.Write(b)
Expand Down Expand Up @@ -1352,6 +1344,7 @@ func (c *client) wsCollapsePtoNB() (net.Buffers, int64) {
}
csz = len(h) + ol
}
nbPoolPut(b) // No longer needed as we copied from above.
// Add to pb the compressed data size (including headers), but
// remove the original uncompressed data size that was added
// during the queueing.
Expand Down

0 comments on commit 21e2723

Please sign in to comment.