request throttling only for media group api

This commit is contained in:
Dimitri Herzog 2020-04-21 07:32:34 +02:00 committed by Deluan Quintão
parent 694be54428
commit f1e1d3bc07
2 changed files with 9 additions and 8 deletions

View File

@ -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)

View File

@ -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)