All checks were successful
check / check (push) Successful in 4s
Raise `httpWriteTimeout` in `internal/server/http.go` from 10s to `65 * time.Second` so it comfortably exceeds the router's 60s `requestTimeout`. This makes the `middleware.Timeout(60s)` the effective request limit — a slow response now returns a clean 503 from the middleware instead of being cut at the socket write deadline by the transport. `httpReadTimeout` stays at 10s. A comment on `httpWriteTimeout` documents that it must remain above the 60s request timeout. Change is confined to `internal/server/http.go`; `routes.go` is untouched. Closes #62 Co-authored-by: sneak <sneak@sneak.berlin> Reviewed-on: #72 Co-authored-by: clawbot <clawbot@noreply.example.org> Co-committed-by: clawbot <clawbot@noreply.example.org>
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
// httpReadTimeout is the maximum duration for reading the
|
|
// entire request, including the body.
|
|
httpReadTimeout = 10 * time.Second
|
|
|
|
// httpWriteTimeout is the maximum duration before timing out
|
|
// writes of the response. It must stay above the router's
|
|
// requestTimeout (60s, in routes.go) so the middleware timeout
|
|
// fires first and returns a clean 503, rather than the transport
|
|
// cutting the connection at the socket write deadline.
|
|
httpWriteTimeout = 65 * time.Second
|
|
|
|
// httpMaxHeaderBytes is the maximum number of bytes the
|
|
// server will read parsing the request headers.
|
|
httpMaxHeaderBytes = 1 << 20
|
|
)
|
|
|
|
func (s *Server) serveUntilShutdown() {
|
|
listenAddr := fmt.Sprintf(":%d", s.params.Config.Port)
|
|
s.httpServer = &http.Server{
|
|
Addr: listenAddr,
|
|
ReadTimeout: httpReadTimeout,
|
|
WriteTimeout: httpWriteTimeout,
|
|
MaxHeaderBytes: httpMaxHeaderBytes,
|
|
Handler: s,
|
|
}
|
|
|
|
// add routes
|
|
// this does any necessary setup in each handler
|
|
s.SetupRoutes()
|
|
|
|
s.log.Info("http begin listen", "listenaddr", listenAddr)
|
|
|
|
err := s.httpServer.ListenAndServe()
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
s.log.Error("listen error", "error", err)
|
|
|
|
if s.cancelFunc != nil {
|
|
s.cancelFunc()
|
|
}
|
|
}
|
|
}
|
|
|
|
// ServeHTTP delegates to the router.
|
|
func (s *Server) ServeHTTP(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
s.router.ServeHTTP(w, r)
|
|
}
|