🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Fix timer going awry
Deluan580e9ae4bdf1
diff --git a/core/artwork/cache_warmer.go b/core/artwork/cache_warmer.goindex 5e0e6029f..0cef277d5 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 waitingif !a.cache.Available(ctx) {diff --git a/core/media_streamer_Internal_test.go b/core/media_streamer_Internal_test.goindex 4e2331035..52ad82393 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.goindex f4d9c5832..588db41bb 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.goindex b91dd0ad4..bbb008fd6 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) boolAvailable(ctx context.Context) bool}@@ -46,7 +46,7 @@ func NewFileCache(name, cacheSize, cacheFolder string, maxItems int, getReader Rfc.cache = cachefc.disabled = cache == nil || err != nillog.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.CachegetReader ReadFuncdisabled bool- ready bool+ ready utils.AtomicBoolmutex *sync.RWMutex}-func (fc *fileCache) Ready(_ context.Context) bool {+func (fc *fileCache) Available(_ context.Context) bool {fc.mutex.RLock()defer fc.mutex.RUnlock()- return fc.ready-}--func (fc *fileCache) Available(ctx 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, errdiff --git a/utils/cache/file_caches_test.go b/utils/cache/file_caches_test.goindex 853a92f2c..67bbdf84d 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 readyfunc 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() {