MCPcopy Create free account
hub / github.com/codealong-dev/ride-sharing / WithBackoff

Function WithBackoff

shared/retry/retry.go:29–58  ·  view source on GitHub ↗

WithBackoff executes the given operation with exponential backoff retry logic

(ctx context.Context, cfg Config, operation func() error)

Source from the content-addressed store, hash-verified

27
28// WithBackoff executes the given operation with exponential backoff retry logic
29func WithBackoff(ctx context.Context, cfg Config, operation func() error) error {
30 var err error
31 wait := cfg.InitialWait
32
33 for attempt := 0; attempt <= cfg.MaxRetries; attempt++ {
34 if attempt > 0 {
35 log.Printf("Retry attempt %d/%d after %v", attempt, cfg.MaxRetries, wait)
36
37 select {
38 case <-ctx.Done():
39 return ctx.Err()
40 case <-time.After(wait):
41 }
42
43 // Exponential backoff with max wait cap
44 wait *= 2
45 if wait > cfg.MaxWait {
46 wait = cfg.MaxWait
47 }
48 }
49
50 if err = operation(); err == nil {
51 return nil
52 }
53
54 log.Printf("Operation failed (attempt %d/%d): %v", attempt+1, cfg.MaxRetries, err)
55 }
56
57 return err
58}

Callers 1

ConsumeMessagesMethod · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected