mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-01 07:21:17 +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.
80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package plugins
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/plugins/capabilities"
|
|
)
|
|
|
|
const CapabilityLyrics Capability = "Lyrics"
|
|
|
|
const (
|
|
FuncLyricsGetLyrics = "nd_lyrics_get_lyrics"
|
|
)
|
|
|
|
// maxConcurrentLyricsCalls caps in-flight lyrics calls per plugin: clients prefetch
|
|
// lyrics for whole queues, and the resulting burst can rate-limit upstream providers.
|
|
const maxConcurrentLyricsCalls = 2
|
|
|
|
func init() {
|
|
registerCapability(
|
|
CapabilityLyrics,
|
|
FuncLyricsGetLyrics,
|
|
)
|
|
}
|
|
|
|
func newLyricsPlugin(p *plugin) *LyricsPlugin {
|
|
return &LyricsPlugin{name: p.name, plugin: p}
|
|
}
|
|
|
|
// LyricsPlugin adapts a WASM plugin with the Lyrics capability.
|
|
type LyricsPlugin struct {
|
|
name string
|
|
plugin *plugin
|
|
}
|
|
|
|
// GetLyrics calls the plugin to fetch lyrics, then content-sniffs each response
|
|
// via model.ParseLyrics (TTML/SRT/YAML/LRC/plain).
|
|
func (l *LyricsPlugin) GetLyrics(ctx context.Context, mf *model.MediaFile) (model.LyricList, error) {
|
|
select {
|
|
case l.plugin.lyricsSem <- struct{}{}:
|
|
defer func() { <-l.plugin.lyricsSem }()
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
}
|
|
req := capabilities.GetLyricsRequest{
|
|
Track: mediaFileToTrackInfo(l.plugin, mf),
|
|
}
|
|
resp, err := callPluginFunction[capabilities.GetLyricsRequest, capabilities.GetLyricsResponse](
|
|
ctx, l.plugin, FuncLyricsGetLyrics, req,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// The lyric text comes from the plugin, not the media file's own tags, so
|
|
// attribute logs to both the plugin and the track it was fetched for.
|
|
ctx = log.NewContext(ctx, "plugin", l.name, "file", mf.Path)
|
|
|
|
var result model.LyricList
|
|
for _, lt := range resp.Lyrics {
|
|
lang := lt.Lang
|
|
if lang == "" {
|
|
lang = "xxx"
|
|
}
|
|
parsed, err := model.ParseLyrics(ctx, "", lang, []byte(lt.Text))
|
|
if err != nil {
|
|
log.Warn(ctx, "Error parsing plugin lyrics", err)
|
|
continue
|
|
}
|
|
for _, lyric := range parsed {
|
|
if !lyric.IsEmpty() {
|
|
result = append(result, lyric)
|
|
}
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|