From 512fdbf63444fb7478fb8d34fc1f4efd6ab5699b Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Sat, 1 Apr 2023 16:36:21 -0500 Subject: [PATCH] chore: debounce agent watch-metadata stream (#6940) --- coderd/workspaceagents.go | 13 ++++++++++++- go.mod | 1 + go.sum | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/coderd/workspaceagents.go b/coderd/workspaceagents.go index 49885bb9e5..3790edc4b4 100644 --- a/coderd/workspaceagents.go +++ b/coderd/workspaceagents.go @@ -6,6 +6,7 @@ import ( "database/sql" "encoding/json" "errors" + "flag" "fmt" "net" "net/http" @@ -18,6 +19,7 @@ import ( "sync/atomic" "time" + "github.com/bep/debounce" "github.com/go-chi/chi/v5" "github.com/google/uuid" "go.opentelemetry.io/otel/trace" @@ -1484,9 +1486,18 @@ 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) + if flag.Lookup("test.v") != nil { + pubsubDebounce = debounce.New(time.Millisecond * 100) + } + // Send metadata on updates. cancelSub, err := api.Pubsub.Subscribe(watchWorkspaceAgentMetadataChannel(workspaceAgent.ID), func(_ context.Context, _ []byte) { - sendMetadata(true) + pubsubDebounce(func() { + sendMetadata(true) + }) }) if err != nil { httpapi.InternalServerError(rw, err) diff --git a/go.mod b/go.mod index 9a774cf3b9..93d0775c1e 100644 --- a/go.mod +++ b/go.mod @@ -173,6 +173,7 @@ require ( require ( cloud.google.com/go/logging v1.6.1 // indirect + github.com/bep/debounce v1.2.1 // indirect github.com/dgraph-io/badger/v3 v3.2103.5 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/golang/glog v1.0.0 // indirect diff --git a/go.sum b/go.sum index 9604c0bbc2..e187c203da 100644 --- a/go.sum +++ b/go.sum @@ -270,6 +270,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY= +github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0= github.com/bep/godartsass v0.16.0 h1:nTpenrZBQjVSjLkCw3AgnYmBB2czauTJa4BLLv448qg= github.com/bep/godartsass v0.16.0/go.mod h1:6LvK9RftsXMxGfsA0LDV12AGc4Jylnu6NgHL+Q5/pE8= github.com/bep/golibsass v1.1.0 h1:pjtXr00IJZZaOdfryNa9wARTB3Q0BmxC3/V1KNcgyTw=