New dev options to control getCoverArt throttling

This commit is contained in:
Deluan 2023-01-13 18:10:32 -05:00 committed by Deluan Quintão
parent ab7668f562
commit c46a2a5f5f
2 changed files with 27 additions and 16 deletions

View file

@ -4,12 +4,14 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"time" "time"
"github.com/kr/pretty" "github.com/kr/pretty"
"github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/utils/number"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -82,6 +84,9 @@ type configOptions struct {
DevSidebarPlaylists bool DevSidebarPlaylists bool
DevEnableBufferedScrobble bool DevEnableBufferedScrobble bool
DevShowArtistPage bool DevShowArtistPage bool
DevArtworkMaxRequests int
DevArtworkThrottleBacklogLimit int
DevArtworkThrottleBacklogTimeout time.Duration
} }
type scannerOptions struct { type scannerOptions struct {
@ -284,6 +289,9 @@ func init() {
viper.SetDefault("devenablebufferedscrobble", true) viper.SetDefault("devenablebufferedscrobble", true)
viper.SetDefault("devsidebarplaylists", true) viper.SetDefault("devsidebarplaylists", true)
viper.SetDefault("devshowartistpage", true) viper.SetDefault("devshowartistpage", true)
viper.SetDefault("devartworkmaxrequests", number.Max(2, runtime.NumCPU()))
viper.SetDefault("devartworkthrottlebackloglimit", consts.RequestThrottleBacklogLimit)
viper.SetDefault("devartworkthrottlebacklogtimeout", consts.RequestThrottleBacklogTimeout)
} }
func InitConfig(cfgFile string) { func InitConfig(cfgFile string) {

View file

@ -6,11 +6,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"runtime"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"
"github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/core" "github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/artwork" "github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/core/scrobbler" "github.com/navidrome/navidrome/core/scrobbler"
@ -20,7 +19,6 @@ import (
"github.com/navidrome/navidrome/server/events" "github.com/navidrome/navidrome/server/events"
"github.com/navidrome/navidrome/server/subsonic/responses" "github.com/navidrome/navidrome/server/subsonic/responses"
"github.com/navidrome/navidrome/utils" "github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/number"
) )
const Version = "1.16.1" const Version = "1.16.1"
@ -138,13 +136,18 @@ func (api *Router) routes() http.Handler {
h(r, "startScan", api.StartScan) h(r, "startScan", api.StartScan)
}) })
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
// configure request throttling
maxRequests := number.Max(2, runtime.NumCPU())
r.Use(middleware.ThrottleBacklog(maxRequests, consts.RequestThrottleBacklogLimit, consts.RequestThrottleBacklogTimeout))
hr(r, "getAvatar", api.GetAvatar) hr(r, "getAvatar", api.GetAvatar)
hr(r, "getCoverArt", api.GetCoverArt)
h(r, "getLyrics", api.GetLyrics) h(r, "getLyrics", api.GetLyrics)
}) })
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,
conf.Server.DevArtworkThrottleBacklogTimeout))
}
hr(r, "getCoverArt", api.GetCoverArt)
})
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
r.Use(getPlayer(api.players)) r.Use(getPlayer(api.players))
hr(r, "stream", api.Stream) hr(r, "stream", api.Stream)