Files
Story Crater Bot 6cf5c3e5a9
Build / Build and push image (push) Successful in 34s
CI / Test, vet, build (push) Successful in 2m16s
fix(server): stop WriteTimeout from killing in-progress LLM streams
http.Server.WriteTimeout is an absolute deadline over the whole
response, not an inactivity timeout -- 15s was cutting off SSE streams
from the reasoning model mid-generation, surfacing to clients as a
"terminated" error well before the model finished. Switch to
ReadHeaderTimeout (protects against slow headers without capping body
duration) and raise WriteTimeout to match the edge nginx Ingress's
proxy-read/send-timeout of 3600s.
2026-08-21 20:06:10 -07:00

90 lines
2.6 KiB
Go

package server
import (
"context"
"net"
"net/http"
"sync"
"time"
)
// Server wraps an HTTP server with graceful shutdown support.
type Server struct {
httpServer *http.Server
shutdownTimeout time.Duration
listener net.Listener
listenerMu sync.RWMutex
healthChecker *HealthChecker
}
// New creates a new Server with the given configuration.
func New(listenAddr string, shutdownTimeout time.Duration, handler http.Handler) *Server {
return &Server{
httpServer: &http.Server{
Addr: listenAddr,
Handler: handler,
// ReadHeaderTimeout (not ReadTimeout) and a long WriteTimeout: both
// ReadTimeout and WriteTimeout are absolute deadlines covering the
// whole request/response body, not inactivity timeouts -- a 15s
// WriteTimeout here was killing in-progress LLM SSE streams (proxy.go's
// outbound transport deliberately avoids this same mistake). Mirrors
// the edge nginx Ingress's proxy-read/send-timeout of 3600s.
ReadHeaderTimeout: 15 * time.Second,
WriteTimeout: 1 * time.Hour,
IdleTimeout: 60 * time.Second,
},
shutdownTimeout: shutdownTimeout,
healthChecker: NewHealthChecker(false, false),
}
}
// ListenAndServe starts the HTTP server and blocks until it exits.
// It returns the error from the server (if any), which will be
// http.ErrServerClosed if Shutdown was called.
func (s *Server) ListenAndServe() error {
listener, err := net.Listen("tcp", s.httpServer.Addr)
if err != nil {
return err
}
s.listenerMu.Lock()
s.listener = listener
s.listenerMu.Unlock()
return s.httpServer.Serve(listener)
}
// Shutdown gracefully shuts down the server. It stops accepting new
// connections and waits for in-flight requests to complete, with a
// bounded deadline. If the deadline is exceeded, it returns an error.
func (s *Server) Shutdown(ctx context.Context) error {
// Create a new context with the shutdown timeout
shutdownCtx, cancel := context.WithTimeout(ctx, s.shutdownTimeout)
defer cancel()
return s.httpServer.Shutdown(shutdownCtx)
}
// Addr returns the network address the server is listening on.
func (s *Server) Addr() string {
s.listenerMu.RLock()
defer s.listenerMu.RUnlock()
if s.listener != nil {
return s.listener.Addr().String()
}
return s.httpServer.Addr
}
// HealthChecker returns the server's health checker.
func (s *Server) HealthChecker() *HealthChecker {
return s.healthChecker
}
// SetHealthChecker sets the server's health checker.
func (s *Server) SetHealthChecker(hc *HealthChecker) {
s.healthChecker = hc
}
// SetHandler sets the server's HTTP handler.
func (s *Server) SetHandler(handler http.Handler) {
s.httpServer.Handler = handler
}