http_client.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package client
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. logger "eta/eta_mini_ht_api/common/component/log"
  7. "fmt"
  8. "io"
  9. "log"
  10. "net/http"
  11. "strings"
  12. "time"
  13. )
  14. type HttpClient struct {
  15. *http.Client
  16. maxRetries int
  17. retryDelayFunc RetryDelayFunc
  18. }
  19. // NewClient 构造函数,其中 delayFunc 参数是可选的
  20. func NewClient(timeout time.Duration, maxRetries int, delayFunc ...RetryDelayFunc) *HttpClient {
  21. var df RetryDelayFunc
  22. if len(delayFunc) > 0 {
  23. df = delayFunc[0]
  24. } else {
  25. df = defaultRetryDelayFunc
  26. }
  27. return &HttpClient{
  28. Client: &http.Client{Timeout: timeout},
  29. maxRetries: maxRetries,
  30. retryDelayFunc: df,
  31. }
  32. }
  33. func DefaultClient() *HttpClient {
  34. return NewClient(time.Second*10, 3)
  35. }
  36. func defaultRetryDelayFunc(attempt int) time.Duration {
  37. delay := time.Duration(attempt) * time.Second
  38. if attempt > 0 {
  39. delay *= 2
  40. }
  41. return delay
  42. }
  43. type RetryDelayFunc func(attempt int) time.Duration
  44. func retryErr(err error) bool {
  45. return errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled)
  46. }
  47. // DoWithRetry 发送带有重试机制的HTTP请求,允许用户自定义重试延迟逻辑
  48. func (hc *HttpClient) DoWithRetry(ctx context.Context, req *http.Request) (resp *http.Response, err error) {
  49. attempt := 0
  50. for {
  51. resp, err = hc.Do(req.WithContext(ctx))
  52. if err != nil && retryErr(err) {
  53. if attempt >= hc.maxRetries {
  54. return nil, fmt.Errorf("请求失败: %w", err)
  55. }
  56. attempt++
  57. delay := hc.retryDelayFunc(attempt)
  58. time.Sleep(delay)
  59. continue
  60. }
  61. return
  62. }
  63. }
  64. func (hc *HttpClient) Post(url string, data interface{}) (resp *http.Response, err error) {
  65. dataStr, err := json.Marshal(data)
  66. if err != nil {
  67. logger.Error("请求data json序列化失败,err:" + err.Error())
  68. }
  69. body := io.NopCloser(strings.NewReader(string(dataStr)))
  70. req, err := http.NewRequest(http.MethodPost, url, body)
  71. req.Header.Set("Content-Type", "application/json")
  72. if err != nil {
  73. logger.Error("创建POST请求失败: %v", err)
  74. }
  75. resp, err = hc.DoWithRetry(req.Context(), req)
  76. code := resp.StatusCode
  77. if code != 200 {
  78. logger.Error("请求错误应答,状态码:%d", code)
  79. errMsg := fmt.Sprintf("请求状态码异常,StatusCode:[%d]", code)
  80. respBody, respErr := io.ReadAll(resp.Body)
  81. if respErr != nil {
  82. logger.Error("读取body失败,err:%v", err)
  83. err = errors.New(errMsg)
  84. return
  85. }
  86. logger.Error("请求错误应答,body:%s", string(respBody))
  87. errMsg = fmt.Sprintf("%s,body:%s", errMsg, string(respBody))
  88. err = errors.New(errMsg)
  89. return
  90. }
  91. return
  92. }
  93. func (hc *HttpClient) Get(url string) (resp *http.Response, err error) {
  94. req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
  95. if err != nil {
  96. log.Fatalf("创建请求失败: %v", err)
  97. }
  98. resp, err = hc.DoWithRetry(req.Context(), req)
  99. return
  100. }