fix(sora): 优化 challenge 重试与调试日志
This commit is contained in:
@@ -13,9 +13,11 @@ import (
|
||||
)
|
||||
|
||||
type soraChallengeCooldownEntry struct {
|
||||
Until time.Time
|
||||
StatusCode int
|
||||
CFRay string
|
||||
Until time.Time
|
||||
StatusCode int
|
||||
CFRay string
|
||||
ConsecutiveChallenges int
|
||||
LastChallengeAt time.Time
|
||||
}
|
||||
|
||||
type soraSidecarSessionEntry struct {
|
||||
@@ -67,6 +69,9 @@ func (c *SoraDirectClient) checkCloudflareChallengeCooldown(account *Account, pr
|
||||
remaining = 1
|
||||
}
|
||||
message := fmt.Sprintf("Sora request cooling down due to recent Cloudflare challenge. Retry in %d seconds.", remaining)
|
||||
if entry.ConsecutiveChallenges > 1 {
|
||||
message = fmt.Sprintf("%s (streak=%d)", message, entry.ConsecutiveChallenges)
|
||||
}
|
||||
if entry.CFRay != "" {
|
||||
message = fmt.Sprintf("%s (last cf-ray: %s)", message, entry.CFRay)
|
||||
}
|
||||
@@ -90,22 +95,33 @@ func (c *SoraDirectClient) recordCloudflareChallengeCooldown(account *Account, p
|
||||
}
|
||||
key := soraAccountProxyKey(account, proxyURL)
|
||||
now := time.Now()
|
||||
until := now.Add(time.Duration(cooldownSeconds) * time.Second)
|
||||
cfRay := soraerror.ExtractCloudflareRayID(headers, body)
|
||||
|
||||
c.challengeCooldownMu.Lock()
|
||||
c.cleanupExpiredChallengeCooldownsLocked(now)
|
||||
|
||||
streak := 1
|
||||
existing, ok := c.challengeCooldowns[key]
|
||||
if ok && now.Sub(existing.LastChallengeAt) <= 30*time.Minute {
|
||||
streak = existing.ConsecutiveChallenges + 1
|
||||
}
|
||||
effectiveCooldown := soraComputeChallengeCooldownSeconds(cooldownSeconds, streak)
|
||||
until := now.Add(time.Duration(effectiveCooldown) * time.Second)
|
||||
if ok && existing.Until.After(until) {
|
||||
until = existing.Until
|
||||
if existing.ConsecutiveChallenges > streak {
|
||||
streak = existing.ConsecutiveChallenges
|
||||
}
|
||||
if cfRay == "" {
|
||||
cfRay = existing.CFRay
|
||||
}
|
||||
}
|
||||
c.challengeCooldowns[key] = soraChallengeCooldownEntry{
|
||||
Until: until,
|
||||
StatusCode: statusCode,
|
||||
CFRay: cfRay,
|
||||
Until: until,
|
||||
StatusCode: statusCode,
|
||||
CFRay: cfRay,
|
||||
ConsecutiveChallenges: streak,
|
||||
LastChallengeAt: now,
|
||||
}
|
||||
c.challengeCooldownMu.Unlock()
|
||||
|
||||
@@ -114,7 +130,44 @@ func (c *SoraDirectClient) recordCloudflareChallengeCooldown(account *Account, p
|
||||
if remain < 0 {
|
||||
remain = 0
|
||||
}
|
||||
c.debugLogf("cloudflare_challenge_cooldown_set key=%s status=%d remain_s=%d cf_ray=%s", key, statusCode, remain, cfRay)
|
||||
c.debugLogf("cloudflare_challenge_cooldown_set key=%s status=%d remain_s=%d streak=%d cf_ray=%s", key, statusCode, remain, streak, cfRay)
|
||||
}
|
||||
}
|
||||
|
||||
func soraComputeChallengeCooldownSeconds(baseSeconds, streak int) int {
|
||||
if baseSeconds <= 0 {
|
||||
return 0
|
||||
}
|
||||
if streak < 1 {
|
||||
streak = 1
|
||||
}
|
||||
multiplier := streak
|
||||
if multiplier > 4 {
|
||||
multiplier = 4
|
||||
}
|
||||
cooldown := baseSeconds * multiplier
|
||||
if cooldown > 3600 {
|
||||
cooldown = 3600
|
||||
}
|
||||
return cooldown
|
||||
}
|
||||
|
||||
func (c *SoraDirectClient) clearCloudflareChallengeCooldown(account *Account, proxyURL string) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
if account == nil || account.ID <= 0 {
|
||||
return
|
||||
}
|
||||
key := soraAccountProxyKey(account, proxyURL)
|
||||
c.challengeCooldownMu.Lock()
|
||||
_, existed := c.challengeCooldowns[key]
|
||||
if existed {
|
||||
delete(c.challengeCooldowns, key)
|
||||
}
|
||||
c.challengeCooldownMu.Unlock()
|
||||
if existed && c.debugEnabled() {
|
||||
c.debugLogf("cloudflare_challenge_cooldown_cleared key=%s", key)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user