httpClient.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package httpClient
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "time"
  13. "go.uber.org/zap"
  14. )
  15. const (
  16. DefaultTimeout time.Duration = 5 // 默认超时时间5s
  17. )
  18. type HttpClient struct {
  19. reLogin *func() string
  20. }
  21. type HttpClientOptions struct {
  22. Timeout *time.Duration // 单位:s
  23. Header *http.Header
  24. Context context.Context
  25. }
  26. // PostBody 包含转换后的 Reader 和推荐的 Content-Type
  27. type PostBody struct {
  28. Reader io.Reader
  29. ContentType string
  30. }
  31. type ResponseData struct {
  32. Code int `json:"code"`
  33. }
  34. // 封装模块的响应结构数据
  35. type Response struct {
  36. Status string // e.g. "200 OK"
  37. StatusCode int // e.g. 200
  38. // Header maps header keys to values. If the response had multiple
  39. // headers with the same key, they may be concatenated, with comma
  40. // delimiters. (RFC 7230, section 3.2.2 requires that multiple headers
  41. // be semantically equivalent to a comma-delimited sequence.) When
  42. // Header values are duplicated by other fields in this struct (e.g.,
  43. // ContentLength, TransferEncoding, Trailer), the field values are
  44. // authoritative.
  45. //
  46. // Keys in the map are canonicalized (see CanonicalHeaderKey).
  47. Header http.Header
  48. // Body represents the response body.
  49. Body []byte
  50. // ContentLength records the length of the associated content. The
  51. // value -1 indicates that the length is unknown. Unless Request.Method
  52. // is "HEAD", values >= 0 indicate that the given number of bytes may
  53. // be read from Body.
  54. ContentLength int64
  55. }
  56. // var reLogin = func() string {
  57. // // empty function
  58. // return ""
  59. // }
  60. func (c *HttpClient) responseInterceptor(response *Response, retry func(*string) (*Response, error)) *Response {
  61. // 判断是否回复code 401
  62. // var data ResponseData
  63. // if err := json.Unmarshal(response.Body, &data); err != nil {
  64. // zap.L().Warn("ResponseData 解析失败", zap.String("Status", response.Status), zap.String("body", string(response.Body)), zap.String("err", err.Error()))
  65. // return response
  66. // }
  67. // if data.Code == 401 {
  68. // TODO 调整401判断逻辑,待观察是否有问题
  69. if response.StatusCode == 401 {
  70. if c.reLogin == nil {
  71. return response
  72. }
  73. tokenStr := (*c.reLogin)()
  74. retryBody, err := retry(&tokenStr)
  75. if err != nil {
  76. zap.L().Warn("http retry 失败(401 重新登录后重试)",
  77. zap.Int("tokenLen", len(tokenStr)), // 避免明文记录 token,仅记录长度
  78. zap.Int("originStatusCode", response.StatusCode),
  79. zap.String("err", err.Error()),
  80. )
  81. return response
  82. }
  83. return retryBody
  84. }
  85. return response
  86. }
  87. func (c *HttpClient) RegisterLogin(login func() string) {
  88. c.reLogin = &login
  89. }
  90. func (c *HttpClient) HttpGet(url string, params *url.Values, opt *HttpClientOptions) (*Response, error) {
  91. // 拼接参数到 URL
  92. finalURL := url
  93. if params != nil {
  94. finalURL = finalURL + "?" + params.Encode()
  95. }
  96. zap.L().Debug("HttpGet", zap.String("url", finalURL))
  97. // 创建请求
  98. requestCtx := context.Background()
  99. if opt != nil && opt.Context != nil {
  100. requestCtx = opt.Context
  101. }
  102. req, err := http.NewRequestWithContext(requestCtx,
  103. http.MethodGet,
  104. finalURL,
  105. nil)
  106. if err != nil {
  107. zap.L().Error("[HttpGet] NewRequest failed.",
  108. zap.String("method", http.MethodGet),
  109. zap.String("url", finalURL),
  110. zap.String("err", err.Error()),
  111. )
  112. return nil, err
  113. }
  114. // 设置 Header
  115. if opt != nil && opt.Header != nil {
  116. for key, values := range *opt.Header {
  117. for _, v := range values {
  118. req.Header.Add(key, v)
  119. }
  120. }
  121. }
  122. // 设置 Timeout
  123. timeout := DefaultTimeout
  124. if opt != nil && opt.Timeout != nil {
  125. timeout = *opt.Timeout
  126. }
  127. // 发送请求 - 封装成函数用于重试
  128. var reqFunc = func(token *string) (*Response, error) {
  129. client := &http.Client{Timeout: timeout * time.Second}
  130. resp, err := client.Do(req)
  131. if err != nil {
  132. zap.L().Error("[HttpGet] Do Send failed.",
  133. zap.String("method", http.MethodGet),
  134. zap.String("url", finalURL),
  135. zap.Duration("timeout", timeout*time.Second),
  136. zap.String("err", err.Error()),
  137. )
  138. return nil, err
  139. }
  140. defer resp.Body.Close()
  141. respBody, err := io.ReadAll(resp.Body)
  142. if err != nil {
  143. zap.L().Error("[HttpGet] Read response body failed.",
  144. zap.String("method", http.MethodGet),
  145. zap.String("url", finalURL),
  146. zap.String("status", resp.Status),
  147. zap.String("err", err.Error()),
  148. )
  149. return nil, err
  150. }
  151. // 响应体大小观测:定位单次上游响应是否异常巨大(内存放大源)。
  152. // 正常业务响应通常几十 KB 内;超过 5MB 视为异常,打 Warn。
  153. if len(respBody) > 5*1024*1024 {
  154. zap.L().Warn("[HttpGet] 响应体异常大",
  155. zap.String("url", finalURL),
  156. zap.Int("bytes", len(respBody)),
  157. zap.Int64("contentLength", resp.ContentLength),
  158. )
  159. }
  160. return &Response{
  161. Status: resp.Status,
  162. StatusCode: resp.StatusCode,
  163. Header: resp.Header,
  164. Body: respBody,
  165. ContentLength: resp.ContentLength,
  166. }, nil
  167. }
  168. response, err := reqFunc(nil)
  169. if err != nil {
  170. return nil, err
  171. }
  172. // 应答失败,直接返回
  173. if response.StatusCode != 200 {
  174. zap.L().Warn("[HttpGet] Response failed",
  175. zap.String("method", http.MethodGet),
  176. zap.String("url", finalURL),
  177. zap.String("status", response.Status),
  178. zap.Int("statusCode", response.StatusCode),
  179. zap.String("body", string(response.Body)),
  180. )
  181. return response, fmt.Errorf("[HttpGet] Response failed, url=%s, status=%s", finalURL, response.Status)
  182. }
  183. return c.responseInterceptor(response, reqFunc), nil
  184. }
  185. // toPostBody 将任意 body 转换为 http POST 可用的 io.Reader
  186. func toPostBody(body any) (*PostBody, error) {
  187. if body == nil {
  188. return &PostBody{Reader: nil, ContentType: ""}, nil
  189. }
  190. switch v := body.(type) {
  191. case io.Reader:
  192. // 直接传 io.Reader
  193. return &PostBody{Reader: v}, nil
  194. case string:
  195. // 字符串默认 text/plain
  196. return &PostBody{Reader: strings.NewReader(v), ContentType: "text/plain"}, nil
  197. case []byte:
  198. // []byte 默认 application/octet-stream
  199. return &PostBody{Reader: bytes.NewReader(v), ContentType: "application/octet-stream"}, nil
  200. case url.Values:
  201. // 表单类型 application/x-www-form-urlencoded
  202. return &PostBody{
  203. Reader: strings.NewReader(v.Encode()),
  204. ContentType: "application/x-www-form-urlencoded; charset=UTF-8",
  205. }, nil
  206. default:
  207. // 尝试序列化为 JSON
  208. jsonData, err := json.Marshal(v)
  209. if err != nil {
  210. return nil, errors.New("unsupported body type and failed to marshal to JSON")
  211. }
  212. return &PostBody{
  213. Reader: bytes.NewReader(jsonData),
  214. ContentType: "application/json",
  215. }, nil
  216. }
  217. }
  218. func (c *HttpClient) HttpPost(url string, body any, opt *HttpClientOptions) (*Response, error) {
  219. // 转换body数据
  220. payload, err := toPostBody(body)
  221. if err != nil {
  222. zap.L().Error("[HttpPost] to PostBody failed.",
  223. zap.String("method", http.MethodPost),
  224. zap.String("url", url),
  225. zap.String("bodyType", fmt.Sprintf("%T", body)),
  226. zap.String("err", err.Error()),
  227. )
  228. return nil, err
  229. }
  230. zap.L().Debug("HttpPost", zap.String("url", url), zap.String("contentType", payload.ContentType))
  231. // 创建请求
  232. requestCtx := context.Background()
  233. if opt != nil && opt.Context != nil {
  234. requestCtx = opt.Context
  235. }
  236. req, err := http.NewRequestWithContext(requestCtx,
  237. http.MethodPost,
  238. url,
  239. payload.Reader)
  240. if err != nil {
  241. zap.L().Error("[HttpPost] NewRequest failed.",
  242. zap.String("method", http.MethodPost),
  243. zap.String("url", url),
  244. zap.String("contentType", payload.ContentType),
  245. zap.String("err", err.Error()),
  246. )
  247. return nil, err
  248. }
  249. // 设置 Content-Type
  250. req.Header.Set("Content-Type", payload.ContentType)
  251. // 设置 Header
  252. if opt != nil && opt.Header != nil {
  253. for key, values := range *opt.Header {
  254. for _, v := range values {
  255. req.Header.Add(key, v)
  256. }
  257. }
  258. }
  259. // 设置 Timeout
  260. timeout := DefaultTimeout
  261. if opt != nil && opt.Timeout != nil {
  262. timeout = *opt.Timeout
  263. }
  264. // 发送请求 - 封装成函数用于重试
  265. var reqFunc = func(token *string) (*Response, error) {
  266. client := &http.Client{Timeout: timeout * time.Second}
  267. resp, err := client.Do(req)
  268. if err != nil {
  269. zap.L().Error("[HttpPost] Do Send failed.",
  270. zap.String("method", http.MethodPost),
  271. zap.String("url", url),
  272. zap.String("contentType", payload.ContentType),
  273. zap.Duration("timeout", timeout*time.Second),
  274. zap.String("err", err.Error()),
  275. )
  276. return nil, err
  277. }
  278. defer resp.Body.Close()
  279. respBody, err := io.ReadAll(resp.Body)
  280. if err != nil {
  281. zap.L().Error("[HttpPost] Read response body failed.",
  282. zap.String("method", http.MethodPost),
  283. zap.String("url", url),
  284. zap.String("status", resp.Status),
  285. zap.String("err", err.Error()),
  286. )
  287. return nil, err
  288. }
  289. return &Response{
  290. Status: resp.Status,
  291. StatusCode: resp.StatusCode,
  292. Header: resp.Header,
  293. Body: respBody,
  294. ContentLength: resp.ContentLength,
  295. }, nil
  296. }
  297. response, err := reqFunc(nil)
  298. if err != nil {
  299. return nil, err
  300. }
  301. // 应答失败,直接返回
  302. if response.StatusCode != 200 {
  303. zap.L().Warn("[HttpPost] Response failed",
  304. zap.String("method", http.MethodPost),
  305. zap.String("url", url),
  306. zap.String("contentType", payload.ContentType),
  307. zap.String("status", response.Status),
  308. zap.Int("statusCode", response.StatusCode),
  309. zap.String("body", string(response.Body)),
  310. )
  311. return response, fmt.Errorf("[HttpPost] Response failed, url=%s, status=%s", url, response.Status)
  312. }
  313. return c.responseInterceptor(response, reqFunc), nil
  314. }