diff --git a/server/server.go b/server/server.go index 990caad7..6b7e5f9c 100644 --- a/server/server.go +++ b/server/server.go @@ -5,7 +5,6 @@ import ( "os" "path" "path/filepath" - "runtime" "time" "github.com/deluan/navidrome/conf" @@ -13,7 +12,6 @@ import ( "github.com/deluan/navidrome/log" "github.com/deluan/navidrome/model" "github.com/deluan/navidrome/scanner" - "github.com/deluan/navidrome/utils" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/go-chi/cors" @@ -64,10 +62,6 @@ func (a *Server) initRoutes() { r.Use(middleware.Heartbeat("/ping")) r.Use(InjectLogger) - // configure request throttling - maxRequests := utils.MaxInt(2, runtime.NumCPU()) - r.Use(middleware.ThrottleBacklog(maxRequests, consts.RequestThrottleBacklogLimit, consts.RequestThrottleBacklogTimeout)) - indexHtml := path.Join(conf.Server.BaseURL, consts.URLPathUI, "index.html") r.Get("/*", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, indexHtml, 302) diff --git a/server/subsonic/api.go b/server/subsonic/api.go index 6feb26cf..9717fab0 100644 --- a/server/subsonic/api.go +++ b/server/subsonic/api.go @@ -4,7 +4,11 @@ import ( "encoding/json" "encoding/xml" "fmt" + "github.com/deluan/navidrome/consts" + "github.com/go-chi/chi/middleware" + "math" "net/http" + "runtime" "github.com/deluan/navidrome/engine" "github.com/deluan/navidrome/log" @@ -116,8 +120,11 @@ func (api *Router) routes() http.Handler { }) r.Group(func(r chi.Router) { c := initMediaRetrievalController(api) - H(r, "getAvatar", c.GetAvatar) - H(r, "getCoverArt", c.GetCoverArt) + // configure request throttling + maxRequests := math.Max(2, float64(runtime.NumCPU())) + withThrottle := r.With(middleware.ThrottleBacklog(int(maxRequests), consts.RequestThrottleBacklogLimit, consts.RequestThrottleBacklogTimeout)) + H(withThrottle, "getAvatar", c.GetAvatar) + H(withThrottle, "getCoverArt", c.GetCoverArt) }) r.Group(func(r chi.Router) { c := initStreamController(api)