Skip to content

Commit

Permalink
configuration for request throttling
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R authored and deluan committed Apr 20, 2020
1 parent ba2d4b6 commit 716f4c5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const (

URLPathUI = "/app"
URLPathSubsonicAPI = "/rest"

RequestThrottleBacklogLimit = 100
RequestThrottleBacklogTimeout = time.Minute
)

// Cache options
Expand Down
6 changes: 6 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package server

import (
"math"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"time"

"github.com/deluan/navidrome/conf"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 716f4c5

Please sign in to comment.