v.pandelane.dev

Timeouts in Go HTTP servers, the short version

2026-09-14

The zero value of http.Server has no timeouts at all. A client that opens a connection and sends one byte a minute holds a goroutine and a file descriptor for as long as it likes.

srv := &http.Server{
    Addr:              ":8080",
    Handler:           mux,
    ReadHeaderTimeout: 10 * time.Second,
    IdleTimeout:       2 * time.Minute,
}

Which timeout does what

ReadHeaderTimeout limits the time to read the request line and headers. This is the one to always set: it stops slow-header clients and does not affect uploads or streaming.

ReadTimeout and WriteTimeout cover the whole request and the whole response. They are fine for an API with small bodies and wrong for anything that streams, because the deadline is fixed when the request starts and a long download simply gets cut.

IdleTimeout closes keep-alive connections that sit unused.

Per-request deadlines

When one handler needs a different limit, set it there instead of on the server:

rc := http.NewResponseController(w)
rc.SetWriteDeadline(time.Now().Add(30 * time.Second))

For work the handler starts itself, pass r.Context() down. It is cancelled when the client goes away, so abandoned requests stop costing anything.