Track in-flight requests with atomic counters

3303
0

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.