Retry Jitter Exponential Backoff
Retry transient failures with capped exponential backoff plus jitter, classifying errors by retryability and giving up with context.
Code
package main
import (
"context"
"errors"
"fmt"
"math/rand"
"time"
)
var (
errTransient = errors.New("transient upstream failure")
errPermanent = errors.New("invalid argument")
)
func classify(err error) bool {
if errors.Is(err, errPermanent) {
return false // 永久错误不值得重试
}
return errors.Is(err, errTransient) || isNetworkErr(err)
}
func isNetworkErr(err error) bool {
var netErr interface{ Timeout() bool }
return errors.As(err, &netErr)
}
func backoff(attempt int, base, cap time.Duration) time.Duration {
exp := base << attempt // base * 2^attempt
if exp > cap || exp <= 0 {
exp = cap
}
jitter := time.Duration(rand.Int63n(int64(exp) / 2))
return exp - jitter // 全抖动:避免多客户端同步重试
}
func withRetry(ctx context.Context, fn func() error, maxAttempts int) error {
var lastErr error
for attempt := 0; attempt < maxAttempts; attempt++ {
if err := ctx.Err(); err != nil {
return fmt.Errorf("context done: %w", err)
}
if err := fn(); err == nil {
return nil
} else {
lastErr = err
if !classify(err) {
return fmt.Errorf("permanent failure: %w", err)
}
}
if attempt < maxAttempts-1 {
wait := backoff(attempt, 100*time.Millisecond, 5*time.Second)
select {
case <-time.After(wait):
case <-ctx.Done():
return ctx.Err()
}
}
}
return fmt.Errorf("gave up after %d attempts: %w", maxAttempts, lastErr)
}
func main() {
calls := 0
err := withRetry(context.Background(), func() error {
calls++
if calls < 3 {
return errTransient
}
return nil
}, 5)
fmt.Println(err, "calls:", calls)
}What the code does
The snippet is organized around the functions classify, isNetworkErr, backoff and withRetry.
Repetition is confined to for attempt := 0; attempt < maxAttempts; attempt++ — the rest of the file is straight-line. Every fallible call checks err != nil (1 site), so an error is handled where it happens rather than bubbling invisibly.
Points of care
- Errors are ordinary return values; skipping the
errcheck means using a zero value silently.
Using it
Signature: func classify(err error) bool
main sequences withRetry; executing the file runs that flow once.
Key statements
return fmt.Errorf("gave up after %d attempts: %w", maxAttempts, lastErr)
return errors.Is(err, errTransient) || isNetworkErr(err)
return fmt.Errorf("permanent failure: %w", err)
Interactive Tools for This Topic
FAQ
- What happens when a call fails?
- The Go way: `err` comes back from each call, and 1 checkpoint here test it before proceeding.
- How do you run Retry Jitter Exponential Backoff?
- Execute it straight away — the program needs nothing beyond the runtime.
Report a problem with this exampleOpens a pre-filled issue on GitHub.