Bound idle time, header size and request reading on every listener, and connections on the client port
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
d3fb217b31
commit
f1b49d0251
9 files changed
+78
-38
No files matched your search
+10
-10
@@ -293,20 +293,20 @@ func newMirrorServeCmd(dir *string) *cobra.Command {
|
||||
// http.Dir refuses paths that escape the root, which is the whole
|
||||
// traversal question answered by the standard library rather than
|
||||
// by me.
|
||||
files := &http.Server{
|
||||
Addr: listen,
|
||||
Handler: readOnly(withManifest(*dir, http.FileServer(http.Dir(*dir)))),
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
}
|
||||
files := withLimits(&http.Server{
|
||||
Addr: listen,
|
||||
Handler: readOnly(withManifest(*dir, http.FileServer(http.Dir(*dir)))),
|
||||
ReadTimeout: time.Minute,
|
||||
})
|
||||
allowed, err := parseAllow(allow)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
admin := &http.Server{
|
||||
Addr: apiListen,
|
||||
Handler: onlyClients(clients, apiGate(tlsc.Enabled(), allowed, api.Handler())),
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
}
|
||||
admin := withLimits(&http.Server{
|
||||
Addr: apiListen,
|
||||
Handler: onlyClients(clients, apiGate(tlsc.Enabled(), allowed, api.Handler())),
|
||||
ReadTimeout: time.Minute,
|
||||
})
|
||||
|
||||
errs := make(chan error, 2)
|
||||
go func() { errs <- fmt.Errorf("serving weights: %w", files.ListenAndServe()) }()
|
||||
|
||||
+31
-13
@@ -26,6 +26,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/net/http2"
|
||||
"golang.org/x/net/http2/h2c"
|
||||
"golang.org/x/net/netutil"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
llmgwv1 "gitea.mynodes.xyz/ilker/llmgw/api/gen/llmgw/v1"
|
||||
@@ -348,12 +349,15 @@ func newServerCmd() *cobra.Command {
|
||||
"api", cfg.Mirror.API, "mtls", cfg.Mirror.TLS.Enabled())
|
||||
}
|
||||
|
||||
apiSrv := &http.Server{
|
||||
Handler: rt.Handler(),
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
apiSrv := withLimits(&http.Server{
|
||||
Handler: rt.Handler(),
|
||||
// Reading the request: long enough for a body up to
|
||||
// max_body_bytes on a slow link, short enough that a client
|
||||
// trickling one cannot hold a connection and its buffer.
|
||||
ReadTimeout: 5 * time.Minute,
|
||||
// No write timeout: responses stream for minutes and the proxy
|
||||
// enforces its own per-phase deadlines.
|
||||
}
|
||||
})
|
||||
if err := checkAdminListen(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -361,15 +365,20 @@ func newServerCmd() *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
adminSrv := &http.Server{
|
||||
Handler: router.AdminGuard(router.AdminAuth(adminToken, cfg.DebugPprof, adminMux)),
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
}
|
||||
adminSrv := withLimits(&http.Server{
|
||||
Handler: router.AdminGuard(router.AdminAuth(adminToken, cfg.DebugPprof, adminMux)),
|
||||
ReadTimeout: time.Minute,
|
||||
})
|
||||
|
||||
apiLn, err := net.Listen("tcp", cfg.Listen)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listening on %s: %w", cfg.Listen, err)
|
||||
}
|
||||
if cfg.MaxConnections > 0 {
|
||||
// Past this, new connections wait in the kernel's backlog
|
||||
// rather than each taking a goroutine and its buffers.
|
||||
apiLn = netutil.LimitListener(apiLn, cfg.MaxConnections)
|
||||
}
|
||||
adminLn, err := net.Listen("tcp", cfg.AdminListen)
|
||||
if err != nil {
|
||||
_ = apiLn.Close()
|
||||
@@ -402,7 +411,7 @@ func newServerCmd() *cobra.Command {
|
||||
mux.Handle("/", h)
|
||||
h = mux
|
||||
}
|
||||
metricsSrv = &http.Server{Handler: h, ReadHeaderTimeout: 10 * time.Second}
|
||||
metricsSrv = withLimits(&http.Server{Handler: h, ReadTimeout: 30 * time.Second})
|
||||
go func() {
|
||||
if err := metricsSrv.Serve(metricsLn); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Error("metrics listener stopped", "err", err)
|
||||
@@ -423,10 +432,9 @@ func newServerCmd() *cobra.Command {
|
||||
if agentHandler != nil {
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle(agentPath, agentHandler)
|
||||
agentSrv = &http.Server{
|
||||
Handler: mux,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
}
|
||||
// No read timeout: an agent's stream is one request that
|
||||
// lasts as long as the agent does.
|
||||
agentSrv = withLimits(&http.Server{Handler: mux})
|
||||
if !cfg.AgentTLS.Enabled() {
|
||||
// Without TLS, agents speak plaintext HTTP/2, which Go will
|
||||
// not do unless asked. Nothing then stops a node from
|
||||
@@ -801,3 +809,13 @@ func agentListenAddr(cfg config.Server) (string, error) {
|
||||
}
|
||||
return cfg.AgentListen, nil
|
||||
}
|
||||
|
||||
// withLimits gives a server what every listener here wants: a bound on how
|
||||
// long the headers may take, how long a connection may sit idle between
|
||||
// requests, and how large the headers may be.
|
||||
func withLimits(s *http.Server) *http.Server {
|
||||
s.ReadHeaderTimeout = 10 * time.Second
|
||||
s.IdleTimeout = 2 * time.Minute
|
||||
s.MaxHeaderBytes = 1 << 20
|
||||
return s
|
||||
}
|
||||
Reference in new issue
Block a user