Skip to content

Commit

Permalink
websocket: move compressor config from nbhttp Engine to upgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed May 12, 2024
1 parent 89ba141 commit e5c233e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions nbhttp/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,11 @@ type Config struct {
// ReadBufferPool .
ReadBufferPool mempool.Allocator

// Deprecated.
// WebsocketCompressor .
WebsocketCompressor func(w io.WriteCloser, level int) io.WriteCloser

// Deprecated.
// WebsocketDecompressor .
WebsocketDecompressor func(r io.Reader) io.ReadCloser

Expand Down
8 changes: 4 additions & 4 deletions nbhttp/websocket/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ func (c *Conn) Parse(data []byte) error {
if c.compress {
var b []byte
var rc io.ReadCloser
if c.Engine.WebsocketDecompressor != nil {
rc = c.Engine.WebsocketDecompressor(io.MultiReader(bytes.NewBuffer(message), strings.NewReader(flateReaderTail)))
if c.WebsocketDecompressor != nil {
rc = c.WebsocketDecompressor(c, io.MultiReader(bytes.NewBuffer(message), strings.NewReader(flateReaderTail)))
} else {
rc = decompressReader(io.MultiReader(bytes.NewBuffer(message), strings.NewReader(flateReaderTail)))
}
Expand Down Expand Up @@ -562,8 +562,8 @@ func (c *Conn) WriteMessage(messageType MessageType, data []byte) error {
w.Reset()

var cw io.WriteCloser
if c.Engine.WebsocketCompressor != nil {
cw = c.Engine.WebsocketCompressor(w, c.compressionLevel)
if c.WebsocketCompressor != nil {
cw = c.WebsocketCompressor(c, w, c.compressionLevel)
} else {
cw = compressWriter(w, c.compressionLevel)
}
Expand Down
3 changes: 3 additions & 0 deletions nbhttp/websocket/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ type commonFields struct {
MessageLengthLimit int
BlockingModAsyncCloseDelay time.Duration

WebsocketCompressor func(c *Conn, w io.WriteCloser, level int) io.WriteCloser
WebsocketDecompressor func(c *Conn, r io.Reader) io.ReadCloser

pingMessageHandler func(c *Conn, appData string)
pongMessageHandler func(c *Conn, appData string)
closeMessageHandler func(c *Conn, code int, text string)
Expand Down

0 comments on commit e5c233e

Please sign in to comment.