fix(delivery): add transient retry, fix queue deadlock, and gate delivery submit by scan auth
This commit is contained in:
@@ -215,12 +215,45 @@ func (p *DLCProvider) Submit(ctx context.Context, order *model.FulfillmentOrder,
|
||||
}
|
||||
|
||||
if err := p.patchOrderQueue(queueOrderID, gameAccount, bindUUID, goodsNo, goodsName); err != nil {
|
||||
return &ProviderSubmitResult{PatchResultData: partial}, fmt.Errorf("补充发货账号失败: %w", err)
|
||||
if isQueueDeadError(err) {
|
||||
// 旧队列已失效/不可操作,清除失效队列并重新创建
|
||||
delete(partial, "queue_order_id")
|
||||
newQueueID, createErr := p.createOrderQueue(goodID, order.OrderNo, goodsNo)
|
||||
if createErr != nil {
|
||||
return &ProviderSubmitResult{PatchResultData: partial}, fmt.Errorf("重置发货队列失败: %w (原错误: %v)", createErr, err)
|
||||
}
|
||||
queueOrderID = newQueueID
|
||||
partial["queue_order_id"] = queueOrderID
|
||||
if patchErr := p.patchOrderQueue(queueOrderID, gameAccount, bindUUID, goodsNo, goodsName); patchErr != nil {
|
||||
return &ProviderSubmitResult{PatchResultData: partial}, fmt.Errorf("补充发货账号失败: %w", patchErr)
|
||||
}
|
||||
} else {
|
||||
return &ProviderSubmitResult{PatchResultData: partial}, fmt.Errorf("补充发货账号失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
upstreamOrder, err := p.createUpstreamOrder(queueOrderID, gameAccount, goodID, order.OrderNo)
|
||||
if err != nil {
|
||||
return &ProviderSubmitResult{PatchResultData: partial}, fmt.Errorf("创建上游正式订单失败: %w", err)
|
||||
if isQueueDeadError(err) {
|
||||
// 创建正式单阶段出现订单不可操作,尝试重建队列重试一次
|
||||
delete(partial, "queue_order_id")
|
||||
newQueueID, createErr := p.createOrderQueue(goodID, order.OrderNo, goodsNo)
|
||||
if createErr == nil {
|
||||
queueOrderID = newQueueID
|
||||
partial["queue_order_id"] = queueOrderID
|
||||
if patchErr := p.patchOrderQueue(queueOrderID, gameAccount, bindUUID, goodsNo, goodsName); patchErr == nil {
|
||||
if retriedOrder, retriedErr := p.createUpstreamOrder(queueOrderID, gameAccount, goodID, order.OrderNo); retriedErr == nil {
|
||||
upstreamOrder = retriedOrder
|
||||
err = nil
|
||||
} else {
|
||||
err = retriedErr
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return &ProviderSubmitResult{PatchResultData: partial}, fmt.Errorf("创建上游正式订单失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
providerOrderNo := firstNonEmpty(
|
||||
@@ -366,6 +399,34 @@ func (p *DLCProvider) SyncStatus(ctx context.Context, order *model.FulfillmentOr
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func isQueueDeadError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := err.Error()
|
||||
return strings.Contains(msg, "订单不可操作") ||
|
||||
strings.Contains(msg, "不可操作") ||
|
||||
strings.Contains(msg, "订单不存在") ||
|
||||
strings.Contains(msg, "队列已失效")
|
||||
}
|
||||
|
||||
func isTransientDeliveryError(err error, statusCode int, message string) bool {
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
if statusCode == http.StatusTooManyRequests || statusCode == http.StatusBadGateway ||
|
||||
statusCode == http.StatusServiceUnavailable || statusCode == http.StatusGatewayTimeout {
|
||||
return true
|
||||
}
|
||||
msg := strings.ToLower(message)
|
||||
return strings.Contains(msg, "请求过于频繁") ||
|
||||
strings.Contains(msg, "稍后再试") ||
|
||||
strings.Contains(msg, "系统繁忙") ||
|
||||
strings.Contains(msg, "网络繁忙") ||
|
||||
strings.Contains(msg, "too many requests") ||
|
||||
strings.Contains(msg, "rate limit")
|
||||
}
|
||||
|
||||
func (p *DLCProvider) signProxy(path, method string, data interface{}, out interface{}) error {
|
||||
payload, err := json.Marshal(map[string]interface{}{
|
||||
"path": path,
|
||||
@@ -375,38 +436,69 @@ func (p *DLCProvider) signProxy(path, method string, data interface{}, out inter
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodPost, p.bffBaseURL+"/sign-proxy", bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := p.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("发货服务请求失败:%w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
var envelope struct {
|
||||
Code interface{} `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &envelope); err != nil {
|
||||
return fmt.Errorf("发货服务响应无法解析:%s", truncateDeliveryText(string(body)))
|
||||
}
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices || !isZeroCode(envelope.Code) {
|
||||
if envelope.Message != "" {
|
||||
return errors.New(envelope.Message)
|
||||
|
||||
const maxRetries = 2
|
||||
var lastErr error
|
||||
|
||||
for attempt := 0; attempt <= maxRetries; attempt++ {
|
||||
if attempt > 0 {
|
||||
// 遭遇频控或瞬时网络波动时的指数避让 (1.2s, 2.4s)
|
||||
backoff := time.Duration(1200*attempt)*time.Millisecond + time.Duration((attempt*137)%300)*time.Millisecond
|
||||
time.Sleep(backoff)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, p.bffBaseURL+"/sign-proxy", bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := p.httpClient.Do(req)
|
||||
if err != nil {
|
||||
lastErr = fmt.Errorf("发货服务请求失败:%w", err)
|
||||
if attempt < maxRetries && isTransientDeliveryError(err, 0, "") {
|
||||
continue
|
||||
}
|
||||
return lastErr
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
resp.Body.Close()
|
||||
|
||||
var envelope struct {
|
||||
Code interface{} `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &envelope); err != nil {
|
||||
lastErr = fmt.Errorf("发货服务响应无法解析:%s", truncateDeliveryText(string(body)))
|
||||
if attempt < maxRetries && resp.StatusCode >= http.StatusInternalServerError {
|
||||
continue
|
||||
}
|
||||
return lastErr
|
||||
}
|
||||
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices || !isZeroCode(envelope.Code) {
|
||||
errMsg := envelope.Message
|
||||
if errMsg == "" {
|
||||
errMsg = fmt.Sprintf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
lastErr = errors.New(errMsg)
|
||||
if attempt < maxRetries && isTransientDeliveryError(nil, resp.StatusCode, errMsg) {
|
||||
continue
|
||||
}
|
||||
return lastErr
|
||||
}
|
||||
|
||||
if out == nil || len(envelope.Data) == 0 || string(envelope.Data) == "null" {
|
||||
return nil
|
||||
}
|
||||
if err := json.Unmarshal(envelope.Data, out); err != nil {
|
||||
return fmt.Errorf("发货服务数据无法解析:%w", err)
|
||||
}
|
||||
return fmt.Errorf("发货服务请求失败:HTTP %d", resp.StatusCode)
|
||||
}
|
||||
if out == nil || len(envelope.Data) == 0 || string(envelope.Data) == "null" {
|
||||
return nil
|
||||
}
|
||||
if err := json.Unmarshal(envelope.Data, out); err != nil {
|
||||
return fmt.Errorf("发货服务数据无法解析:%w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
return lastErr
|
||||
}
|
||||
|
||||
func timeSecond(s int) time.Duration {
|
||||
|
||||
Reference in New Issue
Block a user