-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Close Websocket Connection on Context close/cancel #1728
Conversation
…ed (with an optional close reason).
Signed-off-by: Steve Coffman <[email protected]>
h := testserver.New() | ||
h.AddTransport(transport.Websocket{ | ||
InitFunc: func(ctx context.Context, _ transport.InitPayload) (context.Context, error) { | ||
newCtx, _ := context.WithTimeout(transport.AppendCloseReason(ctx, "beep boop"), time.Millisecond*5) | ||
return newCtx, nil | ||
}, | ||
}) | ||
srv := httptest.NewServer(h) | ||
defer srv.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
h := testserver.New() | |
h.AddTransport(transport.Websocket{ | |
InitFunc: func(ctx context.Context, _ transport.InitPayload) (context.Context, error) { | |
newCtx, _ := context.WithTimeout(transport.AppendCloseReason(ctx, "beep boop"), time.Millisecond*5) | |
return newCtx, nil | |
}, | |
}) | |
srv := httptest.NewServer(h) | |
defer srv.Close() | |
h := testserver.New() | |
var cancel context.CancelFunc | |
h.AddTransport(transport.Websocket{ | |
InitFunc: func(ctx context.Context, _ transport.InitPayload) (context.Context, error) { | |
var newCtx context.Context | |
newCtx, cancel = context.WithTimeout(transport.AppendCloseReason(ctx, "beep boop"), time.Millisecond*5) | |
return newCtx, nil | |
}, | |
}) | |
defer cancel() | |
srv := httptest.NewServer(h) | |
defer srv.Close() |
I know it's just for a test, but what do you think about retaining the closeFunc and calling it in a defer somewhere to satisfy the linter that there's no leaks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I hope you don't mind, but I ran gofmt and pushed that change to your remote branch. The linter is still complaining about the possible context leak, and I provided a suggestion for your consideration. Overall, this looks pretty good to me. @zdraganov do you have any thoughts?
LGTM |
Thanks so much! |
And thank you for the quick support! ;) |
This issue explains it all: #1727
I have:
Not needed: