Skip to content

Commit 2a53d6a

Browse files
committed
fix(coderd): Subscribe before sending initial metadata event
1 parent 89f3db5 commit 2a53d6a

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

coderd/workspaceagents.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,17 +1434,15 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ
14341434
})
14351435
}
14361436

1437-
// Send initial metadata.
1438-
sendMetadata(true)
1439-
14401437
// We debounce metadata updates to avoid overloading the frontend when
14411438
// an agent is sending a lot of updates.
14421439
pubsubDebounce := debounce.New(time.Second)
14431440
if flag.Lookup("test.v") != nil {
14441441
pubsubDebounce = debounce.New(time.Millisecond * 100)
14451442
}
14461443

1447-
// Send metadata on updates.
1444+
// Send metadata on updates, we must ensure subscription before sending
1445+
// initial metadata to guarantee that events in-between are not missed.
14481446
cancelSub, err := api.Pubsub.Subscribe(watchWorkspaceAgentMetadataChannel(workspaceAgent.ID), func(_ context.Context, _ []byte) {
14491447
pubsubDebounce(func() {
14501448
sendMetadata(true)
@@ -1456,6 +1454,9 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ
14561454
}
14571455
defer cancelSub()
14581456

1457+
// Send initial metadata.
1458+
sendMetadata(true)
1459+
14591460
for {
14601461
select {
14611462
case <-senderClosed:

coderd/workspaceagents_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,10 +1290,10 @@ func TestWorkspaceAgent_Metadata(t *testing.T) {
12901290
return nil
12911291
}
12921292

1293-
check := func(want codersdk.WorkspaceAgentMetadataResult, got codersdk.WorkspaceAgentMetadata) {
1293+
check := func(want codersdk.WorkspaceAgentMetadataResult, got codersdk.WorkspaceAgentMetadata, retry bool) {
12941294
// We can't trust the order of the updates due to timers and debounces,
1295-
// so let's check a few times once more.
1296-
for i := 0; i < 2 && (want.Value != got.Result.Value || want.Error != got.Result.Error); i++ {
1295+
// so let's check a few times more.
1296+
for i := 0; retry && i < 2 && (want.Value != got.Result.Value || want.Error != got.Result.Error); i++ {
12971297
update = recvUpdate()
12981298
for _, m := range update {
12991299
if m.Description.Key == got.Description.Key {
@@ -1311,22 +1311,22 @@ func TestWorkspaceAgent_Metadata(t *testing.T) {
13111311

13121312
update = recvUpdate()
13131313
require.Len(t, update, 3)
1314-
check(wantMetadata1, update[0])
1314+
check(wantMetadata1, update[0], false)
13151315
// The second metadata result is not yet posted.
13161316
require.Zero(t, update[1].Result.CollectedAt)
13171317

13181318
wantMetadata2 := wantMetadata1
13191319
post("foo2", wantMetadata2)
13201320
update = recvUpdate()
13211321
require.Len(t, update, 3)
1322-
check(wantMetadata1, update[0])
1323-
check(wantMetadata2, update[1])
1322+
check(wantMetadata1, update[0], true)
1323+
check(wantMetadata2, update[1], true)
13241324

13251325
wantMetadata1.Error = "error"
13261326
post("foo1", wantMetadata1)
13271327
update = recvUpdate()
13281328
require.Len(t, update, 3)
1329-
check(wantMetadata1, update[0])
1329+
check(wantMetadata1, update[0], true)
13301330

13311331
const maxValueLen = 32 << 10
13321332
tooLongValueMetadata := wantMetadata1
@@ -1335,7 +1335,7 @@ func TestWorkspaceAgent_Metadata(t *testing.T) {
13351335
tooLongValueMetadata.CollectedAt = time.Now()
13361336
post("foo3", tooLongValueMetadata)
13371337
got := recvUpdate()[2]
1338-
for len(got.Result.Value) != maxValueLen {
1338+
for i := 0; i < 2 && len(got.Result.Value) != maxValueLen; i++ {
13391339
got = recvUpdate()[2]
13401340
}
13411341
require.Len(t, got.Result.Value, maxValueLen)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy