package req
import (
"math"
"math/rand"
"time"
)
func defaultGetRetryInterval(resp *Response , attempt int ) time .Duration {
return 100 * time .Millisecond
}
type RetryConditionFunc func (resp *Response , err error ) bool
type RetryHookFunc func (resp *Response , err error )
type GetRetryIntervalFunc func (resp *Response , attempt int ) time .Duration
func backoffInterval(min , max time .Duration ) GetRetryIntervalFunc {
base := float64 (min )
capLevel := float64 (max )
return func (resp *Response , attempt int ) time .Duration {
temp := math .Min (capLevel , base *math .Exp2 (float64 (attempt )))
halfTemp := int64 (temp / 2 )
sleep := halfTemp + rand .Int63n (halfTemp )
return time .Duration (sleep )
}
}
func newDefaultRetryOption() *RetryOption {
return &RetryOption {
GetRetryInterval : defaultGetRetryInterval ,
}
}
type RetryOption struct {
MaxRetries int
GetRetryInterval GetRetryIntervalFunc
RetryConditions []RetryConditionFunc
RetryHooks []RetryHookFunc
}
func (ro *RetryOption ) Clone () *RetryOption {
if ro == nil {
return nil
}
o := &RetryOption {
MaxRetries : ro .MaxRetries ,
GetRetryInterval : ro .GetRetryInterval ,
}
o .RetryConditions = append (o .RetryConditions , ro .RetryConditions ...)
o .RetryHooks = append (o .RetryHooks , ro .RetryHooks ...)
return o
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .