- Notifications
You must be signed in to change notification settings - Fork1k
chore: Refactor accepting websocket connections to track for close#7008
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
884c71b
chore: Refactor accepting websocket connections to track for close
Emyrkbf64a43
Refactor websockets to be tracked
Emyrk45b969a
Push websocket tracker into tailnet as well
Emyrk15ed467
Fix import cycle
Emyrk1fd1428
Remove derpclose func
EmyrkFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletionscoderd/activewebsockets/sockets.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package activewebsockets | ||
import ( | ||
"context" | ||
"net/http" | ||
"runtime/pprof" | ||
"sync" | ||
"nhooyr.io/websocket" | ||
"github.com/coder/coder/coderd/httpapi" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
// Active is a helper struct that can be used to track active | ||
// websocket connections. All connections will be closed when the parent | ||
// context is canceled. | ||
type Active struct { | ||
ctx context.Context | ||
cancel func() | ||
wg sync.WaitGroup | ||
} | ||
func New(ctx context.Context) *Active { | ||
ctx, cancel := context.WithCancel(ctx) | ||
return &Active{ | ||
ctx: ctx, | ||
cancel: cancel, | ||
} | ||
} | ||
// Accept accepts a websocket connection and calls f with the connection. | ||
// The function will be tracked by the Active struct and will be | ||
// closed when the parent context is canceled. | ||
// Steps: | ||
// 1. Ensure we are still accepting websocket connections, and not shutting down. | ||
// 2. Add 1 to the wait group. | ||
// 3. Ensure we decrement the wait group when we are done (defer). | ||
// 4. Accept the websocket connection. | ||
// 4a. If there is an error, write the error to the response writer and return. | ||
// 5. Launch go routine to kill websocket if the parent context is canceled. | ||
// 6. Call 'f' with the websocket connection. | ||
func (a *Active) Accept(rw http.ResponseWriter, r *http.Request, options *websocket.AcceptOptions, f func(conn *websocket.Conn)) { | ||
// Ensure we are still accepting websocket connections, and not shutting down. | ||
if err := a.ctx.Err(); err != nil { | ||
httpapi.Write(context.Background(), rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "No longer accepting websocket requests.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
// Ensure we decrement the wait group when we are done. | ||
a.wg.Add(1) | ||
defer a.wg.Done() | ||
// Accept the websocket connection | ||
conn, err := websocket.Accept(rw, r, options) | ||
if err != nil { | ||
httpapi.Write(context.Background(), rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Failed to accept websocket.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
// Always track the connection before allowing the caller to handle it. | ||
// This ensures the connection is closed when the parent context is canceled. | ||
// This new context will end if the parent context is cancelled or if | ||
// the connection is closed. | ||
ctx, cancel := context.WithCancel(a.ctx) | ||
defer cancel() | ||
closeConnOnContext(ctx, conn) | ||
// Handle the websocket connection | ||
f(conn) | ||
} | ||
// closeConnOnContext launches a go routine that will watch a given context | ||
// and close a websocket connection if that context is canceled. | ||
func closeConnOnContext(ctx context.Context, conn *websocket.Conn) { | ||
// Labeling the go routine for goroutine dumps/debugging. | ||
go pprof.Do(ctx, pprof.Labels("service", "ActiveWebsockets"), func(ctx context.Context) { | ||
select { | ||
case <-ctx.Done(): | ||
_ = conn.Close(websocket.StatusNormalClosure, "") | ||
} | ||
}) | ||
} | ||
// Close will close all active websocket connections and wait for them to | ||
// finish. | ||
func (a *Active) Close() { | ||
a.cancel() | ||
a.wg.Wait() | ||
} |
14 changes: 5 additions & 9 deletionscoderd/coderd.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
11 changes: 8 additions & 3 deletionscoderd/healthcheck/derp_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
96 changes: 43 additions & 53 deletionscoderd/provisionerjobs.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.