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