diff --git a/server/websocket.go b/server/websocket.go index 5d19ee6be1..1becca5a4c 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -1,4 +1,4 @@ -// Copyright 2020 The NATS Authors +// Copyright 2020-2023 The NATS Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -15,7 +15,6 @@ package server import ( "bytes" - "compress/flate" "crypto/rand" "crypto/sha1" "crypto/tls" @@ -34,6 +33,8 @@ import ( "sync" "time" "unicode/utf8" + + "github.com/klauspost/compress/flate" ) type wsOpCode int diff --git a/server/websocket_test.go b/server/websocket_test.go index 3439f8e8fc..6e1c91266f 100644 --- a/server/websocket_test.go +++ b/server/websocket_test.go @@ -16,7 +16,6 @@ package server import ( "bufio" "bytes" - "compress/flate" "crypto/tls" "encoding/base64" "encoding/binary" @@ -36,6 +35,8 @@ import ( "github.com/nats-io/jwt/v2" "github.com/nats-io/nkeys" + + "github.com/klauspost/compress/flate" ) type testReader struct { @@ -2863,11 +2864,11 @@ func (wc *testWSWrappedConn) Write(p []byte) (int, error) { } func TestWSCompressionBasic(t *testing.T) { - payload := "This is the content of a message that will be compresseddddddddddddddddddddd." + payload := "This is the content of a message that will be compresseddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd." msgProto := fmt.Sprintf("MSG foo 1 %d\r\n%s\r\n", len(payload), payload) - cbuf := &bytes.Buffer{} - compressor, _ := flate.NewWriter(cbuf, flate.BestSpeed) + compressor, err := flate.NewWriter(cbuf, flate.BestSpeed) + require_NoError(t, err) compressor.Write([]byte(msgProto)) compressor.Flush() compressed := cbuf.Bytes() @@ -2890,14 +2891,14 @@ func TestWSCompressionBasic(t *testing.T) { } var wc *testWSWrappedConn - s.mu.Lock() + s.mu.RLock() for _, c := range s.clients { c.mu.Lock() wc = &testWSWrappedConn{Conn: c.nc, buf: &bytes.Buffer{}} c.nc = wc c.mu.Unlock() } - s.mu.Unlock() + s.mu.RUnlock() nc := natsConnect(t, s.ClientURL()) defer nc.Close()