retry.go 691 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package util
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/util/log"
  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. log.Infof("retry %s successfully", name)
  16. }
  17. break
  18. }
  19. if strings.Contains(err.Error(), "transport") {
  20. hasErr = true
  21. log.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. }