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

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)

View File

@ -7,12 +7,13 @@ import (
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils/pl"
)
func newBufferedScrobbler(ds model.DataStore, s Scrobbler, service string) *bufferedScrobbler {
b := &bufferedScrobbler{ds: ds, wrapped: s, service: service}
b.wakeSignal = make(chan struct{}, 1)
go b.run()
go b.run(context.TODO())
return b
}
@ -49,15 +50,14 @@ func (b *bufferedScrobbler) sendWakeSignal() {
}
}
func (b *bufferedScrobbler) run() {
ctx := context.Background()
func (b *bufferedScrobbler) run(ctx context.Context) {
for {
if !b.processQueue(ctx) {
time.AfterFunc(5*time.Second, func() {
b.sendWakeSignal()
})
}
<-b.wakeSignal
<-pl.ReadOrDone(ctx, b.wakeSignal)
}
}