chore: debounce agent watch-metadata stream (#6940)

This commit is contained in:
Ammar Bandukwala
2023-04-01 16:36:21 -05:00
committed by GitHub
parent e40b0778e9
commit 512fdbf634
3 changed files with 15 additions and 1 deletions

View File

@ -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)

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=