-
Notifications
You must be signed in to change notification settings - Fork 983
feat(site): use websocket connection for devcontainer updates #18808
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
Changes from 1 commit
487ee95
cc42018
fa46517
5aef560
975ef8b
8fdeca3
6da941f
ff5725e
178507c
367b87d
34b17c4
8f12460
81022fa
1768f7b
8240663
6d97960
88a611d
001ccda
3e50965
6ce5c19
cd0c2d5
04a92a4
096a85e
971f9d6
f24401f
64d9252
40c3fd9
1cda455
2ded15f
a87f388
2de01f5
00fdae6
a4a4bb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -560,14 +560,20 @@ func (api *API) watchContainers(rw http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
ctx = api.ctx | ||
|
||
go httpapi.Heartbeat(ctx, conn) | ||
defer conn.Close(websocket.StatusNormalClosure, "connection closed") | ||
|
||
encoder := wsjson.NewEncoder[codersdk.WorkspaceAgentListContainersResponse](conn, websocket.MessageText) | ||
defer encoder.Close(websocket.StatusNormalClosure) | ||
|
||
updateCh := make(chan struct{}) | ||
defer close(updateCh) | ||
updateCh := make(chan struct{}, 1) | ||
defer func() { | ||
api.mu.Lock() | ||
close(updateCh) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd make sense to move this to the defer below to keep the related code close to each other. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 6ce5c19 |
||
api.mu.Unlock() | ||
}() | ||
|
||
api.mu.Lock() | ||
api.updateChans = append(api.updateChans, updateCh) | ||
|
@@ -644,7 +650,10 @@ func (api *API) updateContainers(ctx context.Context) error { | |
|
||
// Broadcast our updates | ||
for _, ch := range api.updateChans { | ||
ch <- struct{}{} | ||
select { | ||
case ch <- struct{}{}: | ||
default: | ||
} | ||
} | ||
|
||
api.logger.Debug(ctx, "containers updated successfully", slog.F("container_count", len(api.containers.Containers)), slog.F("warning_count", len(api.containers.Warnings)), slog.F("devcontainer_count", len(api.knownDevcontainers))) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -865,6 +865,8 @@ func (api *API) watchWorkspaceAgentContainers(rw http.ResponseWriter, r *http.Re | |
return | ||
} | ||
|
||
ctx = api.ctx | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See related comments on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 04a92a4 |
||
|
||
go httpapi.Heartbeat(ctx, conn) | ||
defer conn.Close(websocket.StatusNormalClosure, "connection closed") | ||
|
||
|
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.
We could do this here instead:
Note that we're not passing
api.ctx
toWebsocketNetConn
but that's OK because of the final select (see below).Then a few important changes following:
Ping
fromconn
)conn.Close
(handled viawsNetConn.Close
)wsNetConn
api.ctx
and another forctx
This follows practice we use elsewhere in the code-base.
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.
Is this possible?
wsjson.NewEncoder
requires a*websocket.Conn
butwsNetConn
is anet.Conn
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.
wsjson.NewEncoder
is a tiny wrapper, i'll just write the underlying logic usingwsNetConn
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.
Addressed in 04a92a4
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.
Oh I see, I hadn't actually seen/used
wsjson.NewEncoder
before. TheCloseRead
it calls would be ideal to retain.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.
Have re-added this in 😄 096a85e