From bf17df7f691d0e9ae0fb7fd98151bae6e346ce3e Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Wed, 5 Apr 2023 14:11:32 -0500 Subject: [PATCH 1/2] fix: reduce idle workspace queries --- coderd/apidoc/docs.go | 3 ++ coderd/apidoc/swagger.json | 3 ++ coderd/coderd.go | 2 +- coderd/workspaceagents.go | 15 +++++----- docs/api/agents.md | 59 -------------------------------------- scripts/build_docker.sh | 1 + 6 files changed, 15 insertions(+), 68 deletions(-) diff --git a/coderd/apidoc/docs.go b/coderd/apidoc/docs.go index 87391fe14e309..4ae9f4d575060 100644 --- a/coderd/apidoc/docs.go +++ b/coderd/apidoc/docs.go @@ -4391,6 +4391,9 @@ const docTemplate = `{ "$ref": "#/definitions/agentsdk.StatsResponse" } } + }, + "x-apidocgen": { + "skip": true } } }, diff --git a/coderd/apidoc/swagger.json b/coderd/apidoc/swagger.json index 2046aff0425b7..773efd97f311e 100644 --- a/coderd/apidoc/swagger.json +++ b/coderd/apidoc/swagger.json @@ -3857,6 +3857,9 @@ "$ref": "#/definitions/agentsdk.StatsResponse" } } + }, + "x-apidocgen": { + "skip": true } } }, diff --git a/coderd/coderd.go b/coderd/coderd.go index 5e95360c70c97..afc87b20bd73e 100644 --- a/coderd/coderd.go +++ b/coderd/coderd.go @@ -186,7 +186,7 @@ func New(options *Options) *API { panic("coderd: both AppHostname and AppHostnameRegex must be set or unset") } if options.AgentConnectionUpdateFrequency == 0 { - options.AgentConnectionUpdateFrequency = 3 * time.Second + options.AgentConnectionUpdateFrequency = 15 * time.Second } if options.AgentInactiveDisconnectTimeout == 0 { // Multiply the update by two to allow for some lag-time. diff --git a/coderd/workspaceagents.go b/coderd/workspaceagents.go index be7a93d511808..555020b15239d 100644 --- a/coderd/workspaceagents.go +++ b/coderd/workspaceagents.go @@ -37,6 +37,7 @@ import ( "github.com/coder/coder/coderd/httpmw" "github.com/coder/coder/coderd/rbac" "github.com/coder/coder/coderd/tracing" + "github.com/coder/coder/coderd/util/ptr" "github.com/coder/coder/codersdk" "github.com/coder/coder/codersdk/agentsdk" "github.com/coder/coder/tailnet" @@ -818,8 +819,9 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request // We use a custom heartbeat routine here instead of `httpapi.Heartbeat` // because we want to log the agent's last ping time. - lastPing := time.Now() // Since the agent initiated the request, assume it's alive. - var pingMu sync.Mutex + var lastPing atomic.Pointer[time.Time] + lastPing.Store(ptr.Ref(time.Now())) // Since the agent initiated the request, assume it's alive. + go pprof.Do(ctx, pprof.Labels("agent", workspaceAgent.ID.String()), func(ctx context.Context) { // TODO(mafredri): Is this too frequent? Use separate ping disconnect timeout? t := time.NewTicker(api.AgentConnectionUpdateFrequency) @@ -840,9 +842,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request if err != nil { return } - pingMu.Lock() - lastPing = time.Now() - pingMu.Unlock() + lastPing.Store(ptr.Ref(time.Now())) } }) @@ -950,9 +950,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request case <-ticker.C: } - pingMu.Lock() - lastPing := lastPing - pingMu.Unlock() + lastPing := *lastPing.Load() var connectionStatusChanged bool if time.Since(lastPing) > api.AgentInactiveDisconnectTimeout { @@ -1163,6 +1161,7 @@ func convertWorkspaceAgent(derpMap *tailcfg.DERPMap, coordinator tailnet.Coordin // @Param request body agentsdk.Stats true "Stats request" // @Success 200 {object} agentsdk.StatsResponse // @Router /workspaceagents/me/report-stats [post] +// @x-apidocgen {"skip": true} func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/docs/api/agents.md b/docs/api/agents.md index 79ad8a5029994..e4c548568dfbb 100644 --- a/docs/api/agents.md +++ b/docs/api/agents.md @@ -394,65 +394,6 @@ curl -X GET http://coder-server:8080/api/v2/workspaceagents/me/manifest \ To perform this operation, you must be authenticated. [Learn more](authentication.md). -## Submit workspace agent stats - -### Code samples - -```shell -# Example request using curl -curl -X POST http://coder-server:8080/api/v2/workspaceagents/me/report-stats \ - -H 'Content-Type: application/json' \ - -H 'Accept: application/json' \ - -H 'Coder-Session-Token: API_KEY' -``` - -`POST /workspaceagents/me/report-stats` - -> Body parameter - -```json -{ - "connection_count": 0, - "connection_median_latency_ms": 0, - "connections_by_proto": { - "property1": 0, - "property2": 0 - }, - "rx_bytes": 0, - "rx_packets": 0, - "session_count_jetbrains": 0, - "session_count_reconnecting_pty": 0, - "session_count_ssh": 0, - "session_count_vscode": 0, - "tx_bytes": 0, - "tx_packets": 0 -} -``` - -### Parameters - -| Name | In | Type | Required | Description | -| ------ | ---- | ------------------------------------------ | -------- | ------------- | -| `body` | body | [agentsdk.Stats](schemas.md#agentsdkstats) | true | Stats request | - -### Example responses - -> 200 Response - -```json -{ - "report_interval": 0 -} -``` - -### Responses - -| Status | Meaning | Description | Schema | -| ------ | ------------------------------------------------------- | ----------- | ---------------------------------------------------------- | -| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [agentsdk.StatsResponse](schemas.md#agentsdkstatsresponse) | - -To perform this operation, you must be authenticated. [Learn more](authentication.md). - ## Get workspace agent by ID ### Code samples diff --git a/scripts/build_docker.sh b/scripts/build_docker.sh index eb1726ced50a3..69c853e4f6337 100755 --- a/scripts/build_docker.sh +++ b/scripts/build_docker.sh @@ -142,6 +142,7 @@ docker build \ --build-arg "CODER_VERSION=$version" \ --no-cache \ --tag "$image_tag" \ + --tag "$CODER_IMAGE_BASE:latest" \ -f Dockerfile \ . 1>&2 From d824d09198f5ccb5c0b98f162af1c570156bf71c Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Wed, 5 Apr 2023 17:26:50 -0500 Subject: [PATCH 2/2] fixup! fix: reduce idle workspace queries --- scripts/build_docker.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/build_docker.sh b/scripts/build_docker.sh index 69c853e4f6337..eb1726ced50a3 100755 --- a/scripts/build_docker.sh +++ b/scripts/build_docker.sh @@ -142,7 +142,6 @@ docker build \ --build-arg "CODER_VERSION=$version" \ --no-cache \ --tag "$image_tag" \ - --tag "$CODER_IMAGE_BASE:latest" \ -f Dockerfile \ . 1>&2