Replies: 1 comment
-
the answer is Outside the code i has context with cancel(parentContext) InitFunc: func(ctx context.Context, _ transport.InitPayload) (context.Context, *transport.InitPayload, error) {
newCtx, initFuncCancel := context.WithCancel(ctx)
go func() {
<-parentContext.Done()
initFuncCancel()
}()
return newCtx, nil, nil
}, Also, to ensure that websocket connections are init and close correctly i used this code ...
var liveWebsocketConnections atomic.Int64
h.AddTransport(transport.Websocket{
...
KeepAlivePingInterval: cfg.Connection.WebsocketKeepAliveInterval,
InitFunc: func(ctx context.Context, _ transport.InitPayload) (context.Context, *transport.InitPayload, error) {
liveWebsocketConnections.Add(1)
logger.Warn(fmt.Sprintf("websocket open. Active connections %d", liveWebsocketConnections.Load()))
newCtx, initFuncCancel := context.WithCancel(ctx)
go func() {
<-parentContext.Done()
initFuncCancel()
}()
return newCtx, nil, nil
},
CloseFunc: func(ctx context.Context, closeCode int) {
liveWebsocketConnections.Add(-1)
logger.Warn(fmt.Sprintf("websocket closed. Active connections %d", liveWebsocketConnections.Load()))
},
...
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, i use your library in this way
create handler
and then use it in server.
After i got SIGTERM, i close server by
s.Shutdown
- https://pkg.go.dev/net/http#Server.ShutdownBut default go http server cannot close websocket connections:
so, how to close websocket connections correct?
Beta Was this translation helpful? Give feedback.
All reactions