Skip to content

🎧 Your Personal Streaming Service

configuration for request throttling

Dimitri Herzog716f4c5cf7c1
diff --git a/consts/consts.go b/consts/consts.go
index 51d691a21..1dfa8e70d 100644
--- a/consts/consts.go
+++ b/consts/consts.go
@@ -26,6 +26,9 @@ const (
URLPathUI = "/app"
URLPathSubsonicAPI = "/rest"
+
+ RequestThrottleBacklogLimit = 100
+ RequestThrottleBacklogTimeout = time.Minute
)
// Cache options
diff --git a/server/server.go b/server/server.go
index 6b7e5f9c1..0c98a8555 100644
--- a/server/server.go
+++ b/server/server.go
@@ -1,10 +1,12 @@
package server
import (
+ "math"
"net/http"
"os"
"path"
"path/filepath"
+ "runtime"
"time"
"github.com/deluan/navidrome/conf"
@@ -62,6 +64,10 @@ func (a *Server) initRoutes() {
r.Use(middleware.Heartbeat("/ping"))
r.Use(InjectLogger)
+ // configure request throttling
+ maxRequests := math.Max(2, float64(runtime.NumCPU()))
+ r.Use(middleware.ThrottleBacklog(int(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)