Add throttling to /share/img endpoint.

See: https://github.com/navidrome/navidrome/issues/2130#issuecomment-1414152343
This commit is contained in:
Deluan 2023-02-02 13:59:04 -05:00
parent 9b81aa4403
commit bcab3cc0f9
2 changed files with 10 additions and 2 deletions

View file

@ -288,7 +288,7 @@ 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("devartworkmaxrequests", number.Max(2, runtime.NumCPU()/3))
viper.SetDefault("devartworkthrottlebackloglimit", consts.RequestThrottleBacklogLimit) viper.SetDefault("devartworkthrottlebackloglimit", consts.RequestThrottleBacklogLimit)
viper.SetDefault("devartworkthrottlebacklogtimeout", consts.RequestThrottleBacklogTimeout) viper.SetDefault("devartworkthrottlebacklogtimeout", consts.RequestThrottleBacklogTimeout)
} }

View file

@ -5,6 +5,7 @@ import (
"path" "path"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core" "github.com/navidrome/navidrome/core"
@ -37,7 +38,14 @@ func (p *Router) routes() http.Handler {
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
r.Use(server.URLParamsMiddleware) r.Use(server.URLParamsMiddleware)
r.HandleFunc("/img/{id}", p.handleImages) r.Group(func(r chi.Router) {
if conf.Server.DevArtworkMaxRequests > 0 {
maxRequests := conf.Server.DevArtworkMaxRequests
r.Use(middleware.ThrottleBacklog(maxRequests, conf.Server.DevArtworkThrottleBacklogLimit,
conf.Server.DevArtworkThrottleBacklogTimeout))
}
r.HandleFunc("/img/{id}", p.handleImages)
})
if conf.Server.EnableSharing { if conf.Server.EnableSharing {
r.HandleFunc("/s/{id}", p.handleStream) r.HandleFunc("/s/{id}", p.handleStream)
r.HandleFunc("/{id}", p.handleShares) r.HandleFunc("/{id}", p.handleShares)