From 716f4c5cf7c1119d8dfa884448546d272aa3c457 Mon Sep 17 00:00:00 2001 From: Dimitri Herzog Date: Mon, 20 Apr 2020 17:05:28 +0200 Subject: [PATCH] configuration for request throttling --- consts/consts.go | 3 +++ server/server.go | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/consts/consts.go b/consts/consts.go index 51d691a2..1dfa8e70 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 6b7e5f9c..0c98a855 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)