httpclient/client.go

432 lines
13 KiB
Go

// Package httpclient ...
//
// Description : httpclient ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-05-31 15:22
package httpclient
import (
"fmt"
"git.zhangdeman.cn/gateway/httpclient/cache"
"git.zhangdeman.cn/gateway/httpclient/define"
"git.zhangdeman.cn/gateway/httpclient/validate"
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
"net/http"
"strings"
"time"
)
// NewHttpClient 获取http client
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:27 2024/5/31
func NewHttpClient(reqConfig *define.Request, cacheInstance cache.ICache) (*HttpClient, error) {
// 验证配置正确性以及初始化默认值
if err := validate.RequestConfig(reqConfig); nil != err {
return nil, err
}
restyClient, restyRequest := NewRestyClient(reqConfig)
hc := &HttpClient{
Client: restyClient,
request: restyRequest,
reqConfig: reqConfig,
http4xxHandler: make([]define.Http4xxHandler, 0),
http5xxHandler: make([]define.Http5xxHandler, 0),
httpBusinessErrorHandler: make([]define.HttpBusinessErrorHandler, 0),
requestFinishHandler: make([]define.RequestFinishHandler, 0),
cacheInstance: cacheInstance,
}
return hc, nil
}
// HttpClient 请求客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:27 2024/5/31
type HttpClient struct {
*resty.Client
request *resty.Request
reqConfig *define.Request
http4xxHandler []define.Http4xxHandler
http5xxHandler []define.Http5xxHandler
httpBusinessErrorHandler []define.HttpBusinessErrorHandler
requestSendErrorHandler []define.RequestSendErrorHandler
requestFinishHandler []define.RequestFinishHandler
cacheInstance cache.ICache
}
// OnResponse4xx 4xx处理逻辑
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:20 2024/6/1
func (hc *HttpClient) OnResponse4xx(handlerList ...define.Http4xxHandler) {
hc.http4xxHandler = append(hc.http4xxHandler, handlerList...)
}
// OnResponse5xx 5xx处理逻辑
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:21 2024/6/1
func (hc *HttpClient) OnResponse5xx(handlerList ...define.Http5xxHandler) {
hc.http5xxHandler = append(hc.http5xxHandler, handlerList...)
}
// OnResponseBusinessError 业务错误出路逻辑
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:22 2024/6/1
func (hc *HttpClient) OnResponseBusinessError(handlerList ...define.HttpBusinessErrorHandler) {
hc.httpBusinessErrorHandler = append(hc.httpBusinessErrorHandler, handlerList...)
}
// OnRequestFinish 请求完成时间
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:36 2024/6/1
func (hc *HttpClient) OnRequestFinish(handlerList ...define.RequestFinishHandler) {
hc.requestFinishHandler = append(hc.requestFinishHandler, handlerList...)
}
// getRequestValidateMiddleware 请求验证的Middleware
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:40 2024/5/31
func (hc *HttpClient) getRequestValidateMiddleware() resty.RequestMiddleware {
return func(client *resty.Client, request *resty.Request) error {
return nil
}
}
// getResponseValidateMiddleware 获取相应数据验证的middleware
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:42 2024/5/31
func (hc *HttpClient) getResponseValidateMiddleware() resty.ResponseMiddleware {
return func(client *resty.Client, response *resty.Response) error {
return nil
}
}
// SetRestyClient 设置client
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:54 2024/5/31
func (hc *HttpClient) SetRestyClient(restyClient *resty.Client) {
hc.Client = restyClient
}
// GetRestyClient 获取 resty client
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:57 2024/5/31
func (hc *HttpClient) GetRestyClient() *resty.Client {
return hc.Client
}
// Request 发送请求
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:52 2024/5/31
func (hc *HttpClient) Request() *define.Response {
var (
cacheResult *define.Response
)
if cacheResult = hc.getCacheResult(); nil != cacheResult {
// 判断是否开启预热
inputCachePreHeatConfig := hc.cacheInstance.PreHeatConfig()
var cachePreHeatConfig define.CachePreHeatConfig
serialize.JSON.TransitionIgnoreError(inputCachePreHeatConfig, &cachePreHeatConfig)
if !cachePreHeatConfig.Enable || (cachePreHeatConfig.MinTTL <= 0 && cachePreHeatConfig.MinPercent <= 0 && !cachePreHeatConfig.Force) {
// 无预热配置或未启用预热或者未设置预热规则
return cacheResult
}
go func() {
// 判断是否触发预热
if cachePreHeatConfig.Force {
_ = hc.requestBackendApi()
return
}
// 将百分比的配置归一化成最小剩余时间的配置
if cachePreHeatConfig.MinPercent > 0 {
expectMinTTL := hc.cacheInstance.CacheTime() * cachePreHeatConfig.MinPercent / 100
if cachePreHeatConfig.MinTTL == 0 || cachePreHeatConfig.MinTTL > expectMinTTL {
cachePreHeatConfig.MinTTL = expectMinTTL
}
}
if cachePreHeatConfig.MinTTL <= 0 {
// 未配置最小剩余时间
return
}
ttl := hc.cacheInstance.TTL(cacheResult.CacheInfo.CacheKey)
if ttl < 0 {
// 不存在或者未设置有效期
return
}
if ttl <= cachePreHeatConfig.MinTTL {
// 配置了最小剩余时间,并且key剩余有效期小于最小剩余时间
_ = hc.requestBackendApi()
}
}()
}
return hc.requestBackendApi()
}
// requestBackendApi 请求后端接口
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:47 2024/10/9
func (hc *HttpClient) requestBackendApi() *define.Response {
hc.Client.OnBeforeRequest(hc.getRequestValidateMiddleware()) // 请求参数验证中间件必注册
hc.Client.OnAfterResponse(hc.getResponseValidateMiddleware()) // 响应验证中间件必注册
var (
err error
)
response := hc.newResponse()
for i := 0; i < hc.reqConfig.RetryRule.RetryCount+1; i++ {
response.Seq++
response.RequestCount++
if response.RestyResponse, err = hc.request.Send(); nil != err {
response.FailInfo = &define.ResponseFailInfo{
Type: define.RequestFailTypeSend,
Message: "response instance is nil",
}
for _, itemAfterResponse := range hc.requestFinishHandler {
itemAfterResponse(hc.reqConfig, response)
}
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
continue
}
if nil == response.RestyResponse {
response.FailInfo = &define.ResponseFailInfo{
Type: define.RequestFailTypeSend,
Message: "response instance is nil",
}
for _, itemAfterResponse := range hc.requestFinishHandler {
itemAfterResponse(hc.reqConfig, response)
}
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
continue
}
// 解析返回信息
hc.fillResponseHeader(response)
hc.fillResponseCookie(response)
hc.fillResponseBody(response)
if response.HttpCode != http.StatusOK {
response.FailInfo = &define.ResponseFailInfo{
Type: define.RequestFailTypeServerError,
Message: "http code is " + response.HttpCodeStatus + ", not success",
}
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
continue
}
if !hc.isCodeSuccess(response) {
response.FailInfo = &define.ResponseFailInfo{
Type: define.RequestFailTypeBusinessError,
Message: "business code is " + response.Code + ", not success",
}
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
continue
}
response.IsSuccess = true //设置成功
response.CacheInfo.SetCache, response.CacheInfo.CacheError = hc.setCacheResult(response) // 设置缓存
break
}
return response
}
// newResponse 默认返回数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:44 2024/6/1
func (hc *HttpClient) newResponse() *define.Response {
return &define.Response{
Header: map[string]string{},
Cookie: map[string]string{},
Body: map[string]any{},
Code: "",
Message: "",
Data: "",
HttpCode: 0,
HttpCodeStatus: "",
ResponseDataRule: nil,
Seq: 0,
RequestStartTime: 0,
RequestFinishTime: 0,
UsedTime: 0,
RestyResponse: nil,
IsSuccess: false,
CacheInfo: &define.ResponseCacheInfo{
IsCache: false,
SetCache: false,
CacheKey: "",
CacheValue: "",
CacheEnable: nil != hc.cacheInstance && hc.cacheInstance.Enable(),
CacheError: nil,
},
RequestCount: 0,
FailInfo: nil,
}
}
// fillResponseHeader 填充响应header
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:30 2024/6/5
func (hc *HttpClient) fillResponseHeader(response *define.Response) {
response.Header = map[string]string{} // 清空已有数据
response.HttpCode = response.RestyResponse.StatusCode() // http状态码
response.HttpCodeStatus = response.RestyResponse.Status() // http状态码描述
for headerName, headerValue := range response.RestyResponse.Header() {
if len(headerValue) > 0 {
response.Header[headerName] = headerValue[0]
} else {
response.Header[headerName] = ""
}
}
}
// fillResponseCookie 填充cookie
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:32 2024/6/5
func (hc *HttpClient) fillResponseCookie(response *define.Response) {
response.Cookie = map[string]string{} // 清空已有数据
for _, cookieValue := range response.RestyResponse.Cookies() {
response.Cookie[cookieValue.Name] = cookieValue.Value
}
}
// fillResponseBody 填充响应body
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:38 2024/6/5
func (hc *HttpClient) fillResponseBody(response *define.Response) {
response.Data = string(response.RestyResponse.Body())
response.Code = gjson.Get(response.Data, hc.reqConfig.CodeField).String()
response.Message = gjson.Get(response.Data, hc.reqConfig.MessageField).String()
businessData := gjson.Get(response.Data, hc.reqConfig.DataField)
if businessData.Value() == nil {
// data为空指针, 归一化成空对象
response.Body = map[string]any{}
} else {
if businessData.IsArray() {
// 数组类型的转换
response.Data = fmt.Sprintf(`{"list":` + businessData.String() + "}")
} else {
if businessData.IsObject() {
// 返回的就是对象
response.Data = businessData.String()
} else {
// 返回是普通类型
response.Data = serialize.JSON.MarshalForString(map[string]any{
"value": businessData.Value(),
})
}
}
_ = serialize.JSON.UnmarshalWithNumber([]byte(response.Data), &response.Body)
}
response.ExtendData = map[string]string{}
gjson.Parse(response.Data).ForEach(func(key, value gjson.Result) bool {
if key.String() == hc.reqConfig.CodeField ||
key.String() == hc.reqConfig.MessageField ||
key.String() == hc.reqConfig.DataField {
return true
}
response.ExtendData[key.String()] = value.String()
return true
})
}
// isHttpCodeSuccess ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:48 2024/6/6
func (hc *HttpClient) isCodeSuccess(response *define.Response) bool {
for _, itemSuccessCode := range hc.reqConfig.SuccessCodeList {
if itemSuccessCode == response.Code {
return true
}
}
return false
}
// getCacheResult 获取缓存结果
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:04 2024/6/3
func (hc *HttpClient) getCacheResult() *define.Response {
if nil == hc.cacheInstance || !hc.cacheInstance.Enable() {
return nil
}
startTime := time.Now().UnixMilli()
cacheKey := hc.cacheInstance.GetKey(hc.reqConfig)
cacheValue := strings.TrimSpace(hc.cacheInstance.GetValue(cacheKey))
if len(cacheValue) == 0 {
return nil
}
response := hc.newResponse()
if err := serialize.JSON.UnmarshalWithNumber([]byte(cacheValue), response); nil != err {
return nil
}
response.CacheInfo.IsCache = true // 设置缓存标记
response.RequestStartTime = startTime // 开始时间
response.RequestFinishTime = time.Now().UnixMilli() // 结束时间
response.UsedTime = response.RequestFinishTime - response.RequestStartTime // 耗时
response.CacheInfo.CacheKey = cacheKey // 缓存key
response.CacheInfo.CacheValue = cacheValue // 缓存值
return response
}
// setCacheResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:24 2024/6/3
func (hc *HttpClient) setCacheResult(response *define.Response) (bool, error) {
if nil == response || nil == hc.cacheInstance {
return false, nil
}
// 全局未开启或者当前请求不支持缓存
if !hc.cacheInstance.Enable() || !hc.cacheInstance.IsAllow(hc.reqConfig, response) {
return false, nil
}
cacheKey := hc.cacheInstance.GetKey(hc.reqConfig)
cacheValue := serialize.JSON.MarshalForString(response)
if err := hc.cacheInstance.SetValue(cacheKey, cacheValue); nil != err {
return false, err
}
response.CacheInfo.CacheKey = cacheKey
response.CacheInfo.CacheValue = cacheValue
return true, nil
}