fix: reduce idle workspace queries (#7022)

This commit is contained in:
Colin Adler
2023-04-05 20:58:54 -05:00
committed by GitHub
parent 63f9ef2480
commit a32951c46a
5 changed files with 14 additions and 68 deletions

3
coderd/apidoc/docs.go generated
View File

@ -4391,6 +4391,9 @@ const docTemplate = `{
"$ref": "#/definitions/agentsdk.StatsResponse"
}
}
},
"x-apidocgen": {
"skip": true
}
}
},

View File

@ -3857,6 +3857,9 @@
"$ref": "#/definitions/agentsdk.StatsResponse"
}
}
},
"x-apidocgen": {
"skip": true
}
}
},

View File

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

View File

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