| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- package httpClient
- import (
- "bytes"
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net/http"
- "net/url"
- "strings"
- "time"
- "go.uber.org/zap"
- )
- const (
- DefaultTimeout time.Duration = 5 // 默认超时时间5s
- )
- type HttpClient struct {
- reLogin *func() string
- }
- type HttpClientOptions struct {
- Timeout *time.Duration // 单位:s
- Header *http.Header
- Context context.Context
- }
- // PostBody 包含转换后的 Reader 和推荐的 Content-Type
- type PostBody struct {
- Reader io.Reader
- ContentType string
- }
- type ResponseData struct {
- Code int `json:"code"`
- }
- // 封装模块的响应结构数据
- type Response struct {
- Status string // e.g. "200 OK"
- StatusCode int // e.g. 200
- // Header maps header keys to values. If the response had multiple
- // headers with the same key, they may be concatenated, with comma
- // delimiters. (RFC 7230, section 3.2.2 requires that multiple headers
- // be semantically equivalent to a comma-delimited sequence.) When
- // Header values are duplicated by other fields in this struct (e.g.,
- // ContentLength, TransferEncoding, Trailer), the field values are
- // authoritative.
- //
- // Keys in the map are canonicalized (see CanonicalHeaderKey).
- Header http.Header
- // Body represents the response body.
- Body []byte
- // ContentLength records the length of the associated content. The
- // value -1 indicates that the length is unknown. Unless Request.Method
- // is "HEAD", values >= 0 indicate that the given number of bytes may
- // be read from Body.
- ContentLength int64
- }
- // var reLogin = func() string {
- // // empty function
- // return ""
- // }
- func (c *HttpClient) responseInterceptor(response *Response, retry func(*string) (*Response, error)) *Response {
- // 判断是否回复code 401
- // var data ResponseData
- // if err := json.Unmarshal(response.Body, &data); err != nil {
- // zap.L().Warn("ResponseData 解析失败", zap.String("Status", response.Status), zap.String("body", string(response.Body)), zap.String("err", err.Error()))
- // return response
- // }
- // if data.Code == 401 {
- // TODO 调整401判断逻辑,待观察是否有问题
- if response.StatusCode == 401 {
- if c.reLogin == nil {
- return response
- }
- tokenStr := (*c.reLogin)()
- retryBody, err := retry(&tokenStr)
- if err != nil {
- zap.L().Warn("http retry 失败(401 重新登录后重试)",
- zap.Int("tokenLen", len(tokenStr)), // 避免明文记录 token,仅记录长度
- zap.Int("originStatusCode", response.StatusCode),
- zap.String("err", err.Error()),
- )
- return response
- }
- return retryBody
- }
- return response
- }
- func (c *HttpClient) RegisterLogin(login func() string) {
- c.reLogin = &login
- }
- func (c *HttpClient) HttpGet(url string, params *url.Values, opt *HttpClientOptions) (*Response, error) {
- // 拼接参数到 URL
- finalURL := url
- if params != nil {
- finalURL = finalURL + "?" + params.Encode()
- }
- zap.L().Debug("HttpGet", zap.String("url", finalURL))
- // 创建请求
- requestCtx := context.Background()
- if opt != nil && opt.Context != nil {
- requestCtx = opt.Context
- }
- req, err := http.NewRequestWithContext(requestCtx,
- http.MethodGet,
- finalURL,
- nil)
- if err != nil {
- zap.L().Error("[HttpGet] NewRequest failed.",
- zap.String("method", http.MethodGet),
- zap.String("url", finalURL),
- zap.String("err", err.Error()),
- )
- return nil, err
- }
- // 设置 Header
- if opt != nil && opt.Header != nil {
- for key, values := range *opt.Header {
- for _, v := range values {
- req.Header.Add(key, v)
- }
- }
- }
- // 设置 Timeout
- timeout := DefaultTimeout
- if opt != nil && opt.Timeout != nil {
- timeout = *opt.Timeout
- }
- // 发送请求 - 封装成函数用于重试
- var reqFunc = func(token *string) (*Response, error) {
- client := &http.Client{Timeout: timeout * time.Second}
- resp, err := client.Do(req)
- if err != nil {
- zap.L().Error("[HttpGet] Do Send failed.",
- zap.String("method", http.MethodGet),
- zap.String("url", finalURL),
- zap.Duration("timeout", timeout*time.Second),
- zap.String("err", err.Error()),
- )
- return nil, err
- }
- defer resp.Body.Close()
- respBody, err := io.ReadAll(resp.Body)
- if err != nil {
- zap.L().Error("[HttpGet] Read response body failed.",
- zap.String("method", http.MethodGet),
- zap.String("url", finalURL),
- zap.String("status", resp.Status),
- zap.String("err", err.Error()),
- )
- return nil, err
- }
- // 响应体大小观测:定位单次上游响应是否异常巨大(内存放大源)。
- // 正常业务响应通常几十 KB 内;超过 5MB 视为异常,打 Warn。
- if len(respBody) > 5*1024*1024 {
- zap.L().Warn("[HttpGet] 响应体异常大",
- zap.String("url", finalURL),
- zap.Int("bytes", len(respBody)),
- zap.Int64("contentLength", resp.ContentLength),
- )
- }
- return &Response{
- Status: resp.Status,
- StatusCode: resp.StatusCode,
- Header: resp.Header,
- Body: respBody,
- ContentLength: resp.ContentLength,
- }, nil
- }
- response, err := reqFunc(nil)
- if err != nil {
- return nil, err
- }
- // 应答失败,直接返回
- if response.StatusCode != 200 {
- zap.L().Warn("[HttpGet] Response failed",
- zap.String("method", http.MethodGet),
- zap.String("url", finalURL),
- zap.String("status", response.Status),
- zap.Int("statusCode", response.StatusCode),
- zap.String("body", string(response.Body)),
- )
- return response, fmt.Errorf("[HttpGet] Response failed, url=%s, status=%s", finalURL, response.Status)
- }
- return c.responseInterceptor(response, reqFunc), nil
- }
- // toPostBody 将任意 body 转换为 http POST 可用的 io.Reader
- func toPostBody(body any) (*PostBody, error) {
- if body == nil {
- return &PostBody{Reader: nil, ContentType: ""}, nil
- }
- switch v := body.(type) {
- case io.Reader:
- // 直接传 io.Reader
- return &PostBody{Reader: v}, nil
- case string:
- // 字符串默认 text/plain
- return &PostBody{Reader: strings.NewReader(v), ContentType: "text/plain"}, nil
- case []byte:
- // []byte 默认 application/octet-stream
- return &PostBody{Reader: bytes.NewReader(v), ContentType: "application/octet-stream"}, nil
- case url.Values:
- // 表单类型 application/x-www-form-urlencoded
- return &PostBody{
- Reader: strings.NewReader(v.Encode()),
- ContentType: "application/x-www-form-urlencoded; charset=UTF-8",
- }, nil
- default:
- // 尝试序列化为 JSON
- jsonData, err := json.Marshal(v)
- if err != nil {
- return nil, errors.New("unsupported body type and failed to marshal to JSON")
- }
- return &PostBody{
- Reader: bytes.NewReader(jsonData),
- ContentType: "application/json",
- }, nil
- }
- }
- func (c *HttpClient) HttpPost(url string, body any, opt *HttpClientOptions) (*Response, error) {
- // 转换body数据
- payload, err := toPostBody(body)
- if err != nil {
- zap.L().Error("[HttpPost] to PostBody failed.",
- zap.String("method", http.MethodPost),
- zap.String("url", url),
- zap.String("bodyType", fmt.Sprintf("%T", body)),
- zap.String("err", err.Error()),
- )
- return nil, err
- }
- zap.L().Debug("HttpPost", zap.String("url", url), zap.String("contentType", payload.ContentType))
- // 创建请求
- requestCtx := context.Background()
- if opt != nil && opt.Context != nil {
- requestCtx = opt.Context
- }
- req, err := http.NewRequestWithContext(requestCtx,
- http.MethodPost,
- url,
- payload.Reader)
- if err != nil {
- zap.L().Error("[HttpPost] NewRequest failed.",
- zap.String("method", http.MethodPost),
- zap.String("url", url),
- zap.String("contentType", payload.ContentType),
- zap.String("err", err.Error()),
- )
- return nil, err
- }
- // 设置 Content-Type
- req.Header.Set("Content-Type", payload.ContentType)
- // 设置 Header
- if opt != nil && opt.Header != nil {
- for key, values := range *opt.Header {
- for _, v := range values {
- req.Header.Add(key, v)
- }
- }
- }
- // 设置 Timeout
- timeout := DefaultTimeout
- if opt != nil && opt.Timeout != nil {
- timeout = *opt.Timeout
- }
- // 发送请求 - 封装成函数用于重试
- var reqFunc = func(token *string) (*Response, error) {
- client := &http.Client{Timeout: timeout * time.Second}
- resp, err := client.Do(req)
- if err != nil {
- zap.L().Error("[HttpPost] Do Send failed.",
- zap.String("method", http.MethodPost),
- zap.String("url", url),
- zap.String("contentType", payload.ContentType),
- zap.Duration("timeout", timeout*time.Second),
- zap.String("err", err.Error()),
- )
- return nil, err
- }
- defer resp.Body.Close()
- respBody, err := io.ReadAll(resp.Body)
- if err != nil {
- zap.L().Error("[HttpPost] Read response body failed.",
- zap.String("method", http.MethodPost),
- zap.String("url", url),
- zap.String("status", resp.Status),
- zap.String("err", err.Error()),
- )
- return nil, err
- }
- return &Response{
- Status: resp.Status,
- StatusCode: resp.StatusCode,
- Header: resp.Header,
- Body: respBody,
- ContentLength: resp.ContentLength,
- }, nil
- }
- response, err := reqFunc(nil)
- if err != nil {
- return nil, err
- }
- // 应答失败,直接返回
- if response.StatusCode != 200 {
- zap.L().Warn("[HttpPost] Response failed",
- zap.String("method", http.MethodPost),
- zap.String("url", url),
- zap.String("contentType", payload.ContentType),
- zap.String("status", response.Status),
- zap.Int("statusCode", response.StatusCode),
- zap.String("body", string(response.Body)),
- )
- return response, fmt.Errorf("[HttpPost] Response failed, url=%s, status=%s", url, response.Status)
- }
- return c.responseInterceptor(response, reqFunc), nil
- }
|