升级代码组织
This commit is contained in:
parent
94fdab4e36
commit
088670c7d4
@ -1,11 +1,11 @@
|
|||||||
// Package cache ...
|
// Package abstract ...
|
||||||
//
|
//
|
||||||
// Description : cache ...
|
// Description : cache ...
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 2024-06-03 15:49
|
// Date : 2024-06-03 15:49
|
||||||
package cache
|
package abstract
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
@ -27,7 +27,7 @@ import (
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 15:27 2024/5/31
|
// 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 {
|
if nil == reqConfig.Logger {
|
||||||
reqConfig.Logger = log.Get() // 未单独指定日志实例, 则使用全局日志实例
|
reqConfig.Logger = log.Get() // 未单独指定日志实例, 则使用全局日志实例
|
||||||
}
|
}
|
||||||
@ -95,7 +95,8 @@ func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) {
|
|||||||
hc := &HttpClient{
|
hc := &HttpClient{
|
||||||
Client: restyClient,
|
Client: restyClient,
|
||||||
request: restyRequest,
|
request: restyRequest,
|
||||||
reqConfig: reqConfig,
|
reqOption: reqOption,
|
||||||
|
reqCfg: reqConfig,
|
||||||
http4xxHandler: make([]define.Http4xxHandler, 0),
|
http4xxHandler: make([]define.Http4xxHandler, 0),
|
||||||
http5xxHandler: make([]define.Http5xxHandler, 0),
|
http5xxHandler: make([]define.Http5xxHandler, 0),
|
||||||
httpBusinessErrorHandler: make([]define.HttpBusinessErrorHandler, 0),
|
httpBusinessErrorHandler: make([]define.HttpBusinessErrorHandler, 0),
|
||||||
@ -121,7 +122,8 @@ func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) {
|
|||||||
type HttpClient struct {
|
type HttpClient struct {
|
||||||
*resty.Client
|
*resty.Client
|
||||||
request *resty.Request
|
request *resty.Request
|
||||||
reqConfig *define.Request
|
reqOption *RequestOption
|
||||||
|
reqCfg *define.Request
|
||||||
http4xxHandler []define.Http4xxHandler
|
http4xxHandler []define.Http4xxHandler
|
||||||
http5xxHandler []define.Http5xxHandler
|
http5xxHandler []define.Http5xxHandler
|
||||||
httpBusinessErrorHandler []define.HttpBusinessErrorHandler
|
httpBusinessErrorHandler []define.HttpBusinessErrorHandler
|
||||||
@ -221,14 +223,14 @@ func (hc *HttpClient) Request() *define.Response {
|
|||||||
}
|
}
|
||||||
// 上面若命中缓存, 则后续缓存实例不可能为nil, 无需判断
|
// 上面若命中缓存, 则后续缓存实例不可能为nil, 无需判断
|
||||||
// 判断是否开启预热
|
// 判断是否开启预热
|
||||||
cachePreHeatConfig := hc.reqConfig.CacheInstance.PreHeatConfig()
|
cachePreHeatConfig := hc.reqOption.CacheInstance.PreHeatConfig()
|
||||||
if nil == cachePreHeatConfig {
|
if nil == cachePreHeatConfig {
|
||||||
log.RecordDebug("接口请求命中缓存, PreHeatConfig未返回预热配置, 不做预热处理", map[string]any{}, hc.reqConfig)
|
log.RecordDebug("接口请求命中缓存, PreHeatConfig未返回预热配置, 不做预热处理", map[string]any{}, hc.reqCfg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
log.RecordDebug("接口请求命中缓存, 进行预热策略处理", map[string]any{
|
log.RecordDebug("接口请求命中缓存, 进行预热策略处理", map[string]any{
|
||||||
"cache_info": cacheResult.CacheInfo,
|
"cache_info": cacheResult.CacheInfo,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
defer func() {
|
defer func() {
|
||||||
// 命中缓存的情况下, 检测缓存预热策略, 判断是否进行缓存预热
|
// 命中缓存的情况下, 检测缓存预热策略, 判断是否进行缓存预热
|
||||||
go func() {
|
go func() {
|
||||||
@ -236,25 +238,25 @@ func (hc *HttpClient) Request() *define.Response {
|
|||||||
// 无预热配置或未启用预热或者未设置预热规则
|
// 无预热配置或未启用预热或者未设置预热规则
|
||||||
log.RecordDebug("接口请求命中缓存, 未配置缓存预热策略", map[string]any{
|
log.RecordDebug("接口请求命中缓存, 未配置缓存预热策略", map[string]any{
|
||||||
"cache_pre_heat_config": cachePreHeatConfig,
|
"cache_pre_heat_config": cachePreHeatConfig,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 判断是否触发预热
|
// 判断是否触发预热
|
||||||
if cachePreHeatConfig.Force {
|
if cachePreHeatConfig.Force {
|
||||||
log.RecordDebug("接口请求命中缓存, 强制执行缓存预热, 忽略其他策略配置", map[string]any{
|
log.RecordDebug("接口请求命中缓存, 强制执行缓存预热, 忽略其他策略配置", map[string]any{
|
||||||
"cache_pre_heat_config": cachePreHeatConfig,
|
"cache_pre_heat_config": cachePreHeatConfig,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
_ = hc.requestBackendApi()
|
_ = hc.requestBackendApi()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 将百分比的配置归一化成最小剩余时间的配置
|
// 将百分比的配置归一化成最小剩余时间的配置
|
||||||
if cachePreHeatConfig.MinPercent > 0 {
|
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{
|
log.RecordDebug("接口请求命中缓存, 配置预热策略:有效时长剩余百分比", map[string]any{
|
||||||
"cache_pre_heat_config": cachePreHeatConfig,
|
"cache_pre_heat_config": cachePreHeatConfig,
|
||||||
"percent_min_ttl": expectMinTTL,
|
"percent_min_ttl": expectMinTTL,
|
||||||
"min_ttl": cachePreHeatConfig.MinTTL,
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
if cachePreHeatConfig.MinTTL == 0 || cachePreHeatConfig.MinTTL > expectMinTTL {
|
if cachePreHeatConfig.MinTTL == 0 || cachePreHeatConfig.MinTTL > expectMinTTL {
|
||||||
cachePreHeatConfig.MinTTL = expectMinTTL
|
cachePreHeatConfig.MinTTL = expectMinTTL
|
||||||
}
|
}
|
||||||
@ -263,16 +265,16 @@ func (hc *HttpClient) Request() *define.Response {
|
|||||||
// 未配置最小剩余时间
|
// 未配置最小剩余时间
|
||||||
log.RecordDebug("接口请求命中缓存, 未配置预热市场策略, 不执行预热", map[string]any{
|
log.RecordDebug("接口请求命中缓存, 未配置预热市场策略, 不执行预热", map[string]any{
|
||||||
"min_ttl": cachePreHeatConfig.MinTTL,
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ttl := hc.reqConfig.CacheInstance.TTL(cacheResult.CacheInfo.CacheKey)
|
ttl := hc.reqOption.CacheInstance.TTL(cacheResult.CacheInfo.CacheKey)
|
||||||
if ttl < 0 {
|
if ttl < 0 {
|
||||||
// 不存在或者未设置有效期
|
// 不存在或者未设置有效期
|
||||||
log.RecordDebug("接口请求命中缓存, 当前缓存结果不存在或未设置有效期, 不执行预热", map[string]any{
|
log.RecordDebug("接口请求命中缓存, 当前缓存结果不存在或未设置有效期, 不执行预热", map[string]any{
|
||||||
"min_ttl": cachePreHeatConfig.MinTTL,
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
||||||
"note": "预热时间至少在缓存过期前10s触发预热, 以保证足够时间进行预热, 以及不会因为预热尚未完成, 但是大量流量涌入, 进而导致流量穿透",
|
"note": "预热时间至少在缓存过期前10s触发预热, 以保证足够时间进行预热, 以及不会因为预热尚未完成, 但是大量流量涌入, 进而导致流量穿透",
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,47 +282,47 @@ func (hc *HttpClient) Request() *define.Response {
|
|||||||
log.RecordDebug("接口请求命中缓存, 缓存结果有效期剩余时长大于配置阈值, 不执行预热", map[string]any{
|
log.RecordDebug("接口请求命中缓存, 缓存结果有效期剩余时长大于配置阈值, 不执行预热", map[string]any{
|
||||||
"min_ttl": cachePreHeatConfig.MinTTL,
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
||||||
"remaining_ttl": ttl,
|
"remaining_ttl": ttl,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.RecordDebug("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热", map[string]any{
|
log.RecordDebug("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热", map[string]any{
|
||||||
"min_ttl": cachePreHeatConfig.MinTTL,
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
||||||
"remaining_ttl": ttl,
|
"remaining_ttl": ttl,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
// 配置了最小剩余时间,并且key剩余有效期小于最小剩余时间
|
// 配置了最小剩余时间,并且key剩余有效期小于最小剩余时间
|
||||||
// 预热加锁, 并发请求触发预热, 仅触发一个即可, 使用接口做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{
|
log.RecordWarn("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热, 加锁失败, 未执行预热", map[string]any{
|
||||||
"min_ttl": cachePreHeatConfig.MinTTL,
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
||||||
"remaining_ttl": ttl,
|
"remaining_ttl": ttl,
|
||||||
"err_msg": err.Error(),
|
"err_msg": err.Error(),
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.RecordDebug("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热, 加锁成功, 执行预热", map[string]any{
|
log.RecordDebug("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热, 加锁成功, 执行预热", map[string]any{
|
||||||
"min_ttl": cachePreHeatConfig.MinTTL,
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
||||||
"remaining_ttl": ttl,
|
"remaining_ttl": ttl,
|
||||||
"lock_key": hc.reqConfig.FullUrl,
|
"lock_key": hc.reqCfg.FullUrl,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
_ = hc.requestBackendApi()
|
_ = 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{
|
log.RecordError("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热, 执行预热后, 释放锁失败", map[string]any{
|
||||||
"min_ttl": cachePreHeatConfig.MinTTL,
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
||||||
"remaining_ttl": ttl,
|
"remaining_ttl": ttl,
|
||||||
"lock_key": hc.reqConfig.FullUrl,
|
"lock_key": hc.reqCfg.FullUrl,
|
||||||
"err_msg": err.Error(),
|
"err_msg": err.Error(),
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.RecordDebug("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热, 执行预热后, 释放锁成功", map[string]any{
|
log.RecordDebug("接口请求命中缓存, 缓存结果有效期大于剩余时长小于配置阈值, 触发预热, 执行预热后, 释放锁成功", map[string]any{
|
||||||
"min_ttl": cachePreHeatConfig.MinTTL,
|
"min_ttl": cachePreHeatConfig.MinTTL,
|
||||||
"remaining_ttl": ttl,
|
"remaining_ttl": ttl,
|
||||||
"lock_key": hc.reqConfig.FullUrl,
|
"lock_key": hc.reqCfg.FullUrl,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
}()
|
}()
|
||||||
}()
|
}()
|
||||||
// 命中缓存必然请求成功, 直接记录成功日志即可
|
// 命中缓存必然请求成功, 直接记录成功日志即可
|
||||||
log.Record(consts.LogLevelInfo, "接口请求成功:命中缓存", hc.reqConfig, cacheResult)
|
log.Record(consts.LogLevelInfo, "接口请求成功:命中缓存", hc.reqCfg, cacheResult)
|
||||||
return cacheResult
|
return cacheResult
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -337,17 +339,17 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|||||||
)
|
)
|
||||||
|
|
||||||
response := hc.newResponse()
|
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 {
|
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 {
|
if response.Seq > 0 {
|
||||||
// 说明是重试, 记录上一次的请求信息
|
// 说明是重试, 记录上一次的请求信息
|
||||||
response.RequestFinishTime = time.Now().UnixMilli()
|
response.RequestFinishTime = time.Now().UnixMilli()
|
||||||
response.UsedTime = response.RequestFinishTime - response.RequestStartTime
|
response.UsedTime = response.RequestFinishTime - response.RequestStartTime
|
||||||
for _, itemAfterResponse := range hc.requestFinishHandler {
|
for _, itemAfterResponse := range hc.requestFinishHandler {
|
||||||
itemAfterResponse(hc.reqConfig, response)
|
itemAfterResponse(hc.reqCfg, response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response.RequestStartTime = time.Now().UnixMilli() // 每次重置请求时间
|
response.RequestStartTime = time.Now().UnixMilli() // 每次重置请求时间
|
||||||
@ -356,16 +358,16 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|||||||
response.RequestCount++
|
response.RequestCount++
|
||||||
retryHttpCodeTable := make(map[int64]bool)
|
retryHttpCodeTable := make(map[int64]bool)
|
||||||
retryBusinessCodeTable := make(map[string]bool)
|
retryBusinessCodeTable := make(map[string]bool)
|
||||||
if nil != hc.reqConfig.RetryRule {
|
if nil != hc.reqCfg.RetryRule {
|
||||||
for _, httpCode := range hc.reqConfig.RetryRule.RetryHttpCodeList {
|
for _, httpCode := range hc.reqCfg.RetryRule.RetryHttpCodeList {
|
||||||
retryHttpCodeTable[httpCode] = true
|
retryHttpCodeTable[httpCode] = true
|
||||||
}
|
}
|
||||||
for _, businessCode := range hc.reqConfig.RetryRule.RetryBusinessCodeList {
|
for _, businessCode := range hc.reqCfg.RetryRule.RetryBusinessCodeList {
|
||||||
retryBusinessCodeTable[businessCode] = true
|
retryBusinessCodeTable[businessCode] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
successHttpCodeTable := make(map[int]bool)
|
successHttpCodeTable := make(map[int]bool)
|
||||||
for _, itemHttpCode := range hc.reqConfig.SuccessHttpCodeList {
|
for _, itemHttpCode := range hc.reqCfg.SuccessHttpCodeList {
|
||||||
successHttpCodeTable[itemHttpCode] = true
|
successHttpCodeTable[itemHttpCode] = true
|
||||||
}
|
}
|
||||||
if response.RestyResponse, err = hc.request.Send(); nil != err {
|
if response.RestyResponse, err = hc.request.Send(); nil != err {
|
||||||
@ -386,23 +388,23 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|||||||
log.RecordDebug("请求发送出现异常", map[string]any{
|
log.RecordDebug("请求发送出现异常", map[string]any{
|
||||||
"err_type": errType,
|
"err_type": errType,
|
||||||
"err_msg": err.Error(),
|
"err_msg": err.Error(),
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
if errType == define.RequestFailTypeSend {
|
if errType == define.RequestFailTypeSend {
|
||||||
// 命中限流就不重试了
|
// 命中限流就不重试了
|
||||||
log.RecordDebug("请求命中限流, 忽略重试策略, 不进行重试", nil, hc.reqConfig)
|
log.RecordDebug("请求命中限流, 忽略重试策略, 不进行重试", nil, hc.reqCfg)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if errType == define.RequestFailTypeTimeoutError && !retryHttpCodeTable[499] {
|
if errType == define.RequestFailTypeTimeoutError && !retryHttpCodeTable[499] {
|
||||||
// 未配置超时重试
|
// 未配置超时重试
|
||||||
log.RecordDebug("请求超时, 未配置超时重试, 不进行重试", nil, hc.reqConfig)
|
log.RecordDebug("请求超时, 未配置超时重试, 不进行重试", nil, hc.reqCfg)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
log.RecordDebug("请求发送出现异常, 进行重试", map[string]any{
|
log.RecordDebug("请求发送出现异常, 进行重试", map[string]any{
|
||||||
"err_type": errType,
|
"err_type": errType,
|
||||||
"err_msg": err.Error(),
|
"err_msg": err.Error(),
|
||||||
"time_interval": time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond,
|
"time_interval": time.Duration(hc.reqCfg.RetryRule.RetryTimeInterval) * time.Millisecond,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -413,8 +415,8 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|||||||
}
|
}
|
||||||
log.RecordDebug("RestyResponse为nil, 准备重试", map[string]any{
|
log.RecordDebug("RestyResponse为nil, 准备重试", map[string]any{
|
||||||
"err_type": response.FailInfo.Type,
|
"err_type": response.FailInfo.Type,
|
||||||
"time_interval": time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond,
|
"time_interval": time.Duration(hc.reqCfg.RetryRule.RetryTimeInterval) * time.Millisecond,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// 解析返回信息
|
// 解析返回信息
|
||||||
@ -438,10 +440,10 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|||||||
"err_type": errType,
|
"err_type": errType,
|
||||||
"err_msg": response.RestyResponse.Status(),
|
"err_msg": response.RestyResponse.Status(),
|
||||||
"response_http_code": response.HttpCode,
|
"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)],
|
"allow_retry": len(retryHttpCodeTable) == 0 || retryHttpCodeTable[int64(response.HttpCode)],
|
||||||
"time_interval": time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond,
|
"time_interval": time.Duration(hc.reqCfg.RetryRule.RetryTimeInterval) * time.Millisecond,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
if len(retryHttpCodeTable) > 0 && !retryHttpCodeTable[int64(response.HttpCode)] {
|
if len(retryHttpCodeTable) > 0 && !retryHttpCodeTable[int64(response.HttpCode)] {
|
||||||
// 未配置http code重试
|
// 未配置http code重试
|
||||||
break
|
break
|
||||||
@ -457,10 +459,10 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|||||||
"err_type": response.FailInfo.Type,
|
"err_type": response.FailInfo.Type,
|
||||||
"err_msg": response.Message,
|
"err_msg": response.Message,
|
||||||
"response_code": response.Code,
|
"response_code": response.Code,
|
||||||
"success_code": hc.reqConfig.SuccessCodeList,
|
"success_code": hc.reqCfg.SuccessCodeList,
|
||||||
"allow_retry": len(retryBusinessCodeTable) == 0 || retryBusinessCodeTable[response.Code],
|
"allow_retry": len(retryBusinessCodeTable) == 0 || retryBusinessCodeTable[response.Code],
|
||||||
"time_interval": time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond,
|
"time_interval": time.Duration(hc.reqCfg.RetryRule.RetryTimeInterval) * time.Millisecond,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
if len(retryBusinessCodeTable) > 0 && !retryBusinessCodeTable[response.Code] {
|
if len(retryBusinessCodeTable) > 0 && !retryBusinessCodeTable[response.Code] {
|
||||||
// 未配置业务code重试
|
// 未配置业务code重试
|
||||||
break
|
break
|
||||||
@ -475,7 +477,7 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
|||||||
response.UsedTime = response.RequestFinishTime - response.RequestStartTime
|
response.UsedTime = response.RequestFinishTime - response.RequestStartTime
|
||||||
// 请求完成hook
|
// 请求完成hook
|
||||||
for _, itemAfterResponse := range hc.requestFinishHandler {
|
for _, itemAfterResponse := range hc.requestFinishHandler {
|
||||||
itemAfterResponse(hc.reqConfig, response)
|
itemAfterResponse(hc.reqCfg, response)
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
@ -507,7 +509,7 @@ func (hc *HttpClient) newResponse() *define.Response {
|
|||||||
SetCache: false,
|
SetCache: false,
|
||||||
CacheKey: "",
|
CacheKey: "",
|
||||||
CacheValue: "",
|
CacheValue: "",
|
||||||
CacheEnable: nil != hc.reqConfig.CacheInstance && hc.reqConfig.CacheInstance.Enable(),
|
CacheEnable: nil != hc.reqOption.CacheInstance && hc.reqOption.CacheInstance.Enable(),
|
||||||
CacheError: nil,
|
CacheError: nil,
|
||||||
},
|
},
|
||||||
RequestCount: 0,
|
RequestCount: 0,
|
||||||
@ -552,9 +554,9 @@ func (hc *HttpClient) fillResponseCookie(response *define.Response) {
|
|||||||
// Date : 21:38 2024/6/5
|
// Date : 21:38 2024/6/5
|
||||||
func (hc *HttpClient) fillResponseBody(response *define.Response) {
|
func (hc *HttpClient) fillResponseBody(response *define.Response) {
|
||||||
response.Data = string(response.RestyResponse.Body())
|
response.Data = string(response.RestyResponse.Body())
|
||||||
response.Code = gjson.Get(response.Data, hc.reqConfig.CodeField).String()
|
response.Code = gjson.Get(response.Data, hc.reqCfg.CodeField).String()
|
||||||
response.Message = gjson.Get(response.Data, hc.reqConfig.MessageField).String()
|
response.Message = gjson.Get(response.Data, hc.reqCfg.MessageField).String()
|
||||||
businessData := gjson.Get(response.Data, hc.reqConfig.DataField)
|
businessData := gjson.Get(response.Data, hc.reqCfg.DataField)
|
||||||
if businessData.Value() == nil {
|
if businessData.Value() == nil {
|
||||||
// data为空指针, 归一化成空对象
|
// data为空指针, 归一化成空对象
|
||||||
response.Body = map[string]any{}
|
response.Body = map[string]any{}
|
||||||
@ -578,9 +580,9 @@ func (hc *HttpClient) fillResponseBody(response *define.Response) {
|
|||||||
|
|
||||||
response.ExtendData = map[string]string{}
|
response.ExtendData = map[string]string{}
|
||||||
gjson.Parse(response.Data).ForEach(func(key, value gjson.Result) bool {
|
gjson.Parse(response.Data).ForEach(func(key, value gjson.Result) bool {
|
||||||
if key.String() == hc.reqConfig.CodeField ||
|
if key.String() == hc.reqCfg.CodeField ||
|
||||||
key.String() == hc.reqConfig.MessageField ||
|
key.String() == hc.reqCfg.MessageField ||
|
||||||
key.String() == hc.reqConfig.DataField {
|
key.String() == hc.reqCfg.DataField {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
response.ExtendData[key.String()] = value.String()
|
response.ExtendData[key.String()] = value.String()
|
||||||
@ -594,7 +596,7 @@ func (hc *HttpClient) fillResponseBody(response *define.Response) {
|
|||||||
//
|
//
|
||||||
// Date : 22:48 2024/6/6
|
// Date : 22:48 2024/6/6
|
||||||
func (hc *HttpClient) isCodeSuccess(response *define.Response) bool {
|
func (hc *HttpClient) isCodeSuccess(response *define.Response) bool {
|
||||||
for _, itemSuccessCode := range hc.reqConfig.SuccessCodeList {
|
for _, itemSuccessCode := range hc.reqCfg.SuccessCodeList {
|
||||||
if itemSuccessCode == response.Code {
|
if itemSuccessCode == response.Code {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -608,26 +610,26 @@ func (hc *HttpClient) isCodeSuccess(response *define.Response) bool {
|
|||||||
//
|
//
|
||||||
// Date : 16:04 2024/6/3
|
// Date : 16:04 2024/6/3
|
||||||
func (hc *HttpClient) getCacheResult() *define.Response {
|
func (hc *HttpClient) getCacheResult() *define.Response {
|
||||||
if nil == hc.reqConfig.CacheInstance {
|
if nil == hc.reqOption.CacheInstance {
|
||||||
log.RecordDebug("接口请求前缓存检测, 未设置缓存实例", map[string]any{}, hc.reqConfig)
|
log.RecordDebug("接口请求前缓存检测, 未设置缓存实例", map[string]any{}, hc.reqCfg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if !hc.reqConfig.CacheInstance.Enable() {
|
if !hc.reqOption.CacheInstance.Enable() {
|
||||||
log.RecordDebug("接口请求前缓存检测, 设置缓存实例, 但未启用缓存功能", map[string]any{}, hc.reqConfig)
|
log.RecordDebug("接口请求前缓存检测, 设置缓存实例, 但未启用缓存功能", map[string]any{}, hc.reqCfg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
startTime := time.Now().UnixMilli()
|
startTime := time.Now().UnixMilli()
|
||||||
cacheKey := hc.reqConfig.CacheInstance.GetKey(hc.reqConfig)
|
cacheKey := hc.reqOption.CacheInstance.GetKey(hc.reqCfg)
|
||||||
cacheValue := strings.TrimSpace(hc.reqConfig.CacheInstance.GetValue(cacheKey))
|
cacheValue := strings.TrimSpace(hc.reqOption.CacheInstance.GetValue(cacheKey))
|
||||||
if len(cacheValue) == 0 {
|
if len(cacheValue) == 0 {
|
||||||
log.RecordDebug("接口请求前缓存检测, 未读取到缓存数据", map[string]any{}, hc.reqConfig)
|
log.RecordDebug("接口请求前缓存检测, 未读取到缓存数据", map[string]any{}, hc.reqCfg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
response := hc.newResponse()
|
response := hc.newResponse()
|
||||||
if err := serialize.JSON.UnmarshalWithNumber([]byte(cacheValue), response); nil != err {
|
if err := serialize.JSON.UnmarshalWithNumber([]byte(cacheValue), response); nil != err {
|
||||||
log.RecordWarn("接口请求前缓存检测, 读取到缓存数据, 数据解析失败, 将跳过缓存, 请求对应接口", map[string]any{
|
log.RecordWarn("接口请求前缓存检测, 读取到缓存数据, 数据解析失败, 将跳过缓存, 请求对应接口", map[string]any{
|
||||||
"err_msg": err.Error(),
|
"err_msg": err.Error(),
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
response.CacheInfo.IsCache = true // 设置缓存标记
|
response.CacheInfo.IsCache = true // 设置缓存标记
|
||||||
@ -636,7 +638,7 @@ func (hc *HttpClient) getCacheResult() *define.Response {
|
|||||||
response.UsedTime = response.RequestFinishTime - response.RequestStartTime // 耗时
|
response.UsedTime = response.RequestFinishTime - response.RequestStartTime // 耗时
|
||||||
response.CacheInfo.CacheKey = cacheKey // 缓存key
|
response.CacheInfo.CacheKey = cacheKey // 缓存key
|
||||||
response.CacheInfo.CacheValue = cacheValue // 缓存值
|
response.CacheInfo.CacheValue = cacheValue // 缓存值
|
||||||
log.RecordDebug("接口请求前缓存检测, 命中缓存, 直接返回缓存数据", map[string]any{}, hc.reqConfig)
|
log.RecordDebug("接口请求前缓存检测, 命中缓存, 直接返回缓存数据", map[string]any{}, hc.reqCfg)
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -646,37 +648,37 @@ func (hc *HttpClient) getCacheResult() *define.Response {
|
|||||||
//
|
//
|
||||||
// Date : 16:24 2024/6/3
|
// Date : 16:24 2024/6/3
|
||||||
func (hc *HttpClient) setCacheResult(response *define.Response) (bool, error) {
|
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{
|
log.RecordDebug("接口请求成功后, 缓存设置失败", map[string]any{
|
||||||
"response_is_nil": response == nil,
|
"response_is_nil": response == nil,
|
||||||
"cache_instance_is_nil": hc.reqConfig.CacheInstance == nil,
|
"cache_instance_is_nil": hc.reqOption.CacheInstance == nil,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
// 全局未开启或者当前请求不支持缓存
|
// 全局未开启或者当前请求不支持缓存
|
||||||
globalCacheEnable := hc.reqConfig.CacheInstance.Enable()
|
globalCacheEnable := hc.reqOption.CacheInstance.Enable()
|
||||||
currentRequestAllowCache := hc.reqConfig.CacheInstance.IsAllow(hc.reqConfig, response)
|
currentRequestAllowCache := hc.reqOption.CacheInstance.IsAllow(hc.reqCfg, response)
|
||||||
log.RecordDebug("检测缓存是否允许执行", map[string]any{
|
log.RecordDebug("检测缓存是否允许执行", map[string]any{
|
||||||
"current_cache_enable": currentRequestAllowCache,
|
"current_cache_enable": currentRequestAllowCache,
|
||||||
"global_cache_enable": globalCacheEnable,
|
"global_cache_enable": globalCacheEnable,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
if !globalCacheEnable || !currentRequestAllowCache {
|
if !globalCacheEnable || !currentRequestAllowCache {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
cacheKey := hc.reqConfig.CacheInstance.GetKey(hc.reqConfig)
|
cacheKey := hc.reqOption.CacheInstance.GetKey(hc.reqCfg)
|
||||||
cacheValue := serialize.JSON.MarshalForStringIgnoreError(response)
|
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{
|
log.RecordWarn("开启结果缓存, 缓存设置失败", map[string]any{
|
||||||
"current_cache_enable": currentRequestAllowCache,
|
"current_cache_enable": currentRequestAllowCache,
|
||||||
"global_cache_enable": globalCacheEnable,
|
"global_cache_enable": globalCacheEnable,
|
||||||
"err_msg": err.Error(),
|
"err_msg": err.Error(),
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
log.RecordDebug("开启结果缓存, 缓存设置成功", map[string]any{
|
log.RecordDebug("开启结果缓存, 缓存设置成功", map[string]any{
|
||||||
"current_cache_enable": currentRequestAllowCache,
|
"current_cache_enable": currentRequestAllowCache,
|
||||||
"global_cache_enable": globalCacheEnable,
|
"global_cache_enable": globalCacheEnable,
|
||||||
}, hc.reqConfig)
|
}, hc.reqCfg)
|
||||||
response.CacheInfo.CacheKey = cacheKey
|
response.CacheInfo.CacheKey = cacheKey
|
||||||
response.CacheInfo.CacheValue = cacheValue
|
response.CacheInfo.CacheValue = cacheValue
|
||||||
return true, nil
|
return true, nil
|
||||||
|
@ -9,7 +9,6 @@ package define
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/cache"
|
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
@ -41,7 +40,6 @@ type Request struct {
|
|||||||
RetryRule *RequestRetryRule `json:"retry_rule"` // 重试规则
|
RetryRule *RequestRetryRule `json:"retry_rule"` // 重试规则
|
||||||
Logger *zap.Logger `json:"-"` // 日志记录器
|
Logger *zap.Logger `json:"-"` // 日志记录器
|
||||||
RateLimiter resty.RateLimiter `json:"-"` // 流控实例
|
RateLimiter resty.RateLimiter `json:"-"` // 流控实例
|
||||||
CacheInstance cache.ICache `json:"-"` // 数据结果缓存实例
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequestRetryRule 重试规则
|
// RequestRetryRule 重试规则
|
||||||
|
@ -10,7 +10,7 @@ package mesh
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"git.zhangdeman.cn/zhangdeman/consts"
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/cache"
|
"git.zhangdeman.cn/zhangdeman/network/httpclient/abstract"
|
||||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -38,7 +38,7 @@ type RequestConfigGroupItem struct {
|
|||||||
RequestCfg *define.Request `json:"request_cfg"` // 请求配置
|
RequestCfg *define.Request `json:"request_cfg"` // 请求配置
|
||||||
FailBehavior *RequestConfigGroupItemFailBehavior `json:"fail_behavior"` // 失败的行为, 不配置, 默认失败break
|
FailBehavior *RequestConfigGroupItemFailBehavior `json:"fail_behavior"` // 失败的行为, 不配置, 默认失败break
|
||||||
FinalFailureAllow bool `json:"final_failure_allow"` // 已经确定当前请求是最终失败了,当前请求是否允许执行
|
FinalFailureAllow bool `json:"final_failure_allow"` // 已经确定当前请求是最终失败了,当前请求是否允许执行
|
||||||
CacheInstance cache.ICache `json:"-"` // 数据缓存实例
|
CacheInstance abstract.ICache `json:"-"` // 数据缓存实例
|
||||||
Condition any `json:"condition"` // TODO: 请求条件, 特定条件下不执行当前请求
|
Condition any `json:"condition"` // TODO: 请求条件, 特定条件下不执行当前请求
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,7 +147,9 @@ func (c *client) doRequest(apiList []*RequestConfigGroupItem) bool {
|
|||||||
apiCfg.RequestCfg.Header = param[strings.ToLower(consts.RequestDataLocationHeader.String())] // header
|
apiCfg.RequestCfg.Header = param[strings.ToLower(consts.RequestDataLocationHeader.String())] // header
|
||||||
apiCfg.RequestCfg.Cookie = param[strings.ToLower(consts.RequestDataLocationCookie.String())] // cookie
|
apiCfg.RequestCfg.Cookie = param[strings.ToLower(consts.RequestDataLocationCookie.String())] // cookie
|
||||||
apiCfg.RequestCfg.Query = param[strings.ToLower(consts.RequestDataLocationQuery.String())] // query
|
apiCfg.RequestCfg.Query = param[strings.ToLower(consts.RequestDataLocationQuery.String())] // query
|
||||||
if httpClient, err = httpclient.NewHttpClient(apiCfg.RequestCfg, apiCfg.CacheInstance); nil != err {
|
if httpClient, err = httpclient.NewHttpClient(apiCfg.RequestCfg, &httpclient.RequestOption{
|
||||||
|
CacheInstance: apiCfg.CacheInstance,
|
||||||
|
}); nil != err {
|
||||||
// 此处获取客户端实例即发生异常, 忽略一切配置, 直接作为全局失败, 后续也不请求了
|
// 此处获取客户端实例即发生异常, 忽略一切配置, 直接作为全局失败, 后续也不请求了
|
||||||
c.resp.ErrorCode = "-500"
|
c.resp.ErrorCode = "-500"
|
||||||
c.resp.ErrorMessage = err.Error()
|
c.resp.ErrorMessage = err.Error()
|
||||||
|
17
httpclient/option.go
Normal file
17
httpclient/option.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Package httpclient ...
|
||||||
|
//
|
||||||
|
// Description : httpclient ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2025-05-07 12:19
|
||||||
|
package httpclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.zhangdeman.cn/zhangdeman/network/httpclient/abstract"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RequestOption 请求一些选项
|
||||||
|
type RequestOption struct {
|
||||||
|
CacheInstance abstract.ICache `json:"-"` // 数据结果缓存实例
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user