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)
})
}
}
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
package api
import (
"net/http"
"runtime/debug"
)
Expose build metadata for debugging deploys
export interface RetryOptions {
retries: number;
baseMs: number;
maxMs: number;
signal?: AbortSignal;
onRetry?: (attempt: number, delay: number, err: unknown) => void;
Exponential backoff with jitter for retries
package dbutil
import (
"context"
"github.com/jackc/pgconn"
Retry Postgres serialization failures with bounded attempts
use crossbeam::channel::unbounded;
use std::thread;
fn main() {
let (tx, rx) = unbounded();
Crossbeam for advanced concurrent data structures
// Creating a Promise
const myPromise = new Promise((resolve, reject) => {
const success = true;
setTimeout(() => {
if (success) {
Promises and async/await patterns for asynchronous JavaScript
use tracing::{info, instrument};
#[instrument]
fn process_request(user_id: u64) {
info!(user_id, "Processing request");
// Work happens here
tracing for structured logging and distributed tracing
Share this code
Here's the card — post it anywhere.