fix(server): memory leak in cache warmer (#4095)
* Prevent cache warmer memory leak when cache disabled * refactor(tests): replace disabledCache with mockFileCache in CacheWarmer tests Signed-off-by: Deluan <deluan@navidrome.org> * test(cache): enhance CacheWarmer tests for initialization, buffer management, and error handling Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Vendored
+10
@@ -54,6 +54,9 @@ type FileCache interface {
|
||||
|
||||
// Available checks if the cache is available
|
||||
Available(ctx context.Context) bool
|
||||
|
||||
// Disabled reports if the cache has been permanently disabled
|
||||
Disabled(ctx context.Context) bool
|
||||
}
|
||||
|
||||
// NewFileCache creates a new FileCache. This function initializes the cache and starts it in the background.
|
||||
@@ -119,6 +122,13 @@ func (fc *fileCache) Available(_ context.Context) bool {
|
||||
return fc.ready.Load() && !fc.disabled
|
||||
}
|
||||
|
||||
func (fc *fileCache) Disabled(_ context.Context) bool {
|
||||
fc.mutex.RLock()
|
||||
defer fc.mutex.RUnlock()
|
||||
|
||||
return 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)
|
||||
|
||||
Vendored
+7
@@ -50,6 +50,13 @@ var _ = Describe("File Caches", func() {
|
||||
Expect(fc.cache).To(BeNil())
|
||||
Expect(fc.disabled).To(BeTrue())
|
||||
})
|
||||
|
||||
It("reports when cache is disabled", func() {
|
||||
fc := callNewFileCache("test", "0", "test", 0, nil)
|
||||
Expect(fc.Disabled(context.Background())).To(BeTrue())
|
||||
fc = callNewFileCache("test", "1KB", "test", 0, nil)
|
||||
Expect(fc.Disabled(context.Background())).To(BeFalse())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("FileCache", func() {
|
||||
|
||||
Reference in New Issue
Block a user