You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
541 B

  1. package util
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. )
  7. func Retry(name string, waitTimeLimit time.Duration, job func() error) (err error) {
  8. waitTime := time.Second
  9. hasErr := false
  10. for waitTime < waitTimeLimit {
  11. err = job()
  12. if err == nil {
  13. if hasErr {
  14. glog.V(0).Infof("retry %s successfully", name)
  15. }
  16. break
  17. }
  18. if strings.Contains(err.Error(), "transport") {
  19. hasErr = true
  20. glog.V(0).Infof("retry %s", name)
  21. time.Sleep(waitTime)
  22. waitTime += waitTime / 2
  23. }
  24. }
  25. return err
  26. }