fix: add server timeouts to prevent goroutine/memory leak from idle keep-alive connections

Without IdleTimeout, clients holding keep-alive connections open indefinitely
caused server-side goroutines (and their ~16KB of buffers) to accumulate
linearly until OOM.
This commit is contained in:
Trong Huu Nguyen
2026-05-07 16:00:14 +02:00
parent a8f930e276
commit 1939d18ba8

View File

@@ -17,8 +17,12 @@ import (
func Start(cfg *config.Config, r chi.Router) error {
server := http.Server{
Addr: cfg.BindAddress,
Handler: r,
Addr: cfg.BindAddress,
Handler: r,
ReadHeaderTimeout: 10 * time.Second, // Prevents slowloris attacks (connections held open without sending headers).
IdleTimeout: 90 * time.Second, // Reclaims idle keep-alive connections; without this, goroutines and buffers leak indefinitely.
MaxHeaderBytes: 1 << 16, // 64KB
// ReadTimeout/WriteTimeout intentionally omitted - a reverse proxy must support slow transfers.
}
serverCtx, serverStopCtx := context.WithCancel(context.Background())