diff --git a/server/client.go b/server/client.go index 8c023b86..e7080f82 100644 --- a/server/client.go +++ b/server/client.go @@ -1035,6 +1035,10 @@ func (c *client) flushOutbound() bool { // For selecting primary replacement. cnb := nb + var lfs int + if len(cnb) > 0 { + lfs = len(cnb[0]) + } // In case it goes away after releasing the lock. nc := c.nc @@ -1112,7 +1116,7 @@ func (c *client) flushOutbound() bool { } // Check to see if we can reuse buffers. - if len(cnb) > 0 && n >= int64(len(cnb[0])) { + if lfs != 0 && n >= int64(lfs) { oldp := cnb[0][:0] if cap(oldp) >= int(c.out.sz) { // Replace primary or secondary if they are nil, reusing same buffer. diff --git a/server/client_test.go b/server/client_test.go index 449d21b1..9a5ddc1d 100644 --- a/server/client_test.go +++ b/server/client_test.go @@ -1943,7 +1943,7 @@ type testConnWritePartial struct { func (c *testConnWritePartial) Write(p []byte) (int, error) { n := len(p) if c.partial { - n = 5 + n = 15 } return c.buf.Write(p[:n]) } @@ -1957,7 +1957,7 @@ func TestFlushOutboundNoSliceReuseIfPartial(t *testing.T) { opts.MaxPending = 1024 s := &Server{opts: opts} - fakeConn := &testConnWritePartial{} + fakeConn := &testConnWritePartial{partial: true} c := &client{srv: s, nc: fakeConn} c.initClient() @@ -1967,18 +1967,13 @@ func TestFlushOutboundNoSliceReuseIfPartial(t *testing.T) { []byte("0123456789"), } expected := bytes.Buffer{} - for i, buf := range bufs { + for _, buf := range bufs { expected.Write(buf) c.mu.Lock() - if i == 0 { - fakeConn.partial = true - c.out.sz = 10 - } else { - fakeConn.partial = false - c.out.sz = 5 - } c.queueOutbound(buf) + c.out.sz = 10 c.flushOutbound() + fakeConn.partial = false c.mu.Unlock() } // Ensure everything is flushed.