Skip to content

Commit 7e2ec9f

Browse files
committed
use 15s timeout and timer reset
1 parent 8039a4d commit 7e2ec9f

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

coderd/workspaceapps/stats.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,27 +348,31 @@ func (sc *StatsCollector) start() {
348348
}()
349349
sc.opts.Logger.Info(sc.ctx, "workspace app stats collector started")
350350

351-
ticker := time.NewTicker(sc.opts.ReportInterval)
352-
defer ticker.Stop()
351+
t := time.NewTimer(sc.opts.ReportInterval)
352+
defer t.Stop()
353353

354354
var reportFlushDone chan<- struct{}
355355
done := false
356356
for !done {
357357
select {
358358
case <-sc.ctx.Done():
359-
ticker.Stop()
359+
t.Stop()
360360
done = true
361-
case <-ticker.C:
361+
case <-t.C:
362362
case reportFlushDone = <-sc.opts.Flush:
363363
}
364364

365365
// Ensure we don't hold up this request for too long. Add a few
366366
// seconds to prevent very short intervals from causing a timeout.
367-
ctx, cancel := context.WithTimeout(context.Background(), sc.opts.ReportInterval+5*time.Second)
367+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
368368
//nolint:gocritic // Inserting app stats is a system function.
369369
_ = sc.flush(dbauthz.AsSystemRestricted(ctx))
370370
cancel()
371371

372+
if !done {
373+
t.Reset(sc.opts.ReportInterval)
374+
}
375+
372376
// For tests.
373377
if reportFlushDone != nil {
374378
reportFlushDone <- struct{}{}

0 commit comments

Comments
 (0)