fix delay

This commit is contained in:
liangzai 2025-03-21 16:12:47 +08:00
parent 8a7a7aaba7
commit 29f248f1b4
2 changed files with 27 additions and 8 deletions

View File

@ -46,7 +46,7 @@ func (l *QuerySingleTestLogic) QuerySingleTest(req *types.QuerySingleTestReq) (r
// 使用新的超时上下文
apiResp, err := l.svcCtx.ApiRequestService.PreprocessRequestApi(timeoutCtx, marshalParams, req.Api)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "单查测试, 获取接口失败 : %d", err)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "单查测试, 接口请求失败 : %d", err)
}
var respData interface{}
err = json.Unmarshal(apiResp.Data, &respData)

View File

@ -896,14 +896,14 @@ func (a *ApiRequestService) ProcessCAR059Request(ctx context.Context, params []b
// 检查重试次数
maxRetries := 10
if retryCount >= maxRetries {
fmt.Errorf("车辆维保记录查询任务已达最大重试次数 %d停止重试订单号: %s",
return nil, fmt.Errorf("车辆维保记录查询任务已达最大重试次数 %d停止重试订单号: %s",
maxRetries, orderID.String())
}
// 计算延迟时间
// 计算基础延迟时间
var delay time.Duration
if retryCount == 0 {
delay = 0 // 第一次无延迟
delay = 10 * time.Millisecond // 第一次设置极小延迟而非0
} else {
// 指数退避策略
baseDelay := 3 * time.Second
@ -923,10 +923,29 @@ func (a *ApiRequestService) ProcessCAR059Request(ctx context.Context, params []b
deadline, hasDeadline := ctx.Deadline()
if hasDeadline {
timeRemaining := time.Until(deadline)
if timeRemaining <= delay {
// 如果剩余时间不足使用剩余时间的90%作为超时
delay = time.Duration(float64(timeRemaining) * 0.9)
logx.Infof("调整延迟时间以适应上下文超时,新延迟: %v", delay)
// 检查上下文是否已经超时或即将超时
if timeRemaining <= 0 {
return nil, fmt.Errorf("上下文已超时,停止重试,订单号: %s", orderID.String())
}
// 保留一些安全边界,确保有足够时间执行后续操作
safetyBuffer := 500 * time.Millisecond
// 如果剩余时间不足以完成当前延迟加安全边界
if timeRemaining < delay+safetyBuffer {
// 使用剩余时间的50%作为延迟,确保有足够时间完成后续操作
adjustedDelay := time.Duration(float64(timeRemaining) * 0.5)
// 确保最小延迟不小于10毫秒
if adjustedDelay < 10*time.Millisecond {
adjustedDelay = 10 * time.Millisecond
}
logx.Infof("调整延迟时间以适应上下文超时,原始延迟: %v, 新延迟: %v, 剩余时间: %v",
delay, adjustedDelay, timeRemaining)
delay = adjustedDelay
}
}