🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
fix(server): race condition when updating artist/album from external sources
Deluan2b0bfbd75add
diff --git a/core/external_metadata.go b/core/external_metadata.goindex bc4c30566..e082df9c7 100644--- a/core/external_metadata.go+++ b/core/external_metadata.go@@ -43,8 +43,8 @@ type ExternalMetadata interface {type externalMetadata struct {ds model.DataStoreag *agents.Agents- artistQueue refreshQueue[*auxArtist]- albumQueue refreshQueue[*auxAlbum]+ artistQueue refreshQueue[auxArtist]+ albumQueue refreshQueue[auxAlbum]}type auxAlbum struct {@@ -94,7 +94,7 @@ func (e *externalMetadata) UpdateAlbumInfo(ctx context.Context, id string) (*modupdatedAt := V(album.ExternalInfoUpdatedAt)if updatedAt.IsZero() {log.Debug(ctx, "AlbumInfo not cached. Retrieving it now", "updatedAt", updatedAt, "id", id, "name", album.Name)- err = e.populateAlbumInfo(ctx, album)+ err = e.populateAlbumInfo(ctx, *album)if err != nil {return nil, err}@@ -109,7 +109,7 @@ func (e *externalMetadata) UpdateAlbumInfo(ctx context.Context, id string) (*modreturn &album.Album, nil}-func (e *externalMetadata) populateAlbumInfo(ctx context.Context, album *auxAlbum) error {+func (e *externalMetadata) populateAlbumInfo(ctx context.Context, album auxAlbum) error {start := time.Now()info, err := e.ag.GetAlbumInfo(ctx, album.Name, album.AlbumArtist, album.MbzAlbumID)if errors.Is(err, agents.ErrNotFound) {@@ -197,7 +197,7 @@ func (e *externalMetadata) refreshArtistInfo(ctx context.Context, id string) (*aupdatedAt := V(artist.ExternalInfoUpdatedAt)if updatedAt.IsZero() {log.Debug(ctx, "ArtistInfo not cached. Retrieving it now", "updatedAt", updatedAt, "id", id, "name", artist.Name)- err := e.populateArtistInfo(ctx, artist)+ err := e.populateArtistInfo(ctx, *artist)if err != nil {return nil, err}@@ -211,7 +211,7 @@ func (e *externalMetadata) refreshArtistInfo(ctx context.Context, id string) (*areturn artist, nil}-func (e *externalMetadata) populateArtistInfo(ctx context.Context, artist *auxArtist) error {+func (e *externalMetadata) populateArtistInfo(ctx context.Context, artist auxArtist) error {start := time.Now()// Get MBID first, if it is not yet availableif artist.MbzArtistID == "" {@@ -224,10 +224,10 @@ func (e *externalMetadata) populateArtistInfo(ctx context.Context, artist *auxAr// Call all registered agents and collect informationg := errgroup.Group{}g.SetLimit(2)- g.Go(func() error { e.callGetImage(ctx, e.ag, artist); return nil })- g.Go(func() error { e.callGetBiography(ctx, e.ag, artist); return nil })- g.Go(func() error { e.callGetURL(ctx, e.ag, artist); return nil })- g.Go(func() error { e.callGetSimilar(ctx, e.ag, artist, maxSimilarArtists, true); return nil })+ g.Go(func() error { e.callGetImage(ctx, e.ag, &artist); return nil })+ g.Go(func() error { e.callGetBiography(ctx, e.ag, &artist); return nil })+ g.Go(func() error { e.callGetURL(ctx, e.ag, &artist); return nil })+ g.Go(func() error { e.callGetSimilar(ctx, e.ag, &artist, maxSimilarArtists, true); return nil })_ = g.Wait()if utils.IsCtxDone(ctx) {@@ -552,10 +552,10 @@ func (e *externalMetadata) loadSimilar(ctx context.Context, artist *auxArtist, creturn nil}-type refreshQueue[T any] chan<- T+type refreshQueue[T any] chan<- *Tfunc newRefreshQueue[T any](ctx context.Context, processFn func(context.Context, T) error) refreshQueue[T] {- queue := make(chan T, refreshQueueLength)+ queue := make(chan *T, refreshQueueLength)go func() {for {select {@@ -565,7 +565,7 @@ func newRefreshQueue[T any](ctx context.Context, processFn func(context.Context,ctx, cancel := context.WithTimeout(ctx, refreshTimeout)select {case item := <-queue:- _ = processFn(ctx, item)+ _ = processFn(ctx, *item)cancel()case <-ctx.Done():cancel()@@ -575,7 +575,8 @@ func newRefreshQueue[T any](ctx context.Context, processFn func(context.Context,}()return queue}-func (q *refreshQueue[T]) enqueue(item T) {++func (q *refreshQueue[T]) enqueue(item *T) {select {case *q <- item:default: // It is ok to miss a refresh request