diff --git a/core/artwork/cache_warmer.go b/core/artwork/cache_warmer.go index 5e0e6029..0cef277d 100644 --- a/core/artwork/cache_warmer.go +++ b/core/artwork/cache_warmer.go @@ -65,10 +65,11 @@ func (a *cacheWarmer) sendWakeSignal() { func (a *cacheWarmer) run(ctx context.Context) { for { - time.AfterFunc(10*time.Second, func() { + t := time.AfterFunc(10*time.Second, func() { a.sendWakeSignal() }) <-a.wakeSignal + t.Stop() // If cache not available, keep waiting if !a.cache.Available(ctx) { diff --git a/core/media_streamer_Internal_test.go b/core/media_streamer_Internal_test.go index 4e233103..52ad8239 100644 --- a/core/media_streamer_Internal_test.go +++ b/core/media_streamer_Internal_test.go @@ -27,7 +27,7 @@ var _ = Describe("MediaStreamer", func() { {ID: "123", Path: "tests/fixtures/test.mp3", Suffix: "mp3", BitRate: 128, Duration: 257.0}, }) testCache := GetTranscodingCache() - Eventually(func() bool { return testCache.Ready(context.TODO()) }).Should(BeTrue()) + Eventually(func() bool { return testCache.Available(context.TODO()) }).Should(BeTrue()) }) AfterEach(func() { _ = os.RemoveAll(conf.Server.DataFolder) diff --git a/core/media_streamer_test.go b/core/media_streamer_test.go index f4d9c583..588db41b 100644 --- a/core/media_streamer_test.go +++ b/core/media_streamer_test.go @@ -30,7 +30,7 @@ var _ = Describe("MediaStreamer", func() { {ID: "123", Path: "tests/fixtures/test.mp3", Suffix: "mp3", BitRate: 128, Duration: 257.0}, }) testCache := core.GetTranscodingCache() - Eventually(func() bool { return testCache.Ready(context.TODO()) }).Should(BeTrue()) + Eventually(func() bool { return testCache.Available(context.TODO()) }).Should(BeTrue()) streamer = core.NewMediaStreamer(ds, ffmpeg, testCache) }) AfterEach(func() { diff --git a/utils/cache/file_caches.go b/utils/cache/file_caches.go index b91dd0ad..bbb008fd 100644 --- a/utils/cache/file_caches.go +++ b/utils/cache/file_caches.go @@ -14,6 +14,7 @@ import ( "github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/log" + "github.com/navidrome/navidrome/utils" ) type Item interface { @@ -24,7 +25,6 @@ type ReadFunc func(ctx context.Context, item Item) (io.Reader, error) type FileCache interface { Get(ctx context.Context, item Item) (*CachedStream, error) - Ready(ctx context.Context) bool Available(ctx context.Context) bool } @@ -46,7 +46,7 @@ func NewFileCache(name, cacheSize, cacheFolder string, maxItems int, getReader R fc.cache = cache fc.disabled = cache == nil || err != nil log.Info("Finished initializing cache", "cache", fc.name, "maxSize", fc.cacheSize, "elapsedTime", time.Since(start)) - fc.ready = true + fc.ready.Set(true) if err != nil { log.Error(fmt.Sprintf("Cache %s will be DISABLED due to previous errors", "name"), fc.name, err) } @@ -66,29 +66,20 @@ type fileCache struct { cache fscache.Cache getReader ReadFunc disabled bool - ready bool + ready utils.AtomicBool mutex *sync.RWMutex } -func (fc *fileCache) Ready(_ context.Context) bool { - fc.mutex.RLock() - defer fc.mutex.RUnlock() - return fc.ready -} - -func (fc *fileCache) Available(ctx context.Context) bool { +func (fc *fileCache) Available(_ context.Context) bool { fc.mutex.RLock() defer fc.mutex.RUnlock() - if !fc.ready { - log.Debug(ctx, "Cache not initialized yet", "cache", fc.name) - } - - return fc.ready && !fc.disabled + return fc.ready.Get() && !fc.disabled } func (fc *fileCache) invalidate(ctx context.Context, key string) error { if !fc.Available(ctx) { + log.Debug(ctx, "Cache not initialized yet. Cannot invalidate key", "cache", fc.name, "key", key) return nil } if !fc.cache.Exists(key) { @@ -99,6 +90,7 @@ func (fc *fileCache) invalidate(ctx context.Context, key string) error { func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) { if !fc.Available(ctx) { + log.Debug(ctx, "Cache not initialized yet. Reading data directly from reader", "cache", fc.name) reader, err := fc.getReader(ctx, arg) if err != nil { return nil, err diff --git a/utils/cache/file_caches_test.go b/utils/cache/file_caches_test.go index 853a92f2..67bbdf84 100644 --- a/utils/cache/file_caches_test.go +++ b/utils/cache/file_caches_test.go @@ -16,9 +16,9 @@ import ( // Call NewFileCache and wait for it to be ready func callNewFileCache(name, cacheSize, cacheFolder string, maxItems int, getReader ReadFunc) *fileCache { - fc := NewFileCache(name, cacheSize, cacheFolder, maxItems, getReader) - Eventually(func() bool { return fc.Ready(context.Background()) }).Should(BeTrue()) - return fc.(*fileCache) + fc := NewFileCache(name, cacheSize, cacheFolder, maxItems, getReader).(*fileCache) + Eventually(func() bool { return fc.ready.Get() }).Should(BeTrue()) + return fc } var _ = Describe("File Caches", func() {