6
6
"database/sql"
7
7
"encoding/json"
8
8
"errors"
9
- "flag"
10
9
"fmt"
11
10
"io"
12
11
"net"
@@ -23,10 +22,9 @@ import (
23
22
24
23
"github.com/go-chi/chi/v5"
25
24
"github.com/google/uuid"
26
- "golang.org/x/exp/slices "
25
+ "golang.org/x/exp/maps "
27
26
"golang.org/x/mod/semver"
28
27
"golang.org/x/sync/errgroup"
29
- "golang.org/x/time/rate"
30
28
"golang.org/x/xerrors"
31
29
"nhooyr.io/websocket"
32
30
"tailscale.com/tailcfg"
@@ -39,7 +37,6 @@ import (
39
37
"github.com/coder/coder/v2/coderd/httpmw"
40
38
"github.com/coder/coder/v2/coderd/rbac"
41
39
"github.com/coder/coder/v2/coderd/util/ptr"
42
- "github.com/coder/coder/v2/coderd/util/slice"
43
40
"github.com/coder/coder/v2/codersdk"
44
41
"github.com/coder/coder/v2/codersdk/agentsdk"
45
42
"github.com/coder/coder/v2/tailnet"
@@ -1524,7 +1521,11 @@ func (api *API) workspaceAgentPostMetadata(rw http.ResponseWriter, r *http.Reque
1524
1521
key := chi .URLParam (r , "key" )
1525
1522
1526
1523
const (
1527
- maxValueLen = 32 << 10
1524
+ // maxValueLen is set to 2048 to stay under the 8000 byte Postgres
1525
+ // NOTIFY limit. Since both value and error can be set, the real
1526
+ // payload limit is 2 * 2048 * 4/3 <base64 expansion> = 5461 bytes + a few hundred bytes for JSON
1527
+ // syntax, key names, and metadata.
1528
+ maxValueLen = 2048
1528
1529
maxErrorLen = maxValueLen
1529
1530
)
1530
1531
@@ -1567,7 +1568,15 @@ func (api *API) workspaceAgentPostMetadata(rw http.ResponseWriter, r *http.Reque
1567
1568
slog .F ("value" , ellipse (datum .Value , 16 )),
1568
1569
)
1569
1570
1570
- err = api .Pubsub .Publish (watchWorkspaceAgentMetadataChannel (workspaceAgent .ID ), []byte (datum .Key ))
1571
+ // gob is used instead of JSON due to the potential large performance
1572
+ // overhead of agent metadata.
1573
+ datumJSON , err := json .Marshal (datum )
1574
+ if err != nil {
1575
+ httpapi .InternalServerError (rw , err )
1576
+ return
1577
+ }
1578
+
1579
+ err = api .Pubsub .Publish (watchWorkspaceAgentMetadataChannel (workspaceAgent .ID ), datumJSON )
1571
1580
if err != nil {
1572
1581
httpapi .InternalServerError (rw , err )
1573
1582
return
@@ -1593,7 +1602,42 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ
1593
1602
)
1594
1603
)
1595
1604
1596
- sendEvent , senderClosed , err := httpapi .ServerSentEventSender (rw , r )
1605
+ // We avoid channel-based synchronization here to avoid backpressure problems.
1606
+ var (
1607
+ metadataMapMu sync.Mutex
1608
+ metadataMap = make (map [string ]database.WorkspaceAgentMetadatum , 128 )
1609
+ // pendingChanges must only be mutated when metadataMapMu is held.
1610
+ pendingChanges atomic.Bool
1611
+ )
1612
+
1613
+ // Send metadata on updates, we must ensure subscription before sending
1614
+ // initial metadata to guarantee that events in-between are not missed.
1615
+ cancelSub , err := api .Pubsub .Subscribe (watchWorkspaceAgentMetadataChannel (workspaceAgent .ID ), func (_ context.Context , byt []byte ) {
1616
+ var update database.UpdateWorkspaceAgentMetadataParams
1617
+ err := json .Unmarshal (byt , & update )
1618
+ if err != nil {
1619
+ api .Logger .Error (ctx , "failed to unmarshal pubsub message" , slog .Error (err ))
1620
+ return
1621
+ }
1622
+
1623
+ log .Debug (ctx , "received metadata update" , "key" , update .Key )
1624
+
1625
+ metadataMapMu .Lock ()
1626
+ defer metadataMapMu .Unlock ()
1627
+ md := metadataMap [update .Key ]
1628
+ md .Value = update .Value
1629
+ md .Error = update .Error
1630
+ md .CollectedAt = update .CollectedAt
1631
+ metadataMap [update .Key ] = md
1632
+ pendingChanges .Store (true )
1633
+ })
1634
+ if err != nil {
1635
+ httpapi .InternalServerError (rw , err )
1636
+ return
1637
+ }
1638
+ defer cancelSub ()
1639
+
1640
+ sseSendEvent , sseSenderClosed , err := httpapi .ServerSentEventSender (rw , r )
1597
1641
if err != nil {
1598
1642
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
1599
1643
Message : "Internal error setting up server-sent events." ,
@@ -1603,97 +1647,57 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ
1603
1647
}
1604
1648
// Prevent handler from returning until the sender is closed.
1605
1649
defer func () {
1606
- <- senderClosed
1650
+ <- sseSenderClosed
1607
1651
}()
1608
1652
1609
- const refreshInterval = time .Second * 5
1610
- refreshTicker := time .NewTicker (refreshInterval )
1611
- defer refreshTicker .Stop ()
1653
+ // We send updates at most every second.
1654
+ const sendInterval = time .Second * 1
1655
+ sendTicker := time .NewTicker (sendInterval )
1656
+ defer sendTicker .Stop ()
1612
1657
1613
- var (
1614
- lastDBMetaMu sync.Mutex
1615
- lastDBMeta []database.WorkspaceAgentMetadatum
1616
- )
1617
-
1618
- sendMetadata := func (pull bool ) {
1619
- log .Debug (ctx , "sending metadata update" , "pull" , pull )
1620
- lastDBMetaMu .Lock ()
1621
- defer lastDBMetaMu .Unlock ()
1622
-
1623
- var err error
1624
- if pull {
1625
- // We always use the original Request context because it contains
1626
- // the RBAC actor.
1627
- lastDBMeta , err = api .Database .GetWorkspaceAgentMetadata (ctx , workspaceAgent .ID )
1628
- if err != nil {
1629
- _ = sendEvent (ctx , codersdk.ServerSentEvent {
1630
- Type : codersdk .ServerSentEventTypeError ,
1631
- Data : codersdk.Response {
1632
- Message : "Internal error getting metadata." ,
1633
- Detail : err .Error (),
1634
- },
1635
- })
1636
- return
1637
- }
1638
- slices .SortFunc (lastDBMeta , func (a , b database.WorkspaceAgentMetadatum ) int {
1639
- return slice .Ascending (a .Key , b .Key )
1640
- })
1641
-
1642
- // Avoid sending refresh if the client is about to get a
1643
- // fresh update.
1644
- refreshTicker .Reset (refreshInterval )
1645
- }
1646
-
1647
- _ = sendEvent (ctx , codersdk.ServerSentEvent {
1648
- Type : codersdk .ServerSentEventTypeData ,
1649
- Data : convertWorkspaceAgentMetadata (lastDBMeta ),
1650
- })
1651
- }
1652
-
1653
- // Note: we previously used a debounce here, but when the rate of metadata updates was too
1654
- // high the debounce would never fire.
1655
- //
1656
- // The rate-limit has its own caveat. If the agent sends a burst of metadata
1657
- // but then goes quiet, we will never pull the new metadata and the frontend
1658
- // will go stale until refresh. This would only happen if the agent was
1659
- // under extreme load. Under normal operations, the interval between metadata
1660
- // updates is constant so there is no burst phenomenon.
1661
- pubsubRatelimit := rate .NewLimiter (rate .Every (time .Second ), 2 )
1662
- if flag .Lookup ("test.v" ) != nil {
1663
- // We essentially disable the rate-limit in tests for determinism.
1664
- pubsubRatelimit = rate .NewLimiter (rate .Every (time .Second * 100 ), 100 )
1665
- }
1666
-
1667
- // Send metadata on updates, we must ensure subscription before sending
1668
- // initial metadata to guarantee that events in-between are not missed.
1669
- cancelSub , err := api .Pubsub .Subscribe (watchWorkspaceAgentMetadataChannel (workspaceAgent .ID ), func (_ context.Context , _ []byte ) {
1670
- allow := pubsubRatelimit .Allow ()
1671
- log .Debug (ctx , "received metadata update" , "allow" , allow )
1672
- if allow {
1673
- sendMetadata (true )
1674
- }
1675
- })
1658
+ // We always use the original Request context because it contains
1659
+ // the RBAC actor.
1660
+ md , err := api .Database .GetWorkspaceAgentMetadata (ctx , workspaceAgent .ID )
1676
1661
if err != nil {
1662
+ // If we can't successfully pull the initial metadata, pubsub
1663
+ // updates will be no-op so we may as well terminate the
1664
+ // connection early.
1677
1665
httpapi .InternalServerError (rw , err )
1678
1666
return
1679
1667
}
1680
- defer cancelSub ()
1668
+ for _ , datum := range md {
1669
+ metadataMap [datum .Key ] = datum
1670
+ }
1681
1671
1682
1672
// Send initial metadata.
1683
- sendMetadata (true )
1673
+
1674
+ var lastSend time.Time
1675
+ sendMetadata := func () {
1676
+ lastSend = time .Now ()
1677
+ metadataMapMu .Lock ()
1678
+ values := maps .Values (metadataMap )
1679
+ pendingChanges .Store (false )
1680
+ metadataMapMu .Unlock ()
1681
+ _ = sseSendEvent (ctx , codersdk.ServerSentEvent {
1682
+ Type : codersdk .ServerSentEventTypeData ,
1683
+ Data : convertWorkspaceAgentMetadata (values ),
1684
+ })
1685
+ }
1686
+
1687
+ sendMetadata ()
1684
1688
1685
1689
for {
1686
1690
select {
1687
- case <- senderClosed :
1691
+ case <- sendTicker .C :
1692
+ // We send an update even if there's no change every 5 seconds
1693
+ // to ensure that the frontend always has an accurate "Result.Age".
1694
+ if ! pendingChanges .Load () && time .Since (lastSend ) < time .Second * 5 {
1695
+ continue
1696
+ }
1697
+ sendMetadata ()
1698
+ case <- sseSenderClosed :
1688
1699
return
1689
- case <- refreshTicker .C :
1690
1700
}
1691
-
1692
- // Avoid spamming the DB with reads we know there are no updates. We want
1693
- // to continue sending updates to the frontend so that "Result.Age"
1694
- // is always accurate. This way, the frontend doesn't need to
1695
- // sync its own clock with the backend.
1696
- sendMetadata (false )
1697
1701
}
1698
1702
}
1699
1703
@@ -1717,6 +1721,10 @@ func convertWorkspaceAgentMetadata(db []database.WorkspaceAgentMetadatum) []code
1717
1721
},
1718
1722
})
1719
1723
}
1724
+ // Sorting prevents the metadata from jumping around in the frontend.
1725
+ sort .Slice (result , func (i , j int ) bool {
1726
+ return result [i ].Description .Key < result [j ].Description .Key
1727
+ })
1720
1728
return result
1721
1729
}
1722
1730
0 commit comments