Skip to content

Commit

Permalink
Merge branch 'norace'
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed Oct 11, 2024
2 parents f4cca21 + a40840c commit 3a34aa0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
9 changes: 5 additions & 4 deletions nbhttp/websocket/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Conn struct {
isInReadingLoop bool
expectingFragments bool
compress bool
releasePayload bool
msgType MessageType
message []byte
bytesCached []byte
Expand Down Expand Up @@ -193,13 +194,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.ReleasePayload {
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.ReleasePayload {
if len(body) > 0 && c.releasePayload {
c.Engine.BodyAllocator.Free(body)
}
}) {
Expand Down Expand Up @@ -529,7 +530,7 @@ func (c *Conn) Parse(data []byte) error {
func (c *Conn) OnMessage(h func(*Conn, MessageType, []byte)) {
if h != nil {
c.messageHandler = func(c *Conn, messageType MessageType, data []byte) {
if c.ReleasePayload && len(data) > 0 {
if c.releasePayload && len(data) > 0 {
defer c.Engine.BodyAllocator.Free(data)
}
if !c.closed {
Expand All @@ -545,7 +546,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.ReleasePayload {
if c.releasePayload {
defer c.Engine.BodyAllocator.Free(data)
}
h(c, messageType, fin, data)
Expand Down
13 changes: 9 additions & 4 deletions nbhttp/websocket/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,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.ReleasePayload && len(data) > 0 {
if c.releasePayload && len(data) > 0 {
defer c.Engine.BodyAllocator.Free(data)
}
if !c.closed {
Expand All @@ -259,7 +259,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.ReleasePayload {
if c.releasePayload {
defer c.Engine.BodyAllocator.Free(data)
}
h(c, messageType, fin, data)
Expand Down Expand Up @@ -544,8 +544,13 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
}
}

if !wsc.ReleasePayload {
wsc.ReleasePayload = wsc.Engine.ReleaseWebsocketPayload
// If upgrader.ReleasePayload is false, which maybe by default,
// then set it to engine.ReleaseWebsocketPayload.
// Considering compatibility with old versions, should set it to
// upgrader.ReleasePayload only when upgrader.ReleasePayload is true.
wsc.releasePayload = u.ReleasePayload
if !wsc.releasePayload {
wsc.releasePayload = wsc.Engine.ReleaseWebsocketPayload
}

return wsc, nil
Expand Down

0 comments on commit 3a34aa0

Please sign in to comment.