2016-04-05 20:04:37 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2022-03-27 00:16:22 +03:00
|
|
|
"math"
|
2016-04-05 20:04:37 +01:00
|
|
|
"time"
|
2020-02-02 23:32:55 +03:00
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/util/backoff"
|
2016-04-05 20:04:37 +01:00
|
|
|
)
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// BackoffFunc is the backoff call func
|
2016-04-05 20:04:37 +01:00
|
|
|
type BackoffFunc func(ctx context.Context, req Request, attempts int) (time.Duration, error)
|
|
|
|
|
2022-03-27 00:16:22 +03:00
|
|
|
// BackoffExp using exponential backoff func
|
|
|
|
func BackoffExp(_ context.Context, _ Request, attempts int) (time.Duration, error) {
|
2020-02-02 23:32:55 +03:00
|
|
|
return backoff.Do(attempts), nil
|
2016-04-05 20:04:37 +01:00
|
|
|
}
|
2022-03-27 00:16:22 +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) {
|
2022-04-16 16:36:15 +03:00
|
|
|
td := time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100
|
2022-03-27 00:16:22 +03:00
|
|
|
if td < min {
|
|
|
|
return min, nil
|
|
|
|
} else if td > max {
|
|
|
|
return max, nil
|
|
|
|
}
|
|
|
|
return td, nil
|
|
|
|
}
|
|
|
|
}
|