From 905945843300436c8abe3021c52b2ddb50bd20e6 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Fri, 21 Mar 2025 16:18:58 +0800 Subject: [PATCH] fix Delay --- .../api/internal/service/apirequestService.go | 50 ++++--------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/app/user/cmd/api/internal/service/apirequestService.go b/app/user/cmd/api/internal/service/apirequestService.go index a417f76..abf2ff0 100644 --- a/app/user/cmd/api/internal/service/apirequestService.go +++ b/app/user/cmd/api/internal/service/apirequestService.go @@ -900,24 +900,8 @@ func (a *ApiRequestService) ProcessCAR059Request(ctx context.Context, params []b maxRetries, orderID.String()) } - // 计算基础延迟时间 - var delay time.Duration - if retryCount == 0 { - delay = 10 * time.Millisecond // 第一次设置极小延迟而非0 - } else { - // 指数退避策略 - baseDelay := 3 * time.Second - maxDelay := 1 * time.Hour - - delay = baseDelay - for i := 1; i < int(retryCount); i++ { - delay = delay * 2 - if delay > maxDelay { - delay = maxDelay - break - } - } - } + // 固定延迟时间为15秒 + fixedDelay := 15 * time.Second // 检查ctx是否已经有超时 deadline, hasDeadline := ctx.Deadline() @@ -929,32 +913,20 @@ func (a *ApiRequestService) ProcessCAR059Request(ctx context.Context, params []b 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 + // 如果剩余时间不足以等待完整的15秒 + if timeRemaining < fixedDelay+500*time.Millisecond { + logx.Infof("上下文剩余时间不足,提前返回,订单号: %s, 剩余时间: %v", + orderID.String(), timeRemaining) + return nil, fmt.Errorf("上下文剩余时间不足,无法完成下一次重试") } } - // 等待指定延迟时间 - logx.Infof("安排延迟重试,订单号: %s, 延迟: %v, 重试次数: %d", - orderID.String(), delay, retryCount+1) + // 等待固定的延迟时间 + logx.Infof("安排固定延迟重试,订单号: %s, 延迟: 15秒, 重试次数: %d", + orderID.String(), retryCount+1) select { - case <-time.After(delay): + case <-time.After(fixedDelay): // 延迟时间到,继续处理 case <-ctx.Done(): // 上下文被取消,返回错误