diff --git a/backend/cache/cache.go b/backend/cache/cache.go index 06644b1be..6e4f2973c 100644 --- a/backend/cache/cache.go +++ b/backend/cache/cache.go @@ -503,15 +503,19 @@ func NewFs(ctx context.Context, name, rootPath string, m configmap.Mapper) (fs.F } go func() { + // Wait on the timer and the stop channel together so that a stop + // signalled by StopBackgroundRunners is honoured immediately. + timer := time.NewTimer(time.Duration(f.opt.ChunkCleanInterval)) + defer timer.Stop() for { - time.Sleep(time.Duration(f.opt.ChunkCleanInterval)) select { case <-f.cleanupChan: fs.Infof(f, "stopping cleanup") return - default: + case <-timer.C: fs.Debugf(f, "starting cleanup") f.CleanUpCache(false) + timer.Reset(time.Duration(f.opt.ChunkCleanInterval)) } } }()