mirror of
https://github.com/nais/wonderwall.git
synced 2026-05-21 15:52:54 +00:00
fix(retry): correct usage of MaxDuration, remove unused code
This commit is contained in:
@@ -7,52 +7,26 @@ import (
|
||||
"github.com/sethvargo/go-retry"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultBackoff = Fibonacci()
|
||||
RetryableError = retry.RetryableError
|
||||
var RetryableError = retry.RetryableError
|
||||
|
||||
const (
|
||||
baseDuration = 50 * time.Millisecond
|
||||
maxDuration = 1 * time.Second
|
||||
)
|
||||
|
||||
type FibonacciBackoff struct {
|
||||
Base time.Duration
|
||||
Max time.Duration
|
||||
}
|
||||
|
||||
func WithBaseDuration(base time.Duration) func(*FibonacciBackoff) {
|
||||
return func(f *FibonacciBackoff) {
|
||||
f.Base = base
|
||||
}
|
||||
}
|
||||
|
||||
func WithMaxDuration(max time.Duration) func(*FibonacciBackoff) {
|
||||
return func(f *FibonacciBackoff) {
|
||||
f.Max = max
|
||||
}
|
||||
}
|
||||
|
||||
func Fibonacci(opts ...func(f *FibonacciBackoff)) retry.Backoff {
|
||||
const DefaultBaseDuration = 50 * time.Millisecond
|
||||
const DefaultMaxDuration = 1 * time.Second
|
||||
|
||||
fb := &FibonacciBackoff{
|
||||
Base: DefaultBaseDuration,
|
||||
Max: DefaultMaxDuration,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(fb)
|
||||
}
|
||||
|
||||
b := retry.NewFibonacci(fb.Base)
|
||||
b = retry.WithMaxDuration(fb.Max, b)
|
||||
func fibonacci() retry.Backoff {
|
||||
b := retry.NewFibonacci(baseDuration)
|
||||
// beware: this starts a timer when invoked, on which the max duration is evaluated against
|
||||
b = retry.WithMaxDuration(maxDuration, b)
|
||||
return b
|
||||
}
|
||||
|
||||
// Do retries the given function using the DefaultBackoff strategy.
|
||||
func Do(ctx context.Context, f retry.RetryFunc) error {
|
||||
return retry.Do(ctx, DefaultBackoff, f)
|
||||
return retry.Do(ctx, fibonacci(), f)
|
||||
}
|
||||
|
||||
// DoValue is like Do, but returns a value of type T.
|
||||
func DoValue[T any](ctx context.Context, f retry.RetryFuncValue[T]) (T, error) {
|
||||
return retry.DoValue(ctx, DefaultBackoff, f)
|
||||
return retry.DoValue(ctx, fibonacci(), f)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user