fix(coderd): prevent lost messages in watchWorkspaceAgentMetadata (#7934)

* fix(codersdk): wait for subscription in WatchWorkspaceAgentMetadata
* fix(coderd): subscribe before sending initial metadata event
* test(coderd): add retries to TestWorkspaceAgent_Metadata to avoid flake
This commit is contained in:
Mathias Fredriksson
2023-06-13 15:21:06 +03:00
committed by GitHub
parent 518300a26c
commit 1d0fae83a2
3 changed files with 51 additions and 17 deletions

View File

@ -1434,9 +1434,6 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ
})
}
// Send initial metadata.
sendMetadata(true)
// We debounce metadata updates to avoid overloading the frontend when
// an agent is sending a lot of updates.
pubsubDebounce := debounce.New(time.Second)
@ -1444,7 +1441,8 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ
pubsubDebounce = debounce.New(time.Millisecond * 100)
}
// Send metadata on updates.
// Send metadata on updates, we must ensure subscription before sending
// initial metadata to guarantee that events in-between are not missed.
cancelSub, err := api.Pubsub.Subscribe(watchWorkspaceAgentMetadataChannel(workspaceAgent.ID), func(_ context.Context, _ []byte) {
pubsubDebounce(func() {
sendMetadata(true)
@ -1456,12 +1454,14 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ
}
defer cancelSub()
// Send initial metadata.
sendMetadata(true)
for {
select {
case <-senderClosed:
return
case <-refreshTicker.C:
break
}
// Avoid spamming the DB with reads we know there are no updates. We want