Skip to content

Commit

Permalink
Merge pull request #83 from ksysoev/74-implement-compression-mode
Browse files Browse the repository at this point in the history
Add compression mode and threshold to channel configuration
  • Loading branch information
ksysoev authored Jun 13, 2024
2 parents ad6128a + f04db64 commit 50469b1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
22 changes: 19 additions & 3 deletions channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ type Channel struct {
}

type channelConfig struct {
originPatterns []string
originPatterns []string
compressionMode websocket.CompressionMode
compressionThreshold int
}

type Option func(*channelConfig)
Expand All @@ -36,7 +38,9 @@ func NewChannel(
opts ...Option,
) *Channel {
config := channelConfig{
originPatterns: []string{"*"},
originPatterns: []string{"*"},
compressionMode: websocket.CompressionDisabled,
compressionThreshold: 0,
}

for _, opt := range opts {
Expand Down Expand Up @@ -73,7 +77,9 @@ func (c *Channel) wsConnectionHandler() http.Handler {
}

ws, err := websocket.Accept(w, r, &websocket.AcceptOptions{
OriginPatterns: c.config.originPatterns,
OriginPatterns: c.config.originPatterns,
CompressionMode: c.config.compressionMode,
CompressionThreshold: c.config.compressionThreshold,
})

if err != nil {
Expand Down Expand Up @@ -113,3 +119,13 @@ func WithOriginPatterns(patterns ...string) Option {
c.originPatterns = patterns
}
}

// WithCompressionMode sets the compression mode and threshold for a channel configuration.
// The compression mode determines how the channel data will be compressed, and the threshold
// specifies the minimum size of the payload required for compression to be applied.
func WithCompressionMode(mode websocket.CompressionMode, threshold int) Option {
return func(c *channelConfig) {
c.compressionMode = mode
c.compressionThreshold = threshold
}
}
29 changes: 29 additions & 0 deletions channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/ksysoev/wasabi/mocks"
"nhooyr.io/websocket"
)

func TestNewChannel(t *testing.T) {
Expand Down Expand Up @@ -204,3 +205,31 @@ func TestChannel_wsConnectionHandler_CanAcceptNewConnection(t *testing.T) {
t.Errorf("Unexpected status code: got %d, expected %d", res.StatusCode, http.StatusUpgradeRequired)
}
}
func TestChannel_WithCompressionMode(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher, NewConnectionRegistry())

// Assert that the default compression mode and threshold are set correctly
if channel.config.compressionMode != websocket.CompressionDisabled {
t.Errorf("Unexpected compression mode: got %v, expected %v", channel.config.compressionMode, websocket.CompressionNoContextTakeover)
}

if channel.config.compressionThreshold != 0 {
t.Errorf("Unexpected compression threshold: got %d, expected %d", channel.config.compressionThreshold, 0)
}

compressionMode := websocket.CompressionNoContextTakeover
compressionThreshold := 1024
channel = NewChannel(path, dispatcher, NewConnectionRegistry(), WithCompressionMode(compressionMode, compressionThreshold))

// Assert that the compression mode and threshold are set correctly
if channel.config.compressionMode != compressionMode {
t.Errorf("Unexpected compression mode: got %v, expected %v", channel.config.compressionMode, compressionMode)
}

if channel.config.compressionThreshold != compressionThreshold {
t.Errorf("Unexpected compression threshold: got %d, expected %d", channel.config.compressionThreshold, compressionThreshold)
}
}

0 comments on commit 50469b1

Please sign in to comment.