chore: Refactor accepting websocket connections to track for close #7008
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an idea I had.
The problem is that the Coderd
API
struct callsWait()
on a waitgroup for all websockets to close before theClose()
method returns. TheClose()
method callscancel
on the parent context, but this context is not actually controlling the lifecycle of the individual request for a given websocket.So
Close()
does not tell a websocket to end. It was achieving this because the parent context is tied to*wsconncache.Cache
which kills the tailnet connections from the parent context.To make this work more directly, I implemented ActiveWebsockets. This struct takes the parent context. When you call
Accept
on this struct, it launches a go routine that will force close the websocket if the parent context is cancelled. This all happens when you callClose()
on theActiveWebsocket
struct.This also means we can pass this struct in places like
WithWebsocketSupport
:coder/tailnet/derp.go
Lines 21 to 22 in bf64a43
Right now we just duplicate the
sync.WaitGroup
logic and return aWait()
which is just kinda annoying.This isn't 100% better, but it does handle the tracking:
coder/tailnet/derp.go
Lines 13 to 21 in 15ed467
Thoughts?