Skip to content
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

Gracefully close websocket connections #3638

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

john-markham
Copy link

@john-markham john-markham commented Apr 4, 2025

Fixes #3633 as follow-up to discussion #2847

This is intended to gracefully close WebSocket connections, because the Gorilla/websocket doesn't provide us a nice way to do that.

I have:

  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs)

@john-markham john-markham force-pushed the jm/fix-websockets branch 6 times, most recently from 744d00d to e9aed71 Compare April 4, 2025 18:08
@@ -283,6 +295,16 @@ func (c *wsConnection) run() {
go c.closeOnCancel(ctx)

for {
c.mu.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will have performance implications

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah totally. I'm open to suggestions as to how we should measure how much this impacts performance as this is a concern for me and my team as well.

Overall though - if the thesis of this PR is correct (that we are not gracefully handling shutdown), I'm inclined to trade perf off to get us closer to WS spec compliance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we have a solution that does both: doesn't impact perf and gracefully shuts down the connection. Again open to suggestions on how to refactor the implementation to do both.

@@ -231,6 +238,11 @@ func (c *wsConnection) init() bool {

func (c *wsConnection) write(msg *message) {
c.mu.Lock()
// don't write anything to a closed / closing connection
if c.serverClosed {
c.mu.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move that as defer c.mu.Unlock() right after the lock

@coveralls
Copy link

Coverage Status

coverage: 73.381% (+0.07%) from 73.315%
when pulling e9aed71 on john-markham:jm/fix-websockets
into 4ca0dd6 on 99designs:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Graceful Websocket Connection Shutdown
3 participants