mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-14 13:27:09 +02:00
refactor(artwork): defer animated GIF conversion via a loader error
Replace cache.Transient with an artwork-local *deferredAnimation error returned by resizedItem.Reader. The file cache already stores nothing when its loader fails, so the generic cache no longer needs a transient mode. The error carries the GIF bytes and the static stand-in, replacing the deferAnimated callback. The cache's loader-error path now cancels the entry writer, so readers that joined the miss fail with the loader's error instead of reading an empty entry, and the removal no longer blocks on them. The background job resizes the bytes it already holds instead of re-reading them, orOriginal centralizes the serve-the-original fallback, and warmCache shares the store-and-drain step with the worker's precache.
This commit is contained in:
parent
bc16cafb91
commit
c0da9fcd93
7 changed files with 73 additions and 146 deletions
43
utils/cache/file_caches.go
vendored
43
utils/cache/file_caches.go
vendored
|
|
@ -25,18 +25,6 @@ type Item interface {
|
|||
Key() string
|
||||
}
|
||||
|
||||
// Transient wraps a ReadFunc result that Get must serve but never store.
|
||||
func Transient(rc io.ReadCloser) io.ReadCloser { return transientReader{rc} }
|
||||
|
||||
type transientReader struct{ io.ReadCloser }
|
||||
|
||||
func unwrapTransient(r io.Reader) (io.Reader, bool) {
|
||||
if t, ok := r.(transientReader); ok {
|
||||
return t.ReadCloser, true
|
||||
}
|
||||
return r, false
|
||||
}
|
||||
|
||||
// ReadFunc is a function that retrieves the data to be cached. It receives the Item to be cached and returns
|
||||
// an io.Reader with the data and an error.
|
||||
type ReadFunc func(ctx context.Context, item Item) (io.Reader, error)
|
||||
|
|
@ -167,8 +155,7 @@ func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reader, transient := unwrapTransient(reader)
|
||||
return &CachedStream{Reader: reader, Transient: transient}, nil
|
||||
return &CachedStream{Reader: reader}, nil
|
||||
}
|
||||
|
||||
key := arg.Key()
|
||||
|
|
@ -191,14 +178,15 @@ func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) {
|
|||
reader, err := fc.getReader(ctx, arg)
|
||||
if err != nil {
|
||||
_ = r.Close()
|
||||
_ = w.Close()
|
||||
// Cancelling fails readers that joined meanwhile with err, and keeps the removal from blocking on them.
|
||||
if cw, ok := w.(interface{ CloseWithError(error) error }); ok {
|
||||
_ = cw.CloseWithError(err)
|
||||
} else {
|
||||
_ = w.Close()
|
||||
}
|
||||
_ = fc.invalidate(ctx, key)
|
||||
return nil, err
|
||||
}
|
||||
if reader, transient := unwrapTransient(reader); transient {
|
||||
fc.discard(ctx, key, r, w)
|
||||
return &CachedStream{Reader: reader, Transient: true}, nil
|
||||
}
|
||||
go func() {
|
||||
if err := fc.copyAndClose(ctx, key, w, reader); err != nil {
|
||||
log.Debug(ctx, "Error storing file in cache", "cache", fc.name, "key", key, err)
|
||||
|
|
@ -230,27 +218,12 @@ func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) {
|
|||
return &CachedStream{Reader: r, Cached: cached}, nil
|
||||
}
|
||||
|
||||
// discard drops a miss entry that will never be written. Cancelling first makes the removal
|
||||
// non-blocking, so the key is free when Get returns; readers that joined meanwhile fail.
|
||||
func (fc *fileCache) discard(ctx context.Context, key string, r io.Closer, w io.WriteCloser) {
|
||||
_ = r.Close()
|
||||
if cw, ok := w.(interface{ CloseWithError(error) error }); ok {
|
||||
_ = cw.CloseWithError(errTransientEntry)
|
||||
} else {
|
||||
_ = w.Close()
|
||||
}
|
||||
_ = fc.invalidate(ctx, key)
|
||||
}
|
||||
|
||||
var errTransientEntry = errors.New("cache entry was served without being stored")
|
||||
|
||||
// CachedStream is a wrapper around an io.ReadCloser that allows reading from a cache.
|
||||
type CachedStream struct {
|
||||
io.Reader
|
||||
io.Seeker
|
||||
io.Closer
|
||||
Cached bool
|
||||
Transient bool
|
||||
Cached bool
|
||||
}
|
||||
|
||||
func (s *CachedStream) Close() error {
|
||||
|
|
|
|||
71
utils/cache/file_caches_test.go
vendored
71
utils/cache/file_caches_test.go
vendored
|
|
@ -106,65 +106,28 @@ var _ = Describe("File Caches", func() {
|
|||
Expect(called).To(BeTrue())
|
||||
})
|
||||
|
||||
It("serves a transient result without storing it", func() {
|
||||
var calls atomic.Int32
|
||||
It("fails a reader that joined a miss whose loader failed, without blocking the removal", func() {
|
||||
entered, release := make(chan struct{}), make(chan struct{})
|
||||
fc := callNewFileCache("test", "1KB", "test", 0, func(ctx context.Context, arg Item) (io.Reader, error) {
|
||||
calls.Add(1)
|
||||
return Transient(io.NopCloser(strings.NewReader("stand-in"))), nil
|
||||
close(entered)
|
||||
<-release
|
||||
return nil, errors.New("loader failed")
|
||||
})
|
||||
firstErr := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := fc.Get(context.Background(), &testArg{"k"})
|
||||
firstErr <- err
|
||||
}()
|
||||
<-entered
|
||||
|
||||
for range 2 {
|
||||
s, err := fc.Get(context.Background(), &testArg{"transient"})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(s.Transient).To(BeTrue())
|
||||
Expect(s.Cached).To(BeFalse())
|
||||
Expect(io.ReadAll(s)).To(Equal([]byte("stand-in")))
|
||||
Expect(s.Close()).To(Succeed())
|
||||
}
|
||||
Expect(calls.Load()).To(Equal(int32(2)))
|
||||
|
||||
dataPath := fcSpreadFS(fc).KeyMapper((&testArg{"transient"}).Key())
|
||||
_, statErr := os.Stat(dataPath + ".complete")
|
||||
Expect(os.IsNotExist(statErr)).To(BeTrue())
|
||||
})
|
||||
|
||||
It("stores the next result once a transient one was served", func() {
|
||||
var calls atomic.Int32
|
||||
fc := callNewFileCache("test", "1KB", "test", 0, func(ctx context.Context, arg Item) (io.Reader, error) {
|
||||
if calls.Add(1) == 1 {
|
||||
return Transient(io.NopCloser(strings.NewReader("stand-in"))), nil
|
||||
}
|
||||
return strings.NewReader("real"), nil
|
||||
})
|
||||
|
||||
s, err := fc.Get(context.Background(), &testArg{"k"})
|
||||
joined, err := fc.Get(context.Background(), &testArg{"k"})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(io.ReadAll(s)).To(Equal([]byte("stand-in")))
|
||||
_ = s.Close()
|
||||
close(release)
|
||||
Eventually(firstErr).Should(Receive(MatchError("loader failed")))
|
||||
|
||||
// The transient entry is gone when Get returns, so this is a MISS that stores.
|
||||
s, err = fc.Get(context.Background(), &testArg{"k"})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(s.Transient).To(BeFalse())
|
||||
Expect(io.ReadAll(s)).To(Equal([]byte("real")))
|
||||
_ = s.Close()
|
||||
|
||||
Eventually(func() bool {
|
||||
s, _ := fc.Get(context.Background(), &testArg{"k"})
|
||||
defer s.Close()
|
||||
return s.Cached
|
||||
}).Should(BeTrue())
|
||||
Expect(calls.Load()).To(Equal(int32(2)))
|
||||
})
|
||||
|
||||
It("reports a transient result when the cache is disabled", func() {
|
||||
fc := callNewFileCache("test", "0", "test", 0, func(ctx context.Context, arg Item) (io.Reader, error) {
|
||||
return Transient(io.NopCloser(strings.NewReader("stand-in"))), nil
|
||||
})
|
||||
s, err := fc.Get(context.Background(), &testArg{"k"})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(s.Transient).To(BeTrue())
|
||||
Expect(io.ReadAll(s)).To(Equal([]byte("stand-in")))
|
||||
_, err = io.ReadAll(joined)
|
||||
Expect(err).To(MatchError("loader failed"))
|
||||
_ = joined.Close()
|
||||
})
|
||||
|
||||
It("writes a completion marker after a successful cache write", func() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue