Skip to content

Commit a32951c

Browse files
authored
fix: reduce idle workspace queries (coder#7022)
1 parent 63f9ef2 commit a32951c

File tree

5 files changed

+14
-68
lines changed

5 files changed

+14
-68
lines changed

coderd/apidoc/docs.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func New(options *Options) *API {
186186
panic("coderd: both AppHostname and AppHostnameRegex must be set or unset")
187187
}
188188
if options.AgentConnectionUpdateFrequency == 0 {
189-
options.AgentConnectionUpdateFrequency = 3 * time.Second
189+
options.AgentConnectionUpdateFrequency = 15 * time.Second
190190
}
191191
if options.AgentInactiveDisconnectTimeout == 0 {
192192
// Multiply the update by two to allow for some lag-time.

coderd/workspaceagents.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/coder/coder/coderd/httpmw"
3838
"github.com/coder/coder/coderd/rbac"
3939
"github.com/coder/coder/coderd/tracing"
40+
"github.com/coder/coder/coderd/util/ptr"
4041
"github.com/coder/coder/codersdk"
4142
"github.com/coder/coder/codersdk/agentsdk"
4243
"github.com/coder/coder/tailnet"
@@ -818,8 +819,9 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
818819

819820
// We use a custom heartbeat routine here instead of `httpapi.Heartbeat`
820821
// because we want to log the agent's last ping time.
821-
lastPing := time.Now() // Since the agent initiated the request, assume it's alive.
822-
var pingMu sync.Mutex
822+
var lastPing atomic.Pointer[time.Time]
823+
lastPing.Store(ptr.Ref(time.Now())) // Since the agent initiated the request, assume it's alive.
824+
823825
go pprof.Do(ctx, pprof.Labels("agent", workspaceAgent.ID.String()), func(ctx context.Context) {
824826
// TODO(mafredri): Is this too frequent? Use separate ping disconnect timeout?
825827
t := time.NewTicker(api.AgentConnectionUpdateFrequency)
@@ -840,9 +842,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
840842
if err != nil {
841843
return
842844
}
843-
pingMu.Lock()
844-
lastPing = time.Now()
845-
pingMu.Unlock()
845+
lastPing.Store(ptr.Ref(time.Now()))
846846
}
847847
})
848848

@@ -950,9 +950,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
950950
case <-ticker.C:
951951
}
952952

953-
pingMu.Lock()
954-
lastPing := lastPing
955-
pingMu.Unlock()
953+
lastPing := *lastPing.Load()
956954

957955
var connectionStatusChanged bool
958956
if time.Since(lastPing) > api.AgentInactiveDisconnectTimeout {
@@ -1163,6 +1161,7 @@ func convertWorkspaceAgent(derpMap *tailcfg.DERPMap, coordinator tailnet.Coordin
11631161
// @Param request body agentsdk.Stats true "Stats request"
11641162
// @Success 200 {object} agentsdk.StatsResponse
11651163
// @Router /workspaceagents/me/report-stats [post]
1164+
// @x-apidocgen {"skip": true}
11661165
func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Request) {
11671166
ctx := r.Context()
11681167

docs/api/agents.md

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -394,65 +394,6 @@ curl -X GET http://coder-server:8080/api/v2/workspaceagents/me/manifest \
394394

395395
To perform this operation, you must be authenticated. [Learn more](authentication.md).
396396

397-
## Submit workspace agent stats
398-
399-
### Code samples
400-
401-
```shell
402-
# Example request using curl
403-
curl -X POST http://coder-server:8080/api/v2/workspaceagents/me/report-stats \
404-
-H 'Content-Type: application/json' \
405-
-H 'Accept: application/json' \
406-
-H 'Coder-Session-Token: API_KEY'
407-
```
408-
409-
`POST /workspaceagents/me/report-stats`
410-
411-
> Body parameter
412-
413-
```json
414-
{
415-
"connection_count": 0,
416-
"connection_median_latency_ms": 0,
417-
"connections_by_proto": {
418-
"property1": 0,
419-
"property2": 0
420-
},
421-
"rx_bytes": 0,
422-
"rx_packets": 0,
423-
"session_count_jetbrains": 0,
424-
"session_count_reconnecting_pty": 0,
425-
"session_count_ssh": 0,
426-
"session_count_vscode": 0,
427-
"tx_bytes": 0,
428-
"tx_packets": 0
429-
}
430-
```
431-
432-
### Parameters
433-
434-
| Name | In | Type | Required | Description |
435-
| ------ | ---- | ------------------------------------------ | -------- | ------------- |
436-
| `body` | body | [agentsdk.Stats](schemas.md#agentsdkstats) | true | Stats request |
437-
438-
### Example responses
439-
440-
> 200 Response
441-
442-
```json
443-
{
444-
"report_interval": 0
445-
}
446-
```
447-
448-
### Responses
449-
450-
| Status | Meaning | Description | Schema |
451-
| ------ | ------------------------------------------------------- | ----------- | ---------------------------------------------------------- |
452-
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [agentsdk.StatsResponse](schemas.md#agentsdkstatsresponse) |
453-
454-
To perform this operation, you must be authenticated. [Learn more](authentication.md).
455-
456397
## Get workspace agent by ID
457398

458399
### Code samples

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