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"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
}
|
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
|
|
|
|
glog.V(0).Infof("retry %s", name)
|
2020-11-01 16:21:44 +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
|
|
|
}
|