feat: set global http timeouts except for streaming endpoints

related #411

Release-As: 0.16.2
This commit is contained in:
sentriz
2023-11-18 12:33:36 +00:00
parent dd0f6b3650
commit 2edb1b8eda
3 changed files with 27 additions and 5 deletions

View File

@@ -288,7 +288,11 @@ func main() {
errgrp.Go(func() error { errgrp.Go(func() error {
defer logJob("http")() 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 { errgrp.Go(func() error {
<-ctx.Done() <-ctx.Done()
return server.Shutdown(context.Background()) return server.Shutdown(context.Background())

View File

@@ -105,6 +105,10 @@ func (w *statusWriter) Write(b []byte) (int, error) {
return w.ResponseWriter.Write(b) return w.ResponseWriter.Write(b)
} }
func (w *statusWriter) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}
func statusToBlock(code int) string { func statusToBlock(code int) string {
var bg int var bg int
switch { switch {

View File

@@ -10,6 +10,7 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"time"
"go.senan.xyz/gonic/db" "go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/handlerutil" "go.senan.xyz/gonic/handlerutil"
@@ -94,6 +95,10 @@ func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, podcastsPa
withRequiredParams, withRequiredParams,
withUser(dbc), withUser(dbc),
) )
chainRaw := handlerutil.Chain(
chain,
slow,
)
c.Handle("/getLicense", chain(resp(c.ServeGetLicence))) c.Handle("/getLicense", chain(resp(c.ServeGetLicence)))
c.Handle("/ping", chain(resp(c.ServePing))) 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))) c.Handle("/getLyrics", chain(resp(c.ServeGetLyrics)))
// raw // raw
c.Handle("/getCoverArt", chain(respRaw(c.ServeGetCoverArt))) c.Handle("/getCoverArt", chainRaw(respRaw(c.ServeGetCoverArt)))
c.Handle("/stream", chain(respRaw(c.ServeStream))) c.Handle("/stream", chainRaw(respRaw(c.ServeStream)))
c.Handle("/download", chain(respRaw(c.ServeStream))) c.Handle("/download", chainRaw(respRaw(c.ServeStream)))
c.Handle("/getAvatar", chain(respRaw(c.ServeGetAvatar))) c.Handle("/getAvatar", chainRaw(respRaw(c.ServeGetAvatar)))
// browse by tag // browse by tag
c.Handle("/getAlbum", chain(resp(c.ServeGetAlbum))) 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 { func checkCredsToken(password, token, salt string) bool {
toHash := fmt.Sprintf("%s%s", password, salt) toHash := fmt.Sprintf("%s%s", password, salt)
hash := md5.Sum([]byte(toHash)) hash := md5.Sum([]byte(toHash))