mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-05 05:18:52 +08:00
fb493cc235
Signed-off-by: cai.zhang <cai.zhang@zilliz.com>
41 lines
925 B
Go
41 lines
925 B
Go
package indexservice
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
// Reference: https://blog.cyeam.com/golang/2018/08/27/retry
|
|
|
|
func RetryImpl(attempts int, sleep time.Duration, fn func() error, maxSleepTime time.Duration) error {
|
|
if err := fn(); err != nil {
|
|
if s, ok := err.(InterruptError); ok {
|
|
return s.error
|
|
}
|
|
|
|
if attempts--; attempts > 0 {
|
|
log.Printf("retry func error: %s. attempts #%d after %s.", err.Error(), attempts, sleep)
|
|
time.Sleep(sleep)
|
|
if sleep < maxSleepTime {
|
|
return RetryImpl(attempts, 2*sleep, fn, maxSleepTime)
|
|
}
|
|
return RetryImpl(attempts, maxSleepTime, fn, maxSleepTime)
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Retry(attempts int, sleep time.Duration, fn func() error) error {
|
|
maxSleepTime := time.Millisecond * 1000
|
|
return RetryImpl(attempts, sleep, fn, maxSleepTime)
|
|
}
|
|
|
|
type InterruptError struct {
|
|
error
|
|
}
|
|
|
|
func NoRetryError(err error) InterruptError {
|
|
return InterruptError{err}
|
|
}
|