mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-21 01:19:58 +01:00
refactor(plugins): serialize PlaylistGenerator plugin calls via work queue
Replace the timer-per-playlist + WaitGroup.Go model with a single worker goroutine processing work items from a buffered channel. This eliminates race conditions from parallel plugin calls (rate limiting risk), unsynchronized map/field access across goroutines, and overlapping discovery and refresh timers. The worker owns all mutable state (refreshTimers, discoveryTimer) exclusively, while retryInterval and refreshTimerCount use atomics for safe test observability. Also adds retry-on-failure for GetAvailablePlaylists (5 min delay) and makes MockPlaylistRepo thread-safe with sync.RWMutex.
This commit is contained in:
parent
fde4517339
commit
9c516cf601
5 changed files with 165 additions and 85 deletions
|
|
@ -2,6 +2,7 @@ package tests
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/deluan/rest"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
|
|
@ -17,6 +18,7 @@ func CreateMockPlaylistRepo() *MockPlaylistRepo {
|
|||
|
||||
type MockPlaylistRepo struct {
|
||||
model.PlaylistRepository
|
||||
mu sync.RWMutex
|
||||
Data map[string]*model.Playlist // keyed by ID
|
||||
PathMap map[string]*model.Playlist // keyed by path
|
||||
Last *model.Playlist
|
||||
|
|
@ -26,10 +28,14 @@ type MockPlaylistRepo struct {
|
|||
}
|
||||
|
||||
func (m *MockPlaylistRepo) SetError(err bool) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.Err = err
|
||||
}
|
||||
|
||||
func (m *MockPlaylistRepo) Get(id string) (*model.Playlist, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if m.Err {
|
||||
return nil, errors.New("error")
|
||||
}
|
||||
|
|
@ -46,6 +52,8 @@ func (m *MockPlaylistRepo) GetWithTracks(id string, _, _ bool) (*model.Playlist,
|
|||
}
|
||||
|
||||
func (m *MockPlaylistRepo) Put(pls *model.Playlist) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.Err {
|
||||
return errors.New("error")
|
||||
}
|
||||
|
|
@ -60,6 +68,8 @@ func (m *MockPlaylistRepo) Put(pls *model.Playlist) error {
|
|||
}
|
||||
|
||||
func (m *MockPlaylistRepo) FindByPath(path string) (*model.Playlist, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if m.Err {
|
||||
return nil, errors.New("error")
|
||||
}
|
||||
|
|
@ -72,6 +82,8 @@ func (m *MockPlaylistRepo) FindByPath(path string) (*model.Playlist, error) {
|
|||
}
|
||||
|
||||
func (m *MockPlaylistRepo) Delete(id string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.Err {
|
||||
return errors.New("error")
|
||||
}
|
||||
|
|
@ -80,10 +92,14 @@ func (m *MockPlaylistRepo) Delete(id string) error {
|
|||
}
|
||||
|
||||
func (m *MockPlaylistRepo) Tracks(_ string, _ bool) model.PlaylistTrackRepository {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return m.TracksRepo
|
||||
}
|
||||
|
||||
func (m *MockPlaylistRepo) Exists(id string) (bool, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if m.Err {
|
||||
return false, errors.New("error")
|
||||
}
|
||||
|
|
@ -95,6 +111,8 @@ func (m *MockPlaylistRepo) Exists(id string) (bool, error) {
|
|||
}
|
||||
|
||||
func (m *MockPlaylistRepo) Count(_ ...rest.QueryOptions) (int64, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if m.Err {
|
||||
return 0, errors.New("error")
|
||||
}
|
||||
|
|
@ -102,10 +120,39 @@ func (m *MockPlaylistRepo) Count(_ ...rest.QueryOptions) (int64, error) {
|
|||
}
|
||||
|
||||
func (m *MockPlaylistRepo) CountAll(_ ...model.QueryOptions) (int64, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if m.Err {
|
||||
return 0, errors.New("error")
|
||||
}
|
||||
return int64(len(m.Data)), nil
|
||||
}
|
||||
|
||||
// Len returns the number of playlists in the repo in a thread-safe manner.
|
||||
func (m *MockPlaylistRepo) Len() int {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return len(m.Data)
|
||||
}
|
||||
|
||||
// GetData returns a playlist by ID in a thread-safe manner.
|
||||
func (m *MockPlaylistRepo) GetData(id string) (*model.Playlist, bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
pls, ok := m.Data[id]
|
||||
return pls, ok
|
||||
}
|
||||
|
||||
// FindByPluginPlaylistID returns the first playlist with the given plugin playlist ID.
|
||||
func (m *MockPlaylistRepo) FindByPluginPlaylistID(pluginPlaylistID string) *model.Playlist {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for _, pls := range m.Data {
|
||||
if pls.PluginPlaylistID == pluginPlaylistID {
|
||||
return pls
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ model.PlaylistRepository = (*MockPlaylistRepo)(nil)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue