mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
* 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.
55 lines
1.9 KiB
Go
55 lines
1.9 KiB
Go
package jellyfin
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"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(loadCtx, mf)
|
|
return l, 0, err // 0 → cache DefaultTTL
|
|
})
|
|
if err != nil {
|
|
log.Error(ctx, "Error getting lyrics", "id", mf.ID, "title", mf.Title, err)
|
|
return nil
|
|
}
|
|
return list
|
|
}
|
|
|
|
// getLyrics serves GET /Audio/{itemId}/Lyrics. Jellyfin returns 404 when a track has no lyrics
|
|
// (never an empty 200); all surveyed clients treat that gracefully.
|
|
func (api *Router) getLyrics(w http.ResponseWriter, r *http.Request) {
|
|
mf, ok := api.mediaFileForRequest(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
main, found := servableLyric(api.cachedLyrics(r.Context(), mf))
|
|
if !found {
|
|
http.Error(w, "Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
api.ok(w, r, dto.LyricDtoFromLyrics(*mf, main))
|
|
}
|
|
|
|
// servableLyric is the single predicate for both serving and advertising, so PlaybackInfo never
|
|
// advertises a Lyric stream that this endpoint would 404.
|
|
func servableLyric(list model.LyricList) (model.Lyrics, bool) {
|
|
main, found := list.Main()
|
|
return main, found && !main.IsEmpty()
|
|
}
|