go

Capture status code and bytes written via ResponseWriter wrapper

When you need accurate request logs or metrics, you can’t rely on “what you intended to write” — you need what actually got written. Wrapping http.ResponseWriter to capture WriteHeader and count bytes is a simple way to record status codes and respons

Circuit breaker around flaky dependencies

Retries alone can make an outage worse: if a dependency is hard failing, retries just add load. A circuit breaker adds a simple state machine: closed (normal), open (fail fast), and half-open (probe). I like gobreaker because it’s small and predictabl

Gzip compression middleware with correct Vary header

Compressing responses is an easy bandwidth win for JSON APIs, but only when it's done carefully. I check Accept-Encoding for gzip, set Vary: Accept-Encoding so caches behave correctly, and stream output through gzip.Writer so we don't buffer full resp

CIDR allowlist middleware using netip (trust boundary explicit)

For internal admin endpoints, I often add a network allowlist in addition to auth. The tricky part is deciding which IP to trust: if you’re behind a proxy, you might need X-Forwarded-For, but only if the proxy is controlled by you. The middleware belo

Context-aware cache refresh with atomic.Pointer (Go 1.19+)

I often need a fast read path for small datasets (like a list of active plans or an allowlist) that updates periodically. Instead of locking on every read, I store a pointer to an immutable snapshot in atomic.Pointer. Reads are lock-free and safe; ref

Benchmarking hot paths (allocs and throughput)

When latency matters, I benchmark the smallest interesting unit and watch allocations. In Go, a surprising number of regressions come from accidental heap churn: converting []byte to string, building lots of temporary maps, or using fmt.Sprintf in loo

Enforce JSON Content-Type and method early in handlers

A lot of handler complexity disappears if you reject bad requests early. I enforce the HTTP method (POST, PUT, etc.) and require Content-Type: application/json before attempting to decode. This prevents confusing errors where clients send form-encoded

Singleflight cache fill to prevent thundering herd

When a cache key expires, it’s easy for a burst of requests to stampede the database. I use singleflight.Group to ensure only one goroutine performs the expensive fill per key while others wait for the shared result. This doesn’t replace proper TTLs o

Email delivery via HTTP provider with context, timeout, and idempotency

For transactional email, the reliability problem is usually latency and retries, not MIME formatting. I prefer an HTTP email provider because requests are easy to bound with context.WithTimeout and easier to observe than raw SMTP. The code below build

Optimistic locking with a version column

When multiple clients can update the same record, I prefer optimistic locking over heavy row locks. The idea is simple: every row has a version that increments on each update. The update statement includes WHERE id=$1 AND version=$2, so if someone els

Set a soft memory limit with debug.SetMemoryLimit

In containerized environments, the Go runtime can benefit from knowing the memory budget. With Go 1.19+, debug.SetMemoryLimit lets you set a soft limit so the GC reacts more aggressively before the process gets killed by the OOM killer. I treat this a

expvar counters for quick-and-dirty production introspection

Sometimes you want a quick diagnostic without wiring a full metrics stack. expvar exposes variables at /debug/vars in a standard JSON format. I use it for a handful of counters like “requeststotal” and “jobsfailed” when I’m bootstrapping a service or