Skip to content

Commit

Permalink
request throttling only for media group api
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R authored and deluan committed Apr 21, 2020
1 parent 694be54 commit f1e1d3b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
6 changes: 0 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"os"
"path"
"path/filepath"
"runtime"
"time"

"github.com/deluan/navidrome/conf"
"github.com/deluan/navidrome/consts"
"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"
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions server/subsonic/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f1e1d3b

Please sign in to comment.