|
|
|
@ -27,7 +27,7 @@ import (
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 15:27 2024/5/31
|
|
|
|
|
func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) {
|
|
|
|
|
func NewHttpClient(reqConfig *define.Request, reqOption *RequestOption) (*HttpClient, error) {
|
|
|
|
|
if nil == reqConfig.Logger {
|
|
|
|
|
reqConfig.Logger = log.Get() // 未单独指定日志实例, 则使用全局日志实例
|
|
|
|
|
}
|
|
|
|
@ -95,7 +95,8 @@ func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) {
|
|
|
|
|
hc := &HttpClient{
|
|
|
|
|
Client: restyClient,
|
|
|
|
|
request: restyRequest,
|
|
|
|
|
reqConfig: reqConfig,
|
|
|
|
|
reqOption: reqOption,
|
|
|
|
|
reqCfg: reqConfig,
|
|
|
|
|
http4xxHandler: make([]define.Http4xxHandler, 0),
|
|
|
|
|
http5xxHandler: make([]define.Http5xxHandler, 0),
|
|
|
|
|
httpBusinessErrorHandler: make([]define.HttpBusinessErrorHandler, 0),
|
|
|
|
@ -121,7 +122,8 @@ func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) {
|
|
|
|
|
type HttpClient struct {
|
|
|
|
|
*resty.Client
|
|
|
|
|
request *resty.Request
|
|
|
|
|
reqConfig *define.Request
|
|
|
|
|
reqOption *RequestOption
|
|
|
|
|
reqCfg *define.Request
|
|
|
|
|
http4xxHandler []define.Http4xxHandler
|
|
|
|
|
http5xxHandler []define.Http5xxHandler
|
|
|
|
|
httpBusinessErrorHandler []define.HttpBusinessErrorHandler
|
|
|
|
@ -221,14 +223,14 @@ func (hc *HttpClient) Request() *define.Response {
|
|
|
|
|
}
|
|
|
|
|
// 上面若命中缓存, 则后续缓存实例不可能为nil, 无需判断
|
|
|
|
|
// 判断是否开启预热
|
|
|
|
|
cachePreHeatConfig := hc.reqConfig.CacheInstance.PreHeatConfig()
|
|
|
|
|
cachePreHeatConfig := hc.reqOption.CacheInstance.PreHeatConfig()
|
|
|
|
|
if nil == cachePreHeatConfig {
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, PreHeatConfig未返回预热配置, 不做预热处理", map[string]any{}, hc.reqConfig)
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, PreHeatConfig未返回预热配置, 不做预热处理", map[string]any{}, hc.reqCfg)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, 进行预热策略处理", map[string]any{
|
|
|
|
|
"cache_info": cacheResult.CacheInfo,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
defer func() {
|
|
|
|
|
// 命中缓存的情况下, 检测缓存预热策略, 判断是否进行缓存预热
|
|
|
|
|
go func() {
|
|
|
|
@ -236,25 +238,25 @@ func (hc *HttpClient) Request() *define.Response {
|
|
|
|
|
// 无预热配置或未启用预热或者未设置预热规则
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, 未配置缓存预热策略", map[string]any{
|
|
|
|
|
"cache_pre_heat_config": cachePreHeatConfig,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 判断是否触发预热
|
|
|
|
|
if cachePreHeatConfig.Force {
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, 强制执行缓存预热, 忽略其他策略配置", map[string]any{
|
|
|
|
|
"cache_pre_heat_config": cachePreHeatConfig,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
_ = hc.requestBackendApi()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 将百分比的配置归一化成最小剩余时间的配置
|
|
|
|
|
if cachePreHeatConfig.MinPercent > 0 {
|
|
|
|
|
expectMinTTL := hc.reqConfig.CacheInstance.CacheTime() * cachePreHeatConfig.MinPercent / 100
|
|
|
|
|
expectMinTTL := hc.reqOption.CacheInstance.CacheTime() * cachePreHeatConfig.MinPercent / 100
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, 配置预热策略:有效时长剩余百分比", map[string]any{
|
|
|
|
|
"cache_pre_heat_config": cachePreHeatConfig,
|
|
|
|
|
"percent_min_ttl": expectMinTTL,
|
|
|
|
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
if cachePreHeatConfig.MinTTL == 0 || cachePreHeatConfig.MinTTL > expectMinTTL {
|
|
|
|
|
cachePreHeatConfig.MinTTL = expectMinTTL
|
|
|
|
|
}
|
|
|
|
@ -263,16 +265,16 @@ func (hc *HttpClient) Request() *define.Response {
|
|
|
|
|
// 未配置最小剩余时间
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, 未配置预热市场策略, 不执行预热", map[string]any{
|
|
|
|
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ttl := hc.reqConfig.CacheInstance.TTL(cacheResult.CacheInfo.CacheKey)
|
|
|
|
|
ttl := hc.reqOption.CacheInstance.TTL(cacheResult.CacheInfo.CacheKey)
|
|
|
|
|
if ttl < 0 {
|
|
|
|
|
// 不存在或者未设置有效期
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, 当前缓存结果不存在或未设置有效期, 不执行预热", map[string]any{
|
|
|
|
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
|
|
|
|
"note": "预热时间至少在缓存过期前10s触发预热, 以保证足够时间进行预热, 以及不会因为预热尚未完成, 但是大量流量涌入, 进而导致流量穿透",
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -280,47 +282,47 @@ func (hc *HttpClient) Request() *define.Response {
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, 缓存结果有效期剩余时长大于配置阈值, 不执行预热", map[string]any{
|
|
|
|
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
|
|
|
|
"remaining_ttl": ttl,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热", map[string]any{
|
|
|
|
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
|
|
|
|
"remaining_ttl": ttl,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
// 配置了最小剩余时间,并且key剩余有效期小于最小剩余时间
|
|
|
|
|
// 预热加锁, 并发请求触发预热, 仅触发一个即可, 使用接口做key
|
|
|
|
|
if err := hc.reqConfig.CacheInstance.Lock(hc.reqConfig.FullUrl); err != nil {
|
|
|
|
|
if err := hc.reqOption.CacheInstance.Lock(hc.reqCfg.FullUrl); err != nil {
|
|
|
|
|
log.RecordWarn("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热, 加锁失败, 未执行预热", map[string]any{
|
|
|
|
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
|
|
|
|
"remaining_ttl": ttl,
|
|
|
|
|
"err_msg": err.Error(),
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热, 加锁成功, 执行预热", map[string]any{
|
|
|
|
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
|
|
|
|
"remaining_ttl": ttl,
|
|
|
|
|
"lock_key": hc.reqConfig.FullUrl,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
"lock_key": hc.reqCfg.FullUrl,
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
_ = hc.requestBackendApi()
|
|
|
|
|
if err := hc.reqConfig.CacheInstance.Unlock(hc.reqConfig.FullUrl); nil != err {
|
|
|
|
|
if err := hc.reqOption.CacheInstance.Unlock(hc.reqCfg.FullUrl); nil != err {
|
|
|
|
|
log.RecordError("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热, 执行预热后, 释放锁失败", map[string]any{
|
|
|
|
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
|
|
|
|
"remaining_ttl": ttl,
|
|
|
|
|
"lock_key": hc.reqConfig.FullUrl,
|
|
|
|
|
"lock_key": hc.reqCfg.FullUrl,
|
|
|
|
|
"err_msg": err.Error(),
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.RecordDebug("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热, 执行预热后, 释放锁成功", map[string]any{
|
|
|
|
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
|
|
|
|
"remaining_ttl": ttl,
|
|
|
|
|
"lock_key": hc.reqConfig.FullUrl,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
"lock_key": hc.reqCfg.FullUrl,
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
}()
|
|
|
|
|
}()
|
|
|
|
|
// 命中缓存必然请求成功, 直接记录成功日志即可
|
|
|
|
|
log.Record(consts.LogLevelInfo, "接口请求成功:命中缓存", hc.reqConfig, cacheResult)
|
|
|
|
|
log.Record(consts.LogLevelInfo, "接口请求成功:命中缓存", hc.reqCfg, cacheResult)
|
|
|
|
|
return cacheResult
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -337,17 +339,17 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response := hc.newResponse()
|
|
|
|
|
for i := 0; i < hc.reqConfig.RetryRule.RetryCount+1; i++ {
|
|
|
|
|
for i := 0; i < hc.reqCfg.RetryRule.RetryCount+1; i++ {
|
|
|
|
|
if i > 0 {
|
|
|
|
|
// 非首次请求, 说明是重试, 暂停指定时间间隔
|
|
|
|
|
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
|
|
|
|
|
time.Sleep(time.Duration(hc.reqCfg.RetryRule.RetryTimeInterval) * time.Millisecond)
|
|
|
|
|
}
|
|
|
|
|
if response.Seq > 0 {
|
|
|
|
|
// 说明是重试, 记录上一次的请求信息
|
|
|
|
|
response.RequestFinishTime = time.Now().UnixMilli()
|
|
|
|
|
response.UsedTime = response.RequestFinishTime - response.RequestStartTime
|
|
|
|
|
for _, itemAfterResponse := range hc.requestFinishHandler {
|
|
|
|
|
itemAfterResponse(hc.reqConfig, response)
|
|
|
|
|
itemAfterResponse(hc.reqCfg, response)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
response.RequestStartTime = time.Now().UnixMilli() // 每次重置请求时间
|
|
|
|
@ -356,16 +358,16 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|
|
|
|
response.RequestCount++
|
|
|
|
|
retryHttpCodeTable := make(map[int64]bool)
|
|
|
|
|
retryBusinessCodeTable := make(map[string]bool)
|
|
|
|
|
if nil != hc.reqConfig.RetryRule {
|
|
|
|
|
for _, httpCode := range hc.reqConfig.RetryRule.RetryHttpCodeList {
|
|
|
|
|
if nil != hc.reqCfg.RetryRule {
|
|
|
|
|
for _, httpCode := range hc.reqCfg.RetryRule.RetryHttpCodeList {
|
|
|
|
|
retryHttpCodeTable[httpCode] = true
|
|
|
|
|
}
|
|
|
|
|
for _, businessCode := range hc.reqConfig.RetryRule.RetryBusinessCodeList {
|
|
|
|
|
for _, businessCode := range hc.reqCfg.RetryRule.RetryBusinessCodeList {
|
|
|
|
|
retryBusinessCodeTable[businessCode] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
successHttpCodeTable := make(map[int]bool)
|
|
|
|
|
for _, itemHttpCode := range hc.reqConfig.SuccessHttpCodeList {
|
|
|
|
|
for _, itemHttpCode := range hc.reqCfg.SuccessHttpCodeList {
|
|
|
|
|
successHttpCodeTable[itemHttpCode] = true
|
|
|
|
|
}
|
|
|
|
|
if response.RestyResponse, err = hc.request.Send(); nil != err {
|
|
|
|
@ -386,23 +388,23 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|
|
|
|
log.RecordDebug("请求发送出现异常", map[string]any{
|
|
|
|
|
"err_type": errType,
|
|
|
|
|
"err_msg": err.Error(),
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
if errType == define.RequestFailTypeSend {
|
|
|
|
|
// 命中限流就不重试了
|
|
|
|
|
log.RecordDebug("请求命中限流, 忽略重试策略, 不进行重试", nil, hc.reqConfig)
|
|
|
|
|
log.RecordDebug("请求命中限流, 忽略重试策略, 不进行重试", nil, hc.reqCfg)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if errType == define.RequestFailTypeTimeoutError && !retryHttpCodeTable[499] {
|
|
|
|
|
// 未配置超时重试
|
|
|
|
|
log.RecordDebug("请求超时, 未配置超时重试, 不进行重试", nil, hc.reqConfig)
|
|
|
|
|
log.RecordDebug("请求超时, 未配置超时重试, 不进行重试", nil, hc.reqCfg)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
log.RecordDebug("请求发送出现异常, 进行重试", map[string]any{
|
|
|
|
|
"err_type": errType,
|
|
|
|
|
"err_msg": err.Error(),
|
|
|
|
|
"time_interval": time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
"time_interval": time.Duration(hc.reqCfg.RetryRule.RetryTimeInterval) * time.Millisecond,
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -413,8 +415,8 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|
|
|
|
}
|
|
|
|
|
log.RecordDebug("RestyResponse为nil, 准备重试", map[string]any{
|
|
|
|
|
"err_type": response.FailInfo.Type,
|
|
|
|
|
"time_interval": time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
"time_interval": time.Duration(hc.reqCfg.RetryRule.RetryTimeInterval) * time.Millisecond,
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// 解析返回信息
|
|
|
|
@ -438,10 +440,10 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|
|
|
|
"err_type": errType,
|
|
|
|
|
"err_msg": response.RestyResponse.Status(),
|
|
|
|
|
"response_http_code": response.HttpCode,
|
|
|
|
|
"success_http_code": hc.reqConfig.SuccessHttpCodeList,
|
|
|
|
|
"success_http_code": hc.reqCfg.SuccessHttpCodeList,
|
|
|
|
|
"allow_retry": len(retryHttpCodeTable) == 0 || retryHttpCodeTable[int64(response.HttpCode)],
|
|
|
|
|
"time_interval": time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
"time_interval": time.Duration(hc.reqCfg.RetryRule.RetryTimeInterval) * time.Millisecond,
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
if len(retryHttpCodeTable) > 0 && !retryHttpCodeTable[int64(response.HttpCode)] {
|
|
|
|
|
// 未配置http code重试
|
|
|
|
|
break
|
|
|
|
@ -457,10 +459,10 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|
|
|
|
"err_type": response.FailInfo.Type,
|
|
|
|
|
"err_msg": response.Message,
|
|
|
|
|
"response_code": response.Code,
|
|
|
|
|
"success_code": hc.reqConfig.SuccessCodeList,
|
|
|
|
|
"success_code": hc.reqCfg.SuccessCodeList,
|
|
|
|
|
"allow_retry": len(retryBusinessCodeTable) == 0 || retryBusinessCodeTable[response.Code],
|
|
|
|
|
"time_interval": time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
"time_interval": time.Duration(hc.reqCfg.RetryRule.RetryTimeInterval) * time.Millisecond,
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
if len(retryBusinessCodeTable) > 0 && !retryBusinessCodeTable[response.Code] {
|
|
|
|
|
// 未配置业务code重试
|
|
|
|
|
break
|
|
|
|
@ -475,7 +477,7 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|
|
|
|
response.UsedTime = response.RequestFinishTime - response.RequestStartTime
|
|
|
|
|
// 请求完成hook
|
|
|
|
|
for _, itemAfterResponse := range hc.requestFinishHandler {
|
|
|
|
|
itemAfterResponse(hc.reqConfig, response)
|
|
|
|
|
itemAfterResponse(hc.reqCfg, response)
|
|
|
|
|
}
|
|
|
|
|
return response
|
|
|
|
|
}
|
|
|
|
@ -507,7 +509,7 @@ func (hc *HttpClient) newResponse() *define.Response {
|
|
|
|
|
SetCache: false,
|
|
|
|
|
CacheKey: "",
|
|
|
|
|
CacheValue: "",
|
|
|
|
|
CacheEnable: nil != hc.reqConfig.CacheInstance && hc.reqConfig.CacheInstance.Enable(),
|
|
|
|
|
CacheEnable: nil != hc.reqOption.CacheInstance && hc.reqOption.CacheInstance.Enable(),
|
|
|
|
|
CacheError: nil,
|
|
|
|
|
},
|
|
|
|
|
RequestCount: 0,
|
|
|
|
@ -552,9 +554,9 @@ func (hc *HttpClient) fillResponseCookie(response *define.Response) {
|
|
|
|
|
// 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)
|
|
|
|
|
response.Code = gjson.Get(response.Data, hc.reqCfg.CodeField).String()
|
|
|
|
|
response.Message = gjson.Get(response.Data, hc.reqCfg.MessageField).String()
|
|
|
|
|
businessData := gjson.Get(response.Data, hc.reqCfg.DataField)
|
|
|
|
|
if businessData.Value() == nil {
|
|
|
|
|
// data为空指针, 归一化成空对象
|
|
|
|
|
response.Body = map[string]any{}
|
|
|
|
@ -578,9 +580,9 @@ func (hc *HttpClient) fillResponseBody(response *define.Response) {
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
if key.String() == hc.reqCfg.CodeField ||
|
|
|
|
|
key.String() == hc.reqCfg.MessageField ||
|
|
|
|
|
key.String() == hc.reqCfg.DataField {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
response.ExtendData[key.String()] = value.String()
|
|
|
|
@ -594,7 +596,7 @@ func (hc *HttpClient) fillResponseBody(response *define.Response) {
|
|
|
|
|
//
|
|
|
|
|
// Date : 22:48 2024/6/6
|
|
|
|
|
func (hc *HttpClient) isCodeSuccess(response *define.Response) bool {
|
|
|
|
|
for _, itemSuccessCode := range hc.reqConfig.SuccessCodeList {
|
|
|
|
|
for _, itemSuccessCode := range hc.reqCfg.SuccessCodeList {
|
|
|
|
|
if itemSuccessCode == response.Code {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
@ -608,26 +610,26 @@ func (hc *HttpClient) isCodeSuccess(response *define.Response) bool {
|
|
|
|
|
//
|
|
|
|
|
// Date : 16:04 2024/6/3
|
|
|
|
|
func (hc *HttpClient) getCacheResult() *define.Response {
|
|
|
|
|
if nil == hc.reqConfig.CacheInstance {
|
|
|
|
|
log.RecordDebug("接口请求前缓存检测, 未设置缓存实例", map[string]any{}, hc.reqConfig)
|
|
|
|
|
if nil == hc.reqOption.CacheInstance {
|
|
|
|
|
log.RecordDebug("接口请求前缓存检测, 未设置缓存实例", map[string]any{}, hc.reqCfg)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if !hc.reqConfig.CacheInstance.Enable() {
|
|
|
|
|
log.RecordDebug("接口请求前缓存检测, 设置缓存实例, 但未启用缓存功能", map[string]any{}, hc.reqConfig)
|
|
|
|
|
if !hc.reqOption.CacheInstance.Enable() {
|
|
|
|
|
log.RecordDebug("接口请求前缓存检测, 设置缓存实例, 但未启用缓存功能", map[string]any{}, hc.reqCfg)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
startTime := time.Now().UnixMilli()
|
|
|
|
|
cacheKey := hc.reqConfig.CacheInstance.GetKey(hc.reqConfig)
|
|
|
|
|
cacheValue := strings.TrimSpace(hc.reqConfig.CacheInstance.GetValue(cacheKey))
|
|
|
|
|
cacheKey := hc.reqOption.CacheInstance.GetKey(hc.reqCfg)
|
|
|
|
|
cacheValue := strings.TrimSpace(hc.reqOption.CacheInstance.GetValue(cacheKey))
|
|
|
|
|
if len(cacheValue) == 0 {
|
|
|
|
|
log.RecordDebug("接口请求前缓存检测, 未读取到缓存数据", map[string]any{}, hc.reqConfig)
|
|
|
|
|
log.RecordDebug("接口请求前缓存检测, 未读取到缓存数据", map[string]any{}, hc.reqCfg)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
response := hc.newResponse()
|
|
|
|
|
if err := serialize.JSON.UnmarshalWithNumber([]byte(cacheValue), response); nil != err {
|
|
|
|
|
log.RecordWarn("接口请求前缓存检测, 读取到缓存数据, 数据解析失败, 将跳过缓存, 请求对应接口", map[string]any{
|
|
|
|
|
"err_msg": err.Error(),
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
response.CacheInfo.IsCache = true // 设置缓存标记
|
|
|
|
@ -636,7 +638,7 @@ func (hc *HttpClient) getCacheResult() *define.Response {
|
|
|
|
|
response.UsedTime = response.RequestFinishTime - response.RequestStartTime // 耗时
|
|
|
|
|
response.CacheInfo.CacheKey = cacheKey // 缓存key
|
|
|
|
|
response.CacheInfo.CacheValue = cacheValue // 缓存值
|
|
|
|
|
log.RecordDebug("接口请求前缓存检测, 命中缓存, 直接返回缓存数据", map[string]any{}, hc.reqConfig)
|
|
|
|
|
log.RecordDebug("接口请求前缓存检测, 命中缓存, 直接返回缓存数据", map[string]any{}, hc.reqCfg)
|
|
|
|
|
return response
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -646,37 +648,37 @@ func (hc *HttpClient) getCacheResult() *define.Response {
|
|
|
|
|
//
|
|
|
|
|
// Date : 16:24 2024/6/3
|
|
|
|
|
func (hc *HttpClient) setCacheResult(response *define.Response) (bool, error) {
|
|
|
|
|
if nil == response || nil == hc.reqConfig.CacheInstance {
|
|
|
|
|
if nil == response || nil == hc.reqOption.CacheInstance {
|
|
|
|
|
log.RecordDebug("接口请求成功后, 缓存设置失败", map[string]any{
|
|
|
|
|
"response_is_nil": response == nil,
|
|
|
|
|
"cache_instance_is_nil": hc.reqConfig.CacheInstance == nil,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
"cache_instance_is_nil": hc.reqOption.CacheInstance == nil,
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
// 全局未开启或者当前请求不支持缓存
|
|
|
|
|
globalCacheEnable := hc.reqConfig.CacheInstance.Enable()
|
|
|
|
|
currentRequestAllowCache := hc.reqConfig.CacheInstance.IsAllow(hc.reqConfig, response)
|
|
|
|
|
globalCacheEnable := hc.reqOption.CacheInstance.Enable()
|
|
|
|
|
currentRequestAllowCache := hc.reqOption.CacheInstance.IsAllow(hc.reqCfg, response)
|
|
|
|
|
log.RecordDebug("检测缓存是否允许执行", map[string]any{
|
|
|
|
|
"current_cache_enable": currentRequestAllowCache,
|
|
|
|
|
"global_cache_enable": globalCacheEnable,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
if !globalCacheEnable || !currentRequestAllowCache {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
cacheKey := hc.reqConfig.CacheInstance.GetKey(hc.reqConfig)
|
|
|
|
|
cacheKey := hc.reqOption.CacheInstance.GetKey(hc.reqCfg)
|
|
|
|
|
cacheValue := serialize.JSON.MarshalForStringIgnoreError(response)
|
|
|
|
|
if err := hc.reqConfig.CacheInstance.SetValue(cacheKey, cacheValue); nil != err {
|
|
|
|
|
if err := hc.reqOption.CacheInstance.SetValue(cacheKey, cacheValue); nil != err {
|
|
|
|
|
log.RecordWarn("开启结果缓存, 缓存设置失败", map[string]any{
|
|
|
|
|
"current_cache_enable": currentRequestAllowCache,
|
|
|
|
|
"global_cache_enable": globalCacheEnable,
|
|
|
|
|
"err_msg": err.Error(),
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
log.RecordDebug("开启结果缓存, 缓存设置成功", map[string]any{
|
|
|
|
|
"current_cache_enable": currentRequestAllowCache,
|
|
|
|
|
"global_cache_enable": globalCacheEnable,
|
|
|
|
|
}, hc.reqConfig)
|
|
|
|
|
}, hc.reqCfg)
|
|
|
|
|
response.CacheInfo.CacheKey = cacheKey
|
|
|
|
|
response.CacheInfo.CacheValue = cacheValue
|
|
|
|
|
return true, nil
|
|
|
|
|