From 2edb1b8eda649671aa4b0534ec8065bde65803e3 Mon Sep 17 00:00:00 2001 From: sentriz Date: Sat, 18 Nov 2023 12:33:36 +0000 Subject: [PATCH] feat: set global http timeouts except for streaming endpoints related #411 Release-As: 0.16.2 --- cmd/gonic/gonic.go | 6 +++++- handlerutil/handlerutil.go | 4 ++++ server/ctrlsubsonic/ctrl.go | 22 ++++++++++++++++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index ede497d8..237840d9 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -288,7 +288,11 @@ func main() { errgrp.Go(func() error { defer logJob("http")() - server := &http.Server{Addr: *confListenAddr, Handler: mux, ReadHeaderTimeout: 5 * time.Second} + server := &http.Server{ + Addr: *confListenAddr, + ReadTimeout: 5 * time.Second, WriteTimeout: 5 * time.Second, IdleTimeout: 5 * time.Second, + Handler: mux, + } errgrp.Go(func() error { <-ctx.Done() return server.Shutdown(context.Background()) diff --git a/handlerutil/handlerutil.go b/handlerutil/handlerutil.go index 860f91de..80b929e5 100644 --- a/handlerutil/handlerutil.go +++ b/handlerutil/handlerutil.go @@ -105,6 +105,10 @@ func (w *statusWriter) Write(b []byte) (int, error) { return w.ResponseWriter.Write(b) } +func (w *statusWriter) Unwrap() http.ResponseWriter { + return w.ResponseWriter +} + func statusToBlock(code int) string { var bg int switch { diff --git a/server/ctrlsubsonic/ctrl.go b/server/ctrlsubsonic/ctrl.go index fb8cefeb..189842f2 100644 --- a/server/ctrlsubsonic/ctrl.go +++ b/server/ctrlsubsonic/ctrl.go @@ -10,6 +10,7 @@ import ( "io" "log" "net/http" + "time" "go.senan.xyz/gonic/db" "go.senan.xyz/gonic/handlerutil" @@ -94,6 +95,10 @@ func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, podcastsPa withRequiredParams, withUser(dbc), ) + chainRaw := handlerutil.Chain( + chain, + slow, + ) c.Handle("/getLicense", chain(resp(c.ServeGetLicence))) c.Handle("/ping", chain(resp(c.ServePing))) @@ -124,10 +129,10 @@ func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, podcastsPa c.Handle("/getLyrics", chain(resp(c.ServeGetLyrics))) // raw - c.Handle("/getCoverArt", chain(respRaw(c.ServeGetCoverArt))) - c.Handle("/stream", chain(respRaw(c.ServeStream))) - c.Handle("/download", chain(respRaw(c.ServeStream))) - c.Handle("/getAvatar", chain(respRaw(c.ServeGetAvatar))) + c.Handle("/getCoverArt", chainRaw(respRaw(c.ServeGetCoverArt))) + c.Handle("/stream", chainRaw(respRaw(c.ServeStream))) + c.Handle("/download", chainRaw(respRaw(c.ServeStream))) + c.Handle("/getAvatar", chainRaw(respRaw(c.ServeGetAvatar))) // browse by tag c.Handle("/getAlbum", chain(resp(c.ServeGetAlbum))) @@ -257,6 +262,15 @@ func withUser(dbc *db.DB) handlerutil.Middleware { } } +func slow(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + rc := http.NewResponseController(w) //nolint:bodyclose + _ = rc.SetWriteDeadline(time.Time{}) // set no deadline, since we're probably streaming + _ = rc.SetReadDeadline(time.Time{}) // set no deadline, since we're probably streaming + next.ServeHTTP(w, r) + }) +} + func checkCredsToken(password, token, salt string) bool { toHash := fmt.Sprintf("%s%s", password, salt) hash := md5.Sum([]byte(toHash))