Skip to content

Commit

Permalink
websocket: optimize async close delay
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed Sep 14, 2023
1 parent eb81029 commit 5ca5456
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 4 additions & 1 deletion nbhttp/websocket/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (c *Conn) Close() error {
return nil
}
if c.IsAsyncWrite() {
c.Engine.AfterFunc(time.Second, func() { c.Conn.Close() })
c.Engine.AfterFunc(c.BlockingModAsyncCloseDelay, func() { c.Conn.Close() })
return nil
}
return c.Conn.Close()
Expand Down Expand Up @@ -780,6 +780,9 @@ func NewConn(u *Upgrader, c net.Conn, subprotocol string, remoteCompressionEnabl
if asyncWrite {
wsc.sendQueue = make([][]byte, u.BlockingModSendQueueInitSize)[:0]
wsc.sendQueueSize = u.BlockingModSendQueueMaxSize
if wsc.BlockingModAsyncCloseDelay <= 0 {
wsc.BlockingModAsyncCloseDelay = DefaultBlockingModAsyncCloseDelay
}
}
return wsc
}
Expand Down
14 changes: 9 additions & 5 deletions nbhttp/websocket/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ var (

DefaultBlockingModSendQueueMaxSize = 0

DefaultBlockingModAsyncCloseDelay = time.Second / 10

// DefaultEngine will be set to a Upgrader.Engine to handle details such as buffers.
DefaultEngine = nbhttp.NewEngine(nbhttp.Config{
ReleaseWebsocketPayload: true,
})
)

type commonFields struct {
Engine *nbhttp.Engine
KeepaliveTime time.Duration
MessageLengthLimit int
Engine *nbhttp.Engine
KeepaliveTime time.Duration
MessageLengthLimit int
BlockingModAsyncCloseDelay time.Duration

enableCompression bool
compressionLevel int
Expand Down Expand Up @@ -92,8 +95,9 @@ type Upgrader struct {
func NewUpgrader() *Upgrader {
u := &Upgrader{
commonFields: commonFields{
Engine: DefaultEngine,
compressionLevel: defaultCompressionLevel,
Engine: DefaultEngine,
compressionLevel: defaultCompressionLevel,
BlockingModAsyncCloseDelay: DefaultBlockingModAsyncCloseDelay,
},
BlockingModReadBufferSize: DefaultBlockingReadBufferSize,
BlockingModAsyncWrite: DefaultBlockingModAsyncWrite,
Expand Down

0 comments on commit 5ca5456

Please sign in to comment.