Don't wake CacheWarmer every 10 seconds, let it sleep :)

This commit is contained in:
Deluan
2023-01-18 19:31:15 -05:00
parent 136d5f9a83
commit 7fc964aec5
2 changed files with 22 additions and 9 deletions
+18 -5
View File
@@ -65,11 +65,10 @@ func (a *cacheWarmer) sendWakeSignal() {
func (a *cacheWarmer) run(ctx context.Context) {
for {
t := time.AfterFunc(10*time.Second, func() {
a.sendWakeSignal()
})
<-a.wakeSignal
t.Stop()
a.waitSignal(ctx, 10*time.Second)
if ctx.Err() != nil {
break
}
// If cache not available, keep waiting
if !a.cache.Available(ctx) {
@@ -95,6 +94,20 @@ func (a *cacheWarmer) run(ctx context.Context) {
}
}
func (a *cacheWarmer) waitSignal(ctx context.Context, timeout time.Duration) {
var to <-chan time.Time
if !a.cache.Available(ctx) {
tmr := time.NewTimer(timeout)
defer tmr.Stop()
to = tmr.C
}
select {
case <-to:
case <-a.wakeSignal:
case <-ctx.Done():
}
}
func (a *cacheWarmer) processBatch(ctx context.Context, batch []string) {
log.Trace(ctx, "PreCaching a new batch of artwork", "batchSize", len(batch))
input := pl.FromSlice(ctx, batch)