2020-11-01 16:11:08 +08:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-11-01 16:21:44 +08:00
|
|
|
"strings"
|
2020-11-01 16:11:08 +08:00
|
|
|
"time"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2020-11-01 16:11:08 +08:00
|
|
|
)
|
|
|
|
|
2020-11-01 18:36:43 +08:00
|
|
|
var RetryWaitTime = 6 * time.Second
|
|
|
|
|
|
|
|
func Retry(name string, job func() error) (err error) {
|
2020-11-01 16:11:08 +08:00
|
|
|
waitTime := time.Second
|
2020-11-01 16:40:16 +08:00
|
|
|
hasErr := false
|
2020-11-01 18:36:43 +08:00
|
|
|
for waitTime < RetryWaitTime {
|
2020-11-01 16:11:08 +08:00
|
|
|
err = job()
|
|
|
|
if err == nil {
|
2020-11-01 16:40:16 +08:00
|
|
|
if hasErr {
|
|
|
|
glog.V(0).Infof("retry %s successfully", name)
|
|
|
|
}
|
2023-05-08 14:39:33 +08:00
|
|
|
waitTime = time.Second
|
2020-11-01 16:11:08 +08:00
|
|
|
break
|
|
|
|
}
|
2020-11-01 16:40:16 +08:00
|
|
|
if strings.Contains(err.Error(), "transport") {
|
|
|
|
hasErr = true
|
2020-11-26 17:29:30 +08:00
|
|
|
glog.V(0).Infof("retry %s: err: %v", name, err)
|
|
|
|
} else {
|
|
|
|
break
|
2020-11-01 16:21:44 +08:00
|
|
|
}
|
2022-08-21 14:34:45 +08:00
|
|
|
time.Sleep(waitTime)
|
|
|
|
waitTime += waitTime / 2
|
2020-11-01 16:11:08 +08:00
|
|
|
}
|
|
|
|
return err
|
2020-11-01 18:36:43 +08:00
|
|
|
}
|
2020-11-16 08:58:48 +08:00
|
|
|
|
2023-04-21 15:09:31 +08:00
|
|
|
func MultiRetry(name string, errList []string, job func() error) (err error) {
|
|
|
|
waitTime := time.Second
|
|
|
|
hasErr := false
|
|
|
|
for waitTime < RetryWaitTime {
|
|
|
|
err = job()
|
|
|
|
if err == nil {
|
|
|
|
if hasErr {
|
|
|
|
glog.V(0).Infof("retry %s successfully", name)
|
|
|
|
}
|
2023-05-08 14:39:33 +08:00
|
|
|
waitTime = time.Second
|
2023-04-21 15:09:31 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if containErr(err.Error(), errList) {
|
|
|
|
hasErr = true
|
|
|
|
glog.V(0).Infof("retry %s: err: %v", name, err)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(waitTime)
|
|
|
|
waitTime += waitTime / 2
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-02 02:33:56 +08:00
|
|
|
// RetryUntil retries until the job returns no error or onErrFn returns false
|
|
|
|
func RetryUntil(name string, job func() error, onErrFn func(err error) (shouldContinue bool)) {
|
2021-08-16 03:37:35 +08:00
|
|
|
waitTime := time.Second
|
|
|
|
for {
|
2021-08-16 03:40:22 +08:00
|
|
|
err := job()
|
2021-08-16 03:37:35 +08:00
|
|
|
if err == nil {
|
2021-09-03 10:55:01 +08:00
|
|
|
waitTime = time.Second
|
2021-08-16 03:37:35 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if onErrFn(err) {
|
|
|
|
if strings.Contains(err.Error(), "transport") {
|
|
|
|
glog.V(0).Infof("retry %s: err: %v", name, err)
|
2021-08-16 11:06:47 +08:00
|
|
|
}
|
|
|
|
time.Sleep(waitTime)
|
|
|
|
if waitTime < RetryWaitTime {
|
|
|
|
waitTime += waitTime / 2
|
2021-08-16 03:37:35 +08:00
|
|
|
}
|
|
|
|
continue
|
2023-10-02 02:33:56 +08:00
|
|
|
} else {
|
|
|
|
break
|
2021-08-16 03:37:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:58:48 +08:00
|
|
|
// return the first non empty string
|
|
|
|
func Nvl(values ...string) string {
|
|
|
|
for _, s := range values {
|
|
|
|
if s != "" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2023-04-21 15:09:31 +08:00
|
|
|
|
|
|
|
func containErr(err string, errList []string) bool {
|
|
|
|
for _, e := range errList {
|
|
|
|
if strings.Contains(err, e) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|