Skip to content

Commit

Permalink
Add throttling to /share/img endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Feb 2, 2023
1 parent 9b81aa4 commit bcab3cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func init() {
viper.SetDefault("devenablebufferedscrobble", true)
viper.SetDefault("devsidebarplaylists", 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("devartworkthrottlebacklogtimeout", consts.RequestThrottleBacklogTimeout)
}
Expand Down
10 changes: 9 additions & 1 deletion server/public/public_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core"
Expand Down Expand Up @@ -37,7 +38,14 @@ func (p *Router) routes() http.Handler {

r.Group(func(r chi.Router) {
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 {
r.HandleFunc("/s/{id}", p.handleStream)
r.HandleFunc("/{id}", p.handleShares)
Expand Down

0 comments on commit bcab3cc

Please sign in to comment.