seaweedfs/weed/util/retry.go

44 lines
733 B
Go
Raw Normal View History

package util
import (
2020-11-01 08:21:44 +00:00
"strings"
"time"
"github.com/chrislusf/seaweedfs/weed/glog"
)
2020-11-01 10:36:43 +00:00
var RetryWaitTime = 6 * time.Second
func Retry(name string, job func() error) (err error) {
waitTime := time.Second
2020-11-01 08:40:16 +00:00
hasErr := false
2020-11-01 10:36:43 +00:00
for waitTime < RetryWaitTime {
err = job()
if err == nil {
2020-11-01 08:40:16 +00:00
if hasErr {
glog.V(0).Infof("retry %s successfully", name)
}
break
}
2020-11-01 08:40:16 +00:00
if strings.Contains(err.Error(), "transport") {
hasErr = true
glog.V(0).Infof("retry %s: err: %v", name, err)
2020-11-01 08:21:44 +00:00
time.Sleep(waitTime)
waitTime += waitTime / 2
} else {
break
2020-11-01 08:21:44 +00:00
}
}
return err
2020-11-01 10:36:43 +00:00
}
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 ""
}