fix: dedupe and cap concurrent lyrics plugin fetches (#5792)

* fix: dedupe and cap concurrent lyrics plugin fetches

Clients like Finamp prefetch lyrics for several queue tracks at once. The
resulting burst of concurrent plugin calls can rate-limit the primary
lyrics provider into a timeout, making the plugin fall back to a lower
quality source and cache the bad result.

SimpleCache.GetWithLoader now deduplicates concurrent loads of the same
key via singleflight, with every waiter receiving the winner's result or
error. The Jellyfin lyrics loader is detached from the request context so
one cancelled request cannot fail the load for all waiters, and the
lyrics adapter caps in-flight plugin calls at 2 per plugin, queueing the
rest. As a side effect, the cached HTTP client used by the Last.fm,
Deezer and ListenBrainz agents also collapses identical concurrent
requests into a single upstream call.

* fix: harden lyrics concurrency fixes per review

Replace the stringified singleflight keys with a per-cache flight map
keyed by the cache key type itself, eliminating potential key collisions
for non-string keys, the nil-interface assertion panic, and the
stringification overhead. Release the lyrics semaphore slot via defer so
a panicking plugin call cannot leak it, and bound the detached lyrics
load with a one-minute timeout so a hung plugin cannot pin its
singleflight and semaphore slot indefinitely.
This commit is contained in:
Deluan Quintão 2026-07-16 20:10:35 -04:00 • committed by GitHub
commit 756df9decf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 256 additions and 26 deletions

View file

@ -3,6 +3,8 @@ package cache
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
. "github.com/onsi/ginkgo/v2"
@ -69,6 +71,116 @@ var _ = Describe("SimpleCache", func() {
_, err := cache.GetWithLoader("key", loader)
Expect(err).To(HaveOccurred())
})
It("suppresses concurrent loads for the same key", func() {
var calls atomic.Int32
release := make(chan struct{})
started := make(chan struct{}, 10)
loader := func(key string) (string, time.Duration, error) {
calls.Add(1)
started <- struct{}{}
<-release
return "shared", time.Minute, nil
}
const n = 5
var wg sync.WaitGroup
results := make([]string, n)
errs := make([]error, n)
for i := range n {
wg.Go(func() {
results[i], errs[i] = cache.GetWithLoader("key", loader)
})
}
Eventually(started).Should(Receive())
Consistently(started).ShouldNot(Receive())
close(release)
wg.Wait()
Expect(calls.Load()).To(Equal(int32(1)))
for i := range n {
Expect(errs[i]).ToNot(HaveOccurred())
Expect(results[i]).To(Equal("shared"))
}
})
It("returns the loader error to all concurrent callers", func() {
release := make(chan struct{})
started := make(chan struct{}, 10)
loader := func(key string) (string, time.Duration, error) {
started <- struct{}{}
<-release
return "", 0, errors.New("load failed")
}
const n = 3
var wg sync.WaitGroup
errs := make([]error, n)
for i := range n {
wg.Go(func() {
_, errs[i] = cache.GetWithLoader("key", loader)
})
}
Eventually(started).Should(Receive())
Consistently(started).ShouldNot(Receive())
close(release)
wg.Wait()
for i := range n {
Expect(errs[i]).To(MatchError(ContainSubstring("load failed")))
}
})
It("supports interface value types with nil results", func() {
c := NewSimpleCache[string, any]()
v, err := c.GetWithLoader("key", func(string) (any, time.Duration, error) {
return nil, time.Minute, nil
})
Expect(err).ToNot(HaveOccurred())
Expect(v).To(BeNil())
})
It("cleans up the in-flight registration when the loader panics", func() {
Expect(func() {
_, _ = cache.GetWithLoader("key", func(string) (string, time.Duration, error) {
panic("boom")
})
}).To(PanicWith("boom"))
// Without cleanup this would deadlock on the never-completed flight
v, err := cache.GetWithLoader("key", func(string) (string, time.Duration, error) {
return "ok", 0, nil
})
Expect(err).ToNot(HaveOccurred())
Expect(v).To(Equal("ok"))
})
It("loads different keys independently", func() {
release := make(chan struct{})
started := make(chan struct{}, 10)
loader := func(key string) (string, time.Duration, error) {
started <- struct{}{}
<-release
return key + "=value", time.Minute, nil
}
var wg sync.WaitGroup
for _, key := range []string{"key1", "key2"} {
wg.Go(func() {
value, err := cache.GetWithLoader(key, loader)
Expect(err).ToNot(HaveOccurred())
Expect(value).To(Equal(key + "=value"))
})
}
// Both loaders must be in flight at once: distinct keys are not suppressed
Eventually(started).Should(Receive())
Eventually(started).Should(Receive())
close(release)
wg.Wait()
})
})
Describe("Keys and Values", func() {