Skip to content

Commit

Permalink
feat: set global http timeouts except for streaming endpoints
Browse files Browse the repository at this point in the history
related #411

Release-As: 0.16.2
  • Loading branch information
sentriz committed Nov 18, 2023
1 parent dd0f6b3 commit 2edb1b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
6 changes: 5 additions & 1 deletion cmd/gonic/gonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions handlerutil/handlerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 18 additions & 4 deletions server/ctrlsubsonic/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"log"
"net/http"
"time"

"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/handlerutil"
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 2edb1b8

Please sign in to comment.