fix(delivery): 增加绑定缓存防频控、发货队列防重试死锁、终止无效回调重试

This commit is contained in:
yml2213
2026-09-18 15:26:09 +08:00
parent ec76844da8
commit 40194fa7e5
5 changed files with 135 additions and 18 deletions
+90 -15
View File
@@ -9,16 +9,25 @@ import (
"io"
"net/http"
"strings"
"sync"
"time"
"affiliate_dash/internal/model"
)
type bindCacheEntry struct {
account map[string]interface{}
err error
expiresAt time.Time
}
// DLCProvider 实现第一平台(聚鑫源 / dlc)履约逻辑
type DLCProvider struct {
bffBaseURL string
channel string
httpClient *http.Client
bffBaseURL string
channel string
httpClient *http.Client
bindCacheMu sync.RWMutex
bindCache map[string]bindCacheEntry
}
func NewDLCProvider(bffBaseURL, channel string, httpClient *http.Client) *DLCProvider {
@@ -37,6 +46,7 @@ func NewDLCProvider(bffBaseURL, channel string, httpClient *http.Client) *DLCPro
bffBaseURL: bffBaseURL,
channel: channel,
httpClient: httpClient,
bindCache: make(map[string]bindCacheEntry),
}
}
@@ -147,6 +157,14 @@ func (p *DLCProvider) CheckBind(ctx context.Context, order *model.FulfillmentOrd
Provider: ProviderDLC,
}, nil
}
if isRateLimitError(err) {
return &ProviderBindResult{
BindUUID: bindUUID,
Bound: false,
Message: "正在同步绑定状态,请稍候...",
Provider: ProviderDLC,
}, nil
}
return nil, err
}
@@ -312,25 +330,54 @@ func (p *DLCProvider) goodsDetail(goodID string) (map[string]interface{}, error)
}
func (p *DLCProvider) accountBound(bindUUID, goodID string) (map[string]interface{}, error) {
cacheKey := bindUUID + ":" + goodID
p.bindCacheMu.RLock()
if entry, ok := p.bindCache[cacheKey]; ok && time.Now().Before(entry.expiresAt) {
p.bindCacheMu.RUnlock()
return entry.account, entry.err
}
p.bindCacheMu.RUnlock()
var out struct {
GameAccount map[string]interface{} `json:"gameAccount"`
Snake map[string]interface{} `json:"game_account"`
}
if err := p.signProxy("/public/games/account-bound", "POST", map[string]interface{}{
err := p.signProxy("/public/games/account-bound", "POST", map[string]interface{}{
"bind_uuid": bindUUID,
"bindUuid": bindUUID,
"goodId": goodID,
"good_id": goodID,
}, &out); err != nil {
return nil, err
}, &out)
var account map[string]interface{}
ttl := 2 * time.Second
if err != nil {
if isRateLimitError(err) {
ttl = 3 * time.Second
}
} else {
account = out.GameAccount
if account == nil {
account = out.Snake
}
if account == nil {
err = errors.New("账号尚未绑定,请扫码完成绑定后再提交")
ttl = 2 * time.Second
} else {
ttl = 15 * time.Second
}
}
if out.GameAccount == nil {
out.GameAccount = out.Snake
p.bindCacheMu.Lock()
p.bindCache[cacheKey] = bindCacheEntry{
account: account,
err: err,
expiresAt: time.Now().Add(ttl),
}
if out.GameAccount == nil {
return nil, errors.New("账号尚未绑定,请扫码完成绑定后再提交")
}
return out.GameAccount, nil
p.bindCacheMu.Unlock()
return account, err
}
func (p *DLCProvider) createOrderQueue(goodID, orderNo, goodsNo string) (string, error) {
@@ -346,7 +393,19 @@ func (p *DLCProvider) createOrderQueue(goodID, orderNo, goodsNo string) (string,
if goodsNo != "" {
payload["goods_no"] = goodsNo
}
if err := p.signProxy("/public/users/orders-queue", "POST", payload, &out); err != nil {
err := p.signProxy("/public/users/orders-queue", "POST", payload, &out)
if err != nil {
if strings.Contains(err.Error(), "正在处理中") || strings.Contains(err.Error(), "请勿重复提交") {
// DLC 已存在此 order_sn 的队列,追加随机后缀重新创建队列
retrySN := fmt.Sprintf("%s_%x", orderNo, time.Now().UnixNano()%0xffff)
payload["order_sn"] = retrySN
out.Orders = nil
if retryErr := p.signProxy("/public/users/orders-queue", "POST", payload, &out); retryErr == nil && len(out.Orders) > 0 {
err = nil
}
}
}
if err != nil {
return "", err
}
if len(out.Orders) == 0 {
@@ -407,7 +466,20 @@ func isQueueDeadError(err error) bool {
return strings.Contains(msg, "订单不可操作") ||
strings.Contains(msg, "不可操作") ||
strings.Contains(msg, "订单不存在") ||
strings.Contains(msg, "队列已失效")
strings.Contains(msg, "队列已失效") ||
strings.Contains(msg, "正在处理中") ||
strings.Contains(msg, "请勿重复提交")
}
func isRateLimitError(err error) bool {
if err == nil {
return false
}
msg := strings.ToLower(err.Error())
return strings.Contains(msg, "请求过于频繁") ||
strings.Contains(msg, "稍后再试") ||
strings.Contains(msg, "too many requests") ||
strings.Contains(msg, "rate limit")
}
func isTransientDeliveryError(err error, statusCode int, message string) bool {
@@ -437,7 +509,10 @@ func (p *DLCProvider) signProxy(path, method string, data interface{}, out inter
return err
}
const maxRetries = 2
maxRetries := 2
if path == "/public/games/account-bound" {
maxRetries = 0 // 轮询绑定接口不触发重试睡眠,避免堆积阻塞
}
var lastErr error
for attempt := 0; attempt <= maxRetries; attempt++ {