MCPcopy Index your code
hub / github.com/coder/coder / startCacheRefreshLoop

Method startCacheRefreshLoop

coderd/agentapi/api.go:333–350  ·  view source on GitHub ↗

startCacheRefreshLoop runs a background goroutine that periodically refreshes the cached workspace fields. This is primarily needed to handle prebuild claims where the owner_id and other fields change while the agent connection persists.

(ctx context.Context)

Source from the content-addressed store, hash-verified

331// the cached workspace fields. This is primarily needed to handle prebuild claims
332// where the owner_id and other fields change while the agent connection persists.
333func (a *API) startCacheRefreshLoop(ctx context.Context) {
334 // Refresh every 5 minutes. This provides a reasonable balance between:
335 // - Keeping cache fresh for prebuild claims and other workspace updates
336 // - Minimizing unnecessary database queries
337 ticker := a.opts.Clock.TickerFunc(ctx, workspaceCacheRefreshInterval, func() error {
338 a.refreshCachedWorkspace(ctx)
339 return nil
340 }, "cache_refresh")
341
342 // We need to wait on the ticker exiting.
343 _ = ticker.Wait()
344
345 a.opts.Log.Debug(ctx, "cache refresh loop exited, invalidating the workspace cache on agent API",
346 slog.F("workspace_id", a.cachedWorkspaceFields.identity.ID),
347 slog.F("owner_id", a.cachedWorkspaceFields.identity.OwnerUsername),
348 slog.F("name", a.cachedWorkspaceFields.identity.Name))
349 a.cachedWorkspaceFields.Clear()
350}
351
352func (a *API) publishWorkspaceUpdate(ctx context.Context, agentID uuid.UUID, kind wspubsub.WorkspaceEventKind) error {
353 a.opts.PublishWorkspaceUpdateFn(ctx, a.opts.OwnerID, wspubsub.WorkspaceEvent{

Callers 1

NewFunction · 0.95

Calls 3

WaitMethod · 0.65
ClearMethod · 0.45

Tested by

no test coverage detected