@@ -214,9 +214,8 @@ func (a *agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentM
214
214
if timeout == 0 {
215
215
timeout = md .Interval
216
216
}
217
- ctx , cancel := context .WithDeadline (ctx , time . Now (). Add (
217
+ ctx , cancel := context .WithTimeout (ctx ,
218
218
time .Duration (timeout )* time .Second ,
219
- ),
220
219
)
221
220
defer cancel ()
222
221
@@ -295,63 +294,65 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
295
294
a .logger .Error (ctx , "report metadata" , slog .Error (err ))
296
295
}
297
296
case <- baseTicker .C :
298
- if len (metadataResults ) > cap (metadataResults )/ 2 {
299
- // If we're backpressured on sending back results, we risk
300
- // runaway goroutine growth and/or overloading coderd. So,
301
- // we just skip the collection. Since we never update
302
- // the collections map, we'll retry the collection
303
- // on the next tick.
304
- a .logger .Debug (
305
- ctx , "metadata collection backpressured" ,
306
- slog .F ("queue_len" , len (metadataResults )),
307
- )
308
- continue
309
- }
297
+ break
298
+ }
299
+
300
+ if len (metadataResults ) > cap (metadataResults )/ 2 {
301
+ // If we're backpressured on sending back results, we risk
302
+ // runaway goroutine growth and/or overloading coderd. So,
303
+ // we just skip the collection. Since we never update
304
+ // the collections map, we'll retry the collection
305
+ // on the next tick.
306
+ a .logger .Debug (
307
+ ctx , "metadata collection backpressured" ,
308
+ slog .F ("queue_len" , len (metadataResults )),
309
+ )
310
+ continue
311
+ }
310
312
311
- manifest := a .manifest .Load ()
312
- if manifest == nil {
313
- continue
313
+ manifest := a .manifest .Load ()
314
+ if manifest == nil {
315
+ continue
316
+ }
317
+ // If the manifest changes (e.g. on agent reconnect) we need to
318
+ // purge old cache values to prevent lastCollectedAt from growing
319
+ // boundlessly.
320
+ for key := range lastCollectedAts {
321
+ if slices .IndexFunc (manifest .Metadata , func (md codersdk.WorkspaceAgentMetadataDescription ) bool {
322
+ return md .Key == key
323
+ }) < 0 {
324
+ delete (lastCollectedAts , key )
314
325
}
315
- // If the manifest changes (e.g. on agent reconnect) we need to
316
- // purge old cache values to prevent lastCollectedAt from growing
317
- // boundlessly.
318
- for key := range lastCollectedAts {
319
- if slices .IndexFunc (manifest .Metadata , func (md codersdk.WorkspaceAgentMetadataDescription ) bool {
320
- return md .Key == key
321
- }) < 0 {
322
- delete (lastCollectedAts , key )
326
+ }
327
+
328
+ // Spawn a goroutine for each metadata collection, and use a
329
+ // channel to synchronize the results and avoid both messy
330
+ // mutex logic and overloading the API.
331
+ for _ , md := range manifest .Metadata {
332
+ collectedAt , ok := lastCollectedAts [md .Key ]
333
+ if ok {
334
+ // If the interval is zero, we assume the user just wants
335
+ // a single collection at startup, not a spinning loop.
336
+ if md .Interval == 0 {
337
+ continue
338
+ }
339
+ if collectedAt .Add (
340
+ convertInterval (md .Interval ),
341
+ ).After (time .Now ()) {
342
+ continue
323
343
}
324
344
}
325
345
326
- // Spawn a goroutine for each metadata collection, and use a
327
- // channel to synchronize the results and avoid both messy
328
- // mutex logic and overloading the API.
329
- for _ , md := range manifest .Metadata {
330
- collectedAt , ok := lastCollectedAts [md .Key ]
331
- if ok {
332
- // If the interval is zero, we assume the user just wants
333
- // a single collection at startup, not a spinning loop.
334
- if md .Interval == 0 {
335
- continue
336
- }
337
- if collectedAt .Add (
338
- convertInterval (md .Interval ),
339
- ).After (time .Now ()) {
340
- continue
341
- }
346
+ go func (md codersdk.WorkspaceAgentMetadataDescription ) {
347
+ select {
348
+ case <- ctx .Done ():
349
+ return
350
+ case metadataResults <- metadataResultAndKey {
351
+ key : md .Key ,
352
+ result : a .collectMetadata (ctx , md ),
353
+ }:
342
354
}
343
-
344
- go func (md codersdk.WorkspaceAgentMetadataDescription ) {
345
- select {
346
- case <- ctx .Done ():
347
- return
348
- case metadataResults <- metadataResultAndKey {
349
- key : md .Key ,
350
- result : a .collectMetadata (ctx , md ),
351
- }:
352
- }
353
- }(md )
354
- }
355
+ }(md )
355
356
}
356
357
}
357
358
}
@@ -1077,7 +1078,7 @@ func (a *agent) init(ctx context.Context) {
1077
1078
}
1078
1079
1079
1080
// createCommand processes raw command input with OpenSSH-like behavior.
1080
- // If the rawScript provided is empty, it will default to the users shell.
1081
+ // If the createCommand provided is empty, it will default to the users shell.
1081
1082
// This injects environment variables specified by the user at launch too.
1082
1083
func (a * agent ) createCommand (ctx context.Context , script string , env []string ) (* exec.Cmd , error ) {
1083
1084
currentUser , err := user .Current ()
0 commit comments