quintodrome/core/scrobbler
Deluan Quintão 0faf744e32
refactor: make NowPlaying dispatch asynchronous with worker pool (#4757)
* feat: make NowPlaying dispatch asynchronous with worker pool

Implemented asynchronous NowPlaying dispatch using a queue worker pattern similar to cacheWarmer. Instead of dispatching NowPlaying updates synchronously during the HTTP request, they are now queued and processed by background workers at controlled intervals.

Key changes:
- Added nowPlayingEntry struct to represent queued entries
- Added npQueue map (keyed by playerId), npMu mutex, and npSignal channel to playTracker
- Implemented enqueueNowPlaying() to add entries to the queue
- Implemented nowPlayingWorker() that polls every 100ms, drains queue, and processes entries
- Changed NowPlaying() to queue dispatch instead of calling synchronously
- Renamed dispatchNowPlaying() to dispatchNowPlayingAsync() and updated it to use background context

Benefits:
- HTTP handlers return immediately without waiting for scrobbler responses
- Deduplication by key: rapid calls (seeking) only dispatch latest state
- Fire-and-forget: one-shot attempts with logged failures
- Backpressure-free: worker processes at its own pace
- Tests updated to use Eventually() assertions for async dispatch

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(play_tracker): increase timeout duration for signal handling

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(play_tracker): simplify queue processing by directly assigning entries

Signed-off-by: Deluan <deluan@navidrome.org>

---------

Signed-off-by: Deluan <deluan@navidrome.org>
2025-12-01 22:21:54 -05:00
..
buffered_scrobbler.go feat(plugins): experimental support for plugins (#3998) 2025-06-22 20:45:38 -04:00
buffered_scrobbler_test.go refactor: make NowPlaying dispatch asynchronous with worker pool (#4757) 2025-12-01 22:21:54 -05:00
interfaces.go feat(plugins): experimental support for plugins (#3998) 2025-06-22 20:45:38 -04:00
play_tracker.go refactor: make NowPlaying dispatch asynchronous with worker pool (#4757) 2025-12-01 22:21:54 -05:00
play_tracker_test.go refactor: make NowPlaying dispatch asynchronous with worker pool (#4757) 2025-12-01 22:21:54 -05:00
scrobbler_suite_test.go Rename log.LevelCritical to log.LevelFatal 2022-12-21 14:53:36 -05:00