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.

41 lines
699 B

4 years ago
  1. package util
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. )
  7. var RetryWaitTime = 6 * time.Second
  8. func Retry(name string, job func() error) (err error) {
  9. waitTime := time.Second
  10. hasErr := false
  11. for waitTime < RetryWaitTime {
  12. err = job()
  13. if err == nil {
  14. if hasErr {
  15. glog.V(0).Infof("retry %s successfully", name)
  16. }
  17. break
  18. }
  19. if strings.Contains(err.Error(), "transport") {
  20. hasErr = true
  21. glog.V(0).Infof("retry %s", name)
  22. time.Sleep(waitTime)
  23. waitTime += waitTime / 2
  24. }
  25. }
  26. return err
  27. }
  28. // return the first non empty string
  29. func Nvl(values ...string) string {
  30. for _, s := range values {
  31. if s != "" {
  32. return s
  33. }
  34. }
  35. return ""
  36. }