Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions internal/retry/retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package retry

import (
"context"
"time"
)

// Policy configures automatic retry behavior for failed payment operations.
type Policy struct {
MaxAttempts int
Backoff time.Duration
}

// DefaultPolicy retries up to 3 times with a 2 second backoff between
// attempts.
var DefaultPolicy = Policy{MaxAttempts: 3, Backoff: 2 * time.Second}

// Do runs fn, retrying on error according to the policy. It stops retrying
// once ctx is done and returns the last error encountered.
func (p Policy) Do(ctx context.Context, fn func() error) error {
var err error
for attempt := 1; attempt <= p.MaxAttempts; attempt++ {
if err = fn(); err == nil {
return nil
}

if attempt == p.MaxAttempts {
break
}

select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(p.Backoff):
}
}
return err
}
Loading