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.

43 lines
733 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: err: %v", name, err)
  22. time.Sleep(waitTime)
  23. waitTime += waitTime / 2
  24. } else {
  25. break
  26. }
  27. }
  28. return err
  29. }
  30. // return the first non empty string
  31. func Nvl(values ...string) string {
  32. for _, s := range values {
  33. if s != "" {
  34. return s
  35. }
  36. }
  37. return ""
  38. }