Files
commerce/util/retry/retry.go
zeekay 1943f1c093 Hanzo Commerce
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans,
usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0.

Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
2026-09-23 21:52:16 -07:00

47 lines
823 B
Go

package retry
import (
"time"
"github.com/cenkalti/backoff"
"github.com/hanzoai/commerce/log"
)
func Retry(times int, fn func() error) error {
b := backoff.NewExponentialBackOff()
b.InitialInterval = 3 * time.Second
b.RandomizationFactor = 0.1
b.Multiplier = 2.0
b.MaxInterval = 12 * time.Second
b.MaxElapsedTime = 5 * time.Minute
ticker := backoff.NewTicker(b)
var err error
tries := 0
for _ = range ticker.C {
// Give up after so many tries
if tries > times {
ticker.Stop()
break
}
tries = tries + 1
// Try and do something that might fail, retry if it does.
if err = fn(); err != nil {
if tries > 1 {
// Warn about > 1 retries.
log.Debug("%v (%d tries, will retry...)", err, tries)
}
continue
}
// Holy shit it worked
ticker.Stop()
break
}
return err
}