Skip to content

Commit

Permalink
websocket: add Upgrader/Conn.ReleasePayload config
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed May 13, 2024
1 parent e5c233e commit 187f5b6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 4 additions & 4 deletions nbhttp/websocket/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ func (c *Conn) handleMessage(opcode MessageType, body []byte) {
func (c *Conn) handleProtocolMessage(opcode MessageType, body []byte) {
if c.isBlockingMod {
c.handleWsMessage(opcode, body)
if len(body) > 0 && c.Engine.ReleaseWebsocketPayload {
if len(body) > 0 && c.ReleasePayload {
c.Engine.BodyAllocator.Free(body)
}
} else {
if !c.Execute(func() {
c.handleWsMessage(opcode, body)
if len(body) > 0 && c.Engine.ReleaseWebsocketPayload {
if len(body) > 0 && c.ReleasePayload {
c.Engine.BodyAllocator.Free(body)
}
}) {
Expand Down Expand Up @@ -493,7 +493,7 @@ Exit:
func (c *Conn) OnMessage(h func(*Conn, MessageType, []byte)) {
if h != nil {
c.messageHandler = func(c *Conn, messageType MessageType, data []byte) {
if c.Engine.ReleaseWebsocketPayload && len(data) > 0 {
if c.ReleasePayload && len(data) > 0 {
defer c.Engine.BodyAllocator.Free(data)
}
if !c.closed {
Expand All @@ -507,7 +507,7 @@ func (c *Conn) OnMessage(h func(*Conn, MessageType, []byte)) {
func (c *Conn) OnDataFrame(h func(*Conn, MessageType, bool, []byte)) {
if h != nil {
c.dataFrameHandler = func(c *Conn, messageType MessageType, fin bool, data []byte) {
if c.Engine.ReleaseWebsocketPayload {
if c.ReleasePayload {
defer c.Engine.BodyAllocator.Free(data)
}
h(c, messageType, fin, data)
Expand Down
9 changes: 7 additions & 2 deletions nbhttp/websocket/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type commonFields struct {
MessageLengthLimit int
BlockingModAsyncCloseDelay time.Duration

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

Expand Down Expand Up @@ -225,7 +226,7 @@ func (u *Upgrader) OnOpen(h func(*Conn)) {
func (u *Upgrader) OnMessage(h func(*Conn, MessageType, []byte)) {
if h != nil {
u.messageHandler = func(c *Conn, messageType MessageType, data []byte) {
if c.Engine.ReleaseWebsocketPayload && len(data) > 0 {
if c.ReleasePayload && len(data) > 0 {
defer c.Engine.BodyAllocator.Free(data)
}
if !c.closed {
Expand All @@ -239,7 +240,7 @@ func (u *Upgrader) OnMessage(h func(*Conn, MessageType, []byte)) {
func (u *Upgrader) OnDataFrame(h func(*Conn, MessageType, bool, []byte)) {
if h != nil {
u.dataFrameHandler = func(c *Conn, messageType MessageType, fin bool, data []byte) {
if c.Engine.ReleaseWebsocketPayload {
if c.ReleasePayload {
defer c.Engine.BodyAllocator.Free(data)
}
h(c, messageType, fin, data)
Expand Down Expand Up @@ -520,6 +521,10 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
}
}

if !wsc.ReleasePayload {
wsc.ReleasePayload = wsc.Engine.ReleaseWebsocketPayload
}

return wsc, nil
}

Expand Down

0 comments on commit 187f5b6

Please sign in to comment.