Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap out flate from std library for faster one from compress. #4087

Merged
merged 2 commits into from Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions 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
Expand All @@ -15,7 +15,6 @@ package server

import (
"bytes"
"compress/flate"
"crypto/rand"
"crypto/sha1"
"crypto/tls"
Expand All @@ -34,6 +33,8 @@ import (
"sync"
"time"
"unicode/utf8"

"github.com/klauspost/compress/flate"
)

type wsOpCode int
Expand Down
13 changes: 7 additions & 6 deletions server/websocket_test.go
Expand Up @@ -16,7 +16,6 @@ package server
import (
"bufio"
"bytes"
"compress/flate"
"crypto/tls"
"encoding/base64"
"encoding/binary"
Expand All @@ -36,6 +35,8 @@ import (

"github.com/nats-io/jwt/v2"
"github.com/nats-io/nkeys"

"github.com/klauspost/compress/flate"
)

type testReader struct {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down