Skip to content

Commit

Permalink
Add dev option to increase external metadata cache expiration. More logs
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Feb 2, 2023
1 parent 588ee94 commit ad2ad51
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type configOptions struct {
DevArtworkMaxRequests int
DevArtworkThrottleBacklogLimit int
DevArtworkThrottleBacklogTimeout time.Duration
DevArtistInfoTimeToLive time.Duration
DevAlbumInfoTimeToLive time.Duration
}

type scannerOptions struct {
Expand Down Expand Up @@ -291,6 +293,8 @@ func init() {
viper.SetDefault("devartworkmaxrequests", number.Max(2, runtime.NumCPU()/3))
viper.SetDefault("devartworkthrottlebackloglimit", consts.RequestThrottleBacklogLimit)
viper.SetDefault("devartworkthrottlebacklogtimeout", consts.RequestThrottleBacklogTimeout)
viper.SetDefault("devartistinfotimetolive", consts.ArtistInfoTimeToLive)
viper.SetDefault("devalbuminfotimetolive", consts.AlbumInfoTimeToLive)
}

func InitConfig(cfgFile string) {
Expand Down
8 changes: 5 additions & 3 deletions core/external_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/Masterminds/squirrel"
"github.com/deluan/sanitize"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/core/agents"
_ "github.com/navidrome/navidrome/core/agents/lastfm"
_ "github.com/navidrome/navidrome/core/agents/listenbrainz"
Expand Down Expand Up @@ -90,7 +90,7 @@ func (e *externalMetadata) UpdateAlbumInfo(ctx context.Context, id string) (*mod
}
}

if time.Since(album.ExternalInfoUpdatedAt) > consts.AlbumInfoTimeToLive {
if time.Since(album.ExternalInfoUpdatedAt) > conf.Server.DevAlbumInfoTimeToLive {
log.Debug("Found expired cached AlbumInfo, refreshing in the background", "updatedAt", album.ExternalInfoUpdatedAt, "name", album.Name)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
Expand Down Expand Up @@ -205,7 +205,7 @@ func (e *externalMetadata) refreshArtistInfo(ctx context.Context, id string) (*a
}

// If info is expired, trigger a populateArtistInfo in the background
if time.Since(artist.ExternalInfoUpdatedAt) > consts.ArtistInfoTimeToLive {
if time.Since(artist.ExternalInfoUpdatedAt) > conf.Server.DevArtistInfoTimeToLive {
log.Debug("Found expired cached ArtistInfo, refreshing in the background", "updatedAt", artist.ExternalInfoUpdatedAt, "name", artist.Name)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
Expand Down Expand Up @@ -474,7 +474,9 @@ func (e *externalMetadata) callGetSimilar(ctx context.Context, agent agents.Arti
if len(similar) == 0 || err != nil {
return
}
start := time.Now()
sa, err := e.mapSimilarArtists(ctx, similar, includeNotPresent)
log.Debug(ctx, "Mapped Similar Artists", "agent", "artist", artist.Name, "numSimilar", len(sa), "elapsed", time.Since(start))
if err != nil {
return
}
Expand Down
7 changes: 5 additions & 2 deletions server/public/public_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
"github.com/navidrome/navidrome/ui"
Expand Down Expand Up @@ -40,8 +41,10 @@ func (p *Router) routes() http.Handler {
r.Use(server.URLParamsMiddleware)
r.Group(func(r chi.Router) {
if conf.Server.DevArtworkMaxRequests > 0 {
maxRequests := conf.Server.DevArtworkMaxRequests
r.Use(middleware.ThrottleBacklog(maxRequests, conf.Server.DevArtworkThrottleBacklogLimit,
log.Debug("Public images endpoint will be throttled", "maxRequests", conf.Server.DevArtworkMaxRequests,
"backlogLimit", conf.Server.DevArtworkThrottleBacklogLimit, "backlogTimeout",
conf.Server.DevArtworkThrottleBacklogTimeout)
r.Use(middleware.ThrottleBacklog(conf.Server.DevArtworkMaxRequests, conf.Server.DevArtworkThrottleBacklogLimit,
conf.Server.DevArtworkThrottleBacklogTimeout))
}
r.HandleFunc("/img/{id}", p.handleImages)
Expand Down
6 changes: 4 additions & 2 deletions server/subsonic/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ func (api *Router) routes() http.Handler {
r.Group(func(r chi.Router) {
// configure request throttling
if conf.Server.DevArtworkMaxRequests > 0 {
maxRequests := conf.Server.DevArtworkMaxRequests
r.Use(middleware.ThrottleBacklog(maxRequests, conf.Server.DevArtworkThrottleBacklogLimit,
log.Debug("Subsonic getCoverArt endpoint will be throttled", "maxRequests", conf.Server.DevArtworkMaxRequests,
"backlogLimit", conf.Server.DevArtworkThrottleBacklogLimit, "backlogTimeout",
conf.Server.DevArtworkThrottleBacklogTimeout)
r.Use(middleware.ThrottleBacklog(conf.Server.DevArtworkMaxRequests, conf.Server.DevArtworkThrottleBacklogLimit,
conf.Server.DevArtworkThrottleBacklogTimeout))
}
hr(r, "getCoverArt", api.GetCoverArt)
Expand Down

0 comments on commit ad2ad51

Please sign in to comment.