go
31 lines · 1 tab
Leah Thompson
Jan 2026
1 tab
package retry
import (
"context"
"net/http"
"strconv"
"time"
)
func SleepRetryAfter(ctx context.Context, resp *http.Response, max time.Duration) bool {
v := resp.Header.Get("Retry-After")
if v == "" {
return false
}
secs, err := strconv.Atoi(v)
if err != nil {
return false
}
d := time.Duration(secs) * time.Second
if d > max {
d = max
}
t := time.NewTimer(d)
defer t.Stop()
select {
case <-ctx.Done():
return true
case <-t.C:
return true
}
}
1 file · go
Explain with highlit
Rate limits are normal in production; what matters is how clients behave when they hit them. Instead of hammering an upstream with immediate retries, I parse Retry-After (seconds) and sleep before retrying. I still keep an upper bound so one request doesn’t stall forever. The helper uses context.Context so cancellations stop the wait, which is important during shutdown or when the caller times out. I also treat 429 differently from 5xx: 429 means “you’re too fast,” so the right response is to slow down, not to fan out more retries. In practice I pair this with a token bucket limiter so the client stays under the rate most of the time. This is one of those small details that makes integrations stable.
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.