Token bucket rate limiter for outbound calls

12032
0

Outbound rate limiting is one of those “quiet reliability” features: customers rarely notice it until it’s missing. I prefer a simple token bucket using golang.org/x/time/rate because it’s well-tested and easy to reason about. Each call waits for a token via limiter.Wait(ctx); if the request is canceled or the deadline is exceeded, the wait returns an error and we skip the downstream call. This makes throttling cooperative and consistent with request timeouts. I usually attach a limiter per dependency, not per handler, so one noisy endpoint doesn’t starve everything else. The pattern also makes it easy to tune in production by adjusting rate.Limit and burst size based on real metrics.