go
24 lines · 1 tab
Leah Thompson
Jan 2026
1 tab
package deps
import (
"context"
"net/http"
"golang.org/x/time/rate"
)
type LimitedClient struct {
client *http.Client
limiter *rate.Limiter
}
func NewLimitedClient(client *http.Client, r rate.Limit, burst int) *LimitedClient {
return &LimitedClient{client: client, limiter: rate.NewLimiter(r, burst)}
}
func (c *LimitedClient) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
if err := c.limiter.Wait(ctx); err != nil {
return nil, err
}
return c.client.Do(req.WithContext(ctx))
}
1 file · go
Explain with highlit
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.
Related snips
go
package api
import (
"net/http"
"runtime/debug"
)
Expose build metadata for debugging deploys
go
observability
build
by Leah Thompson
1 tab
typescript
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
typescript
reliability
retry
by codesnips
2 tabs
go
package dbutil
import (
"context"
"github.com/jackc/pgconn"
Retry Postgres serialization failures with bounded attempts
go
postgres
transactions
by Leah Thompson
1 tab
go
package files
import (
"context"
"time"
Presigned S3 upload URLs (AWS SDK v2)
go
aws
s3
by Leah Thompson
1 tab
go
package deps
import (
"net"
"net/http"
"time"
HTTP client tuned for production: timeouts, transport, and connection reuse
go
http
client
by Leah Thompson
1 tab
go
package api
import (
"io"
"net/http"
"os"
Safe multipart uploads using temp files (bounded memory)
go
http
uploads
by Leah Thompson
1 tab
Share this code
Here's the card — post it anywhere.