retry.go 767 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package httpclient
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. )
  7. const (
  8. // DefaultRetryTimes 如果请求失败,最多重试3次
  9. DefaultRetryTimes = 3
  10. // DefaultRetryDelay 在重试前,延迟等待100毫秒
  11. DefaultRetryDelay = time.Millisecond * 100
  12. )
  13. // Verify parse the body and verify that it is correct
  14. type RetryVerify func(body []byte) (shouldRetry bool)
  15. func shouldRetry(ctx context.Context, httpCode int) bool {
  16. select {
  17. case <-ctx.Done():
  18. return false
  19. default:
  20. }
  21. switch httpCode {
  22. case
  23. _StatusReadRespErr,
  24. _StatusDoReqErr,
  25. http.StatusRequestTimeout,
  26. http.StatusLocked,
  27. http.StatusTooEarly,
  28. http.StatusTooManyRequests,
  29. http.StatusServiceUnavailable,
  30. http.StatusGatewayTimeout:
  31. return true
  32. default:
  33. return false
  34. }
  35. }