Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit77f9c6a

Browse files
committed
remove unnecessary calls to GetWorkspaceAgentByID for stats api calls
Signed-off-by: Callum Styan <callumstyan@gmail.com>
1 parent54497f4 commit77f9c6a

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

‎coderd/agentapi/api.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type API struct {
5454
*SubAgentAPI
5555
*tailnet.DRPCService
5656

57+
cachedWorkspace database.Workspace
58+
5759
mu sync.Mutex
5860
}
5961

@@ -92,14 +94,15 @@ type Options struct {
9294
UpdateAgentMetricsFnfunc(ctx context.Context,labels prometheusmetrics.AgentMetricLabels,metrics []*agentproto.Stats_Metric)
9395
}
9496

95-
funcNew(optsOptions)*API {
97+
funcNew(optsOptions,workspace database.Workspace)*API {
9698
ifopts.Clock==nil {
9799
opts.Clock=quartz.NewReal()
98100
}
99101

100102
api:=&API{
101-
opts:opts,
102-
mu: sync.Mutex{},
103+
opts:opts,
104+
cachedWorkspace:workspace,
105+
mu: sync.Mutex{},
103106
}
104107

105108
api.ManifestAPI=&ManifestAPI{
@@ -139,6 +142,7 @@ func New(opts Options) *API {
139142

140143
api.StatsAPI=&StatsAPI{
141144
AgentFn:api.agent,
145+
WorkspaceFn:api.workspace,
142146
Database:opts.Database,
143147
Log:opts.Log,
144148
StatsReporter:opts.StatsReporter,
@@ -254,6 +258,10 @@ func (a *API) agent(ctx context.Context) (database.WorkspaceAgent, error) {
254258
returnagent,nil
255259
}
256260

261+
func (a*API)workspace() (database.Workspace,error) {
262+
returna.cachedWorkspace,nil
263+
}
264+
257265
func (a*API)publishWorkspaceUpdate(ctx context.Context,agent*database.WorkspaceAgent,kind wspubsub.WorkspaceEventKind)error {
258266
a.opts.PublishWorkspaceUpdateFn(ctx,a.opts.OwnerID, wspubsub.WorkspaceEvent{
259267
Kind:kind,

‎coderd/agentapi/stats.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
typeStatsAPIstruct {
1919
AgentFnfunc(context.Context) (database.WorkspaceAgent,error)
20+
WorkspaceFnfunc() (database.Workspace,error)
2021
Database database.Store
2122
Log slog.Logger
2223
StatsReporter*workspacestats.Reporter
@@ -46,11 +47,12 @@ func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsR
4647
iferr!=nil {
4748
returnnil,err
4849
}
49-
getWorkspaceAgentByIDRow,err:=a.Database.GetWorkspaceByAgentID(ctx,workspaceAgent.ID)
50+
// Construct workspace from cached fields to avoid DB query
51+
workspace,err:=a.WorkspaceFn()
5052
iferr!=nil {
51-
returnnil,xerrors.Errorf("get workspace by agent ID %q: %w",workspaceAgent.ID,err)
53+
returnnil,err
5254
}
53-
workspace:=getWorkspaceAgentByIDRow
55+
5456
a.Log.Debug(ctx,"read stats report",
5557
slog.F("interval",a.AgentStatsRefreshInterval),
5658
slog.F("workspace_id",workspace.ID),
@@ -72,7 +74,7 @@ func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsR
7274
a.now(),
7375
workspace,
7476
workspaceAgent,
75-
getWorkspaceAgentByIDRow.TemplateName,
77+
workspace.TemplateName,
7678
req.Stats,
7779
false,
7880
)

‎coderd/agentapi/stats_test.go‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ func TestUpdateStates(t *testing.T) {
111111
AgentFn:func(context.Context) (database.WorkspaceAgent,error) {
112112
returnagent,nil
113113
},
114+
WorkspaceFn:func() (database.Workspace,error) {
115+
returnworkspace,nil
116+
},
114117
Database:dbM,
115118
StatsReporter:workspacestats.NewReporter(workspacestats.ReporterOptions{
116119
Database:dbM,
@@ -136,9 +139,6 @@ func TestUpdateStates(t *testing.T) {
136139
}
137140
deferwut.Close()
138141

139-
// Workspace gets fetched.
140-
dbM.EXPECT().GetWorkspaceByAgentID(gomock.Any(),agent.ID).Return(workspace,nil)
141-
142142
// We expect an activity bump because ConnectionCount > 0.
143143
dbM.EXPECT().ActivityBumpWorkspace(gomock.Any(), database.ActivityBumpWorkspaceParams{
144144
WorkspaceID:workspace.ID,
@@ -223,6 +223,9 @@ func TestUpdateStates(t *testing.T) {
223223
AgentFn:func(context.Context) (database.WorkspaceAgent,error) {
224224
returnagent,nil
225225
},
226+
WorkspaceFn:func() (database.Workspace,error) {
227+
returnworkspace,nil
228+
},
226229
Database:dbM,
227230
StatsReporter:workspacestats.NewReporter(workspacestats.ReporterOptions{
228231
Database:dbM,
@@ -239,9 +242,6 @@ func TestUpdateStates(t *testing.T) {
239242
},
240243
}
241244

242-
// Workspace gets fetched.
243-
dbM.EXPECT().GetWorkspaceByAgentID(gomock.Any(),agent.ID).Return(workspace,nil)
244-
245245
_,err:=api.UpdateStats(context.Background(),req)
246246
require.NoError(t,err)
247247
})
@@ -260,6 +260,9 @@ func TestUpdateStates(t *testing.T) {
260260
AgentFn:func(context.Context) (database.WorkspaceAgent,error) {
261261
returnagent,nil
262262
},
263+
WorkspaceFn:func() (database.Workspace,error) {
264+
returnworkspace,nil
265+
},
263266
Database:dbM,
264267
StatsReporter:workspacestats.NewReporter(workspacestats.ReporterOptions{
265268
Database:dbM,
@@ -337,6 +340,9 @@ func TestUpdateStates(t *testing.T) {
337340
AgentFn:func(context.Context) (database.WorkspaceAgent,error) {
338341
returnagent,nil
339342
},
343+
WorkspaceFn:func() (database.Workspace,error) {
344+
returnworkspace,nil
345+
},
340346
Database:dbM,
341347
StatsReporter:workspacestats.NewReporter(workspacestats.ReporterOptions{
342348
Database:dbM,
@@ -362,9 +368,6 @@ func TestUpdateStates(t *testing.T) {
362368
}
363369
deferwut.Close()
364370

365-
// Workspace gets fetched.
366-
dbM.EXPECT().GetWorkspaceByAgentID(gomock.Any(),agent.ID).Return(workspace,nil)
367-
368371
// We expect an activity bump because ConnectionCount > 0. However, the
369372
// next autostart time will be set on the bump.
370373
dbM.EXPECT().ActivityBumpWorkspace(gomock.Any(), database.ActivityBumpWorkspaceParams{
@@ -451,6 +454,9 @@ func TestUpdateStates(t *testing.T) {
451454
AgentFn:func(context.Context) (database.WorkspaceAgent,error) {
452455
returnagent,nil
453456
},
457+
WorkspaceFn:func() (database.Workspace,error) {
458+
returnworkspace,nil
459+
},
454460
Database:dbM,
455461
StatsReporter:workspacestats.NewReporter(workspacestats.ReporterOptions{
456462
Database:dbM,
@@ -478,9 +484,6 @@ func TestUpdateStates(t *testing.T) {
478484
},
479485
}
480486

481-
// Workspace gets fetched.
482-
dbM.EXPECT().GetWorkspaceByAgentID(gomock.Any(),agent.ID).Return(workspace,nil)
483-
484487
// We expect an activity bump because ConnectionCount > 0.
485488
dbM.EXPECT().ActivityBumpWorkspace(gomock.Any(), database.ActivityBumpWorkspaceParams{
486489
WorkspaceID:workspace.ID,

‎coderd/workspaceagentsrpc.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (api *API) workspaceAgentRPC(rw http.ResponseWriter, r *http.Request) {
158158

159159
// Optional:
160160
UpdateAgentMetricsFn:api.UpdateAgentMetrics,
161-
})
161+
},workspace)
162162

163163
streamID:= tailnet.StreamID{
164164
Name:fmt.Sprintf("%s-%s-%s",workspace.OwnerUsername,workspace.Name,workspaceAgent.Name),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp