micro/client/backoff.go

31 lines
824 B
Go
Raw Permalink Normal View History

2016-04-05 22:04:37 +03:00
package client
import (
2018-03-03 14:53:52 +03:00
"context"
"math"
2016-04-05 22:04:37 +03:00
"time"
"go.unistack.org/micro/v3/util/backoff"
2016-04-05 22:04:37 +03:00
)
// BackoffFunc is the backoff call func
2016-04-05 22:04:37 +03:00
type BackoffFunc func(ctx context.Context, req Request, attempts int) (time.Duration, error)
// BackoffExp using exponential backoff func
func BackoffExp(_ context.Context, _ Request, attempts int) (time.Duration, error) {
return backoff.Do(attempts), nil
2016-04-05 22:04:37 +03:00
}
// BackoffInterval specifies randomization interval for backoff func
func BackoffInterval(min time.Duration, max time.Duration) BackoffFunc {
return func(_ context.Context, _ Request, attempts int) (time.Duration, error) {
td := time.Duration(time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100)
if td < min {
return min, nil
} else if td > max {
return max, nil
}
return td, nil
}
}