go 20 lines · 1 tab

Track in-flight requests with atomic counters

Leah Thompson Jan 2026
1 tab
package middleware

import (
  "net/http"
  "sync/atomic"
)

type Gauge struct{ n atomic.Int64 }

func (g *Gauge) Value() int64 { return g.n.Load() }

func InFlight(g *Gauge) func(http.Handler) http.Handler {
  return func(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      g.n.Add(1)
      defer g.n.Add(-1)
      next.ServeHTTP(w, r)
    })
  }
}
1 file · go Explain with highlit

In-flight request count is a simple but powerful saturation signal. I keep an atomic.Int64 gauge that increments at the start of a request and decrements in a defer, which makes it robust even on early returns. This gauge can be exported via expvar, Prometheus, or logs at intervals. The key is correctness: always decrement, even if the handler panics (pair it with a recovery middleware) and always use atomic operations so you don’t introduce locks in the hottest path. In production, I use this in dashboards alongside p95 latency and error rate; when latency rises and in-flight rises, it’s often a sign of downstream contention. It’s also useful during deploys to confirm draining behavior: in-flight should fall to zero before shutdown completes. Small instrumentation like this goes a long way when you’re debugging under pressure.


Related snips

Share this code

Here's the card — post it anywhere.

Track in-flight requests with atomic counters — share card
Link copied