mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-14 14:18:42 +01:00
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:
parent
29f481cd7b
commit
756df9decf
8 changed files with 256 additions and 26 deletions
|
|
@ -10,11 +10,18 @@ import (
|
|||
"github.com/navidrome/navidrome/server/jellyfin/dto"
|
||||
)
|
||||
|
||||
const lyricsLoadTimeout = time.Minute
|
||||
|
||||
// cachedLyrics resolves lyrics through the full source pipeline (embedded, sidecar, plugins),
|
||||
// caching results — including empty: clients poll per played track, so misses are the hot path.
|
||||
func (api *Router) cachedLyrics(ctx context.Context, mf *model.MediaFile) model.LyricList {
|
||||
// The load is shared across requests (singleflight) and cached, so don't let one
|
||||
// cancelled request abort it for everybody — detach it from the request's lifetime,
|
||||
// keeping a bound so a hung plugin can't pin the fetch (and its plugin slot) forever.
|
||||
loadCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), lyricsLoadTimeout)
|
||||
defer cancel()
|
||||
list, err := api.lyricsCache.GetWithLoader(mf.ID, func(string) (model.LyricList, time.Duration, error) {
|
||||
l, err := api.lyrics.GetLyrics(ctx, mf)
|
||||
l, err := api.lyrics.GetLyrics(loadCtx, mf)
|
||||
return l, 0, err // 0 → cache DefaultTTL
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -17,13 +17,18 @@ import (
|
|||
|
||||
// fakeLyricsService returns canned lyrics per media-file ID and counts calls.
|
||||
type fakeLyricsService struct {
|
||||
lyrics map[string]model.LyricList
|
||||
err error
|
||||
calls int
|
||||
lyrics map[string]model.LyricList
|
||||
err error
|
||||
calls int
|
||||
hadDeadline bool
|
||||
}
|
||||
|
||||
func (f *fakeLyricsService) GetLyrics(_ context.Context, mf *model.MediaFile) (model.LyricList, error) {
|
||||
func (f *fakeLyricsService) GetLyrics(ctx context.Context, mf *model.MediaFile) (model.LyricList, error) {
|
||||
f.calls++
|
||||
_, f.hadDeadline = ctx.Deadline()
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if f.err != nil {
|
||||
return nil, f.err
|
||||
}
|
||||
|
|
@ -129,4 +134,22 @@ var _ = Describe("getLyrics", func() {
|
|||
Expect(doRequest("s2").Code).To(Equal(http.StatusNotFound))
|
||||
Expect(fake.calls).To(Equal(1))
|
||||
})
|
||||
|
||||
It("completes and caches the fetch even when the request context is cancelled", func() {
|
||||
fake.lyrics["s1"] = model.LyricList{
|
||||
{Kind: "main", Synced: true, Line: []model.Line{{Start: p(1000), Value: "hello"}}},
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
list := api.cachedLyrics(ctx, &model.MediaFile{ID: "s1"})
|
||||
Expect(list).ToNot(BeEmpty())
|
||||
Expect(doRequest("s1").Code).To(Equal(http.StatusOK))
|
||||
Expect(fake.calls).To(Equal(1))
|
||||
})
|
||||
|
||||
It("bounds the detached fetch with a timeout", func() {
|
||||
Expect(doRequest("s2").Code).To(Equal(http.StatusNotFound))
|
||||
Expect(fake.hadDeadline).To(BeTrue())
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue