Skip to content

Commit

Permalink
Swap out flate from std library for faster one from compress. (#4087)
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison committed Apr 21, 2023
2 parents f9f4bf5 + 50522f1 commit 0490896
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
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

0 comments on commit 0490896

Please sign in to comment.