mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-05 05:18:52 +08:00
7ee8623e1a
Signed-off-by: Xiangyu Wang <xiangyu.wang@zilliz.com>
33 lines
602 B
Go
33 lines
602 B
Go
package util
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
// Reference: https://blog.cyeam.com/golang/2018/08/27/retry
|
|
|
|
func Retry(attempts int, sleep time.Duration, fn func() error) 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)
|
|
return Retry(attempts, 2*sleep, fn)
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type InterruptError struct {
|
|
error
|
|
}
|
|
|
|
func NoRetryError(err error) InterruptError {
|
|
return InterruptError{err}
|
|
}
|