mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 07:05:24 +00:00
fix(handler): avoid cache billing on same-account retry
This commit is contained in:
1 parent
d4b9797ff7
commit
a2acbf553b
2 files changed
+46
-7
No files matched your search
@@ -83,8 +83,9 @@ func (s *FailoverState) HandleFailoverError(
|
|||||||
return FailoverExhausted
|
return FailoverExhausted
|
||||||
}
|
}
|
||||||
|
|
||||||
// 缓存计费判断
|
// 同账号重试不算切换账号,粘性会话仅在实际切换时强制缓存计费。
|
||||||
if needForceCacheBilling(s.hasBoundSession, failoverErr) {
|
sameAccountRetry := failoverErr.RetryableOnSameAccount && s.SameAccountRetryCount[accountID] < retryLimit
|
||||||
|
if needForceCacheBilling(s.hasBoundSession, failoverErr, sameAccountRetry) {
|
||||||
s.ForceCacheBilling = true
|
s.ForceCacheBilling = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,9 +175,9 @@ func (s *FailoverState) HandleSelectionExhausted(ctx context.Context) FailoverAc
|
|||||||
}
|
}
|
||||||
|
|
||||||
// needForceCacheBilling 判断 failover 时是否需要强制缓存计费。
|
// needForceCacheBilling 判断 failover 时是否需要强制缓存计费。
|
||||||
// 粘性会话切换账号、或上游明确标记时,将 input_tokens 转为 cache_read 计费。
|
// 粘性会话实际切换账号、或上游明确标记时,将 input_tokens 转为 cache_read 计费。
|
||||||
func needForceCacheBilling(hasBoundSession bool, failoverErr *service.UpstreamFailoverError) bool {
|
func needForceCacheBilling(hasBoundSession bool, failoverErr *service.UpstreamFailoverError, sameAccountRetry bool) bool {
|
||||||
return hasBoundSession || (failoverErr != nil && failoverErr.ForceCacheBilling)
|
return (hasBoundSession && !sameAccountRetry) || (failoverErr != nil && failoverErr.ForceCacheBilling)
|
||||||
}
|
}
|
||||||
|
|
||||||
// failoverClientGone 判断下游客户端是否已断开(请求 context 已取消)。
|
// failoverClientGone 判断下游客户端是否已断开(请求 context 已取消)。
|
||||||
|
|||||||
@@ -271,7 +271,7 @@ func TestHandleFailoverError_BasicSwitch(t *testing.T) {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
func TestHandleFailoverError_CacheBilling(t *testing.T) {
|
func TestHandleFailoverError_CacheBilling(t *testing.T) {
|
||||||
t.Run("hasBoundSession为true时设置ForceCacheBilling", func(t *testing.T) {
|
t.Run("hasBoundSession为true且实际切换时设置ForceCacheBilling", func(t *testing.T) {
|
||||||
mock := &mockTempUnscheduler{}
|
mock := &mockTempUnscheduler{}
|
||||||
fs := NewFailoverState(3, true) // hasBoundSession=true
|
fs := NewFailoverState(3, true) // hasBoundSession=true
|
||||||
err := newTestFailoverErr(500, false, false)
|
err := newTestFailoverErr(500, false, false)
|
||||||
@@ -280,6 +280,32 @@ func TestHandleFailoverError_CacheBilling(t *testing.T) {
|
|||||||
require.True(t, fs.ForceCacheBilling)
|
require.True(t, fs.ForceCacheBilling)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("同账号重试时仅凭hasBoundSession不设置ForceCacheBilling", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, true)
|
||||||
|
err := newTestFailoverErr(400, true, false)
|
||||||
|
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", maxSameAccountRetries, err)
|
||||||
|
|
||||||
|
require.False(t, fs.ForceCacheBilling)
|
||||||
|
require.Zero(t, fs.SwitchCount)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("同账号重试耗尽并实际切换时设置ForceCacheBilling", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, true)
|
||||||
|
err := newTestFailoverErr(400, true, false)
|
||||||
|
|
||||||
|
for i := 0; i < maxSameAccountRetries; i++ {
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", maxSameAccountRetries, err)
|
||||||
|
require.False(t, fs.ForceCacheBilling)
|
||||||
|
}
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", maxSameAccountRetries, err)
|
||||||
|
|
||||||
|
require.True(t, fs.ForceCacheBilling)
|
||||||
|
require.Equal(t, 1, fs.SwitchCount)
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("failoverErr.ForceCacheBilling为true时设置", func(t *testing.T) {
|
t.Run("failoverErr.ForceCacheBilling为true时设置", func(t *testing.T) {
|
||||||
mock := &mockTempUnscheduler{}
|
mock := &mockTempUnscheduler{}
|
||||||
fs := NewFailoverState(3, false)
|
fs := NewFailoverState(3, false)
|
||||||
@@ -289,6 +315,17 @@ func TestHandleFailoverError_CacheBilling(t *testing.T) {
|
|||||||
require.True(t, fs.ForceCacheBilling)
|
require.True(t, fs.ForceCacheBilling)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("同账号重试保留显式ForceCacheBilling", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, true)
|
||||||
|
err := newTestFailoverErr(400, true, true)
|
||||||
|
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", maxSameAccountRetries, err)
|
||||||
|
|
||||||
|
require.True(t, fs.ForceCacheBilling)
|
||||||
|
require.Zero(t, fs.SwitchCount)
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("两者均为false时不设置", func(t *testing.T) {
|
t.Run("两者均为false时不设置", func(t *testing.T) {
|
||||||
mock := &mockTempUnscheduler{}
|
mock := &mockTempUnscheduler{}
|
||||||
fs := NewFailoverState(3, false)
|
fs := NewFailoverState(3, false)
|
||||||
@@ -633,13 +670,14 @@ func TestHandleFailoverError_IntegrationScenario(t *testing.T) {
|
|||||||
for i := 0; i < maxSameAccountRetries; i++ {
|
for i := 0; i < maxSameAccountRetries; i++ {
|
||||||
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", maxSameAccountRetries, retryErr)
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", maxSameAccountRetries, retryErr)
|
||||||
require.Equal(t, FailoverContinue, action)
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.False(t, fs.ForceCacheBilling, "同账号重试期间不应仅因绑定会话强制缓存计费")
|
||||||
}
|
}
|
||||||
require.True(t, fs.ForceCacheBilling, "hasBoundSession=true 应设置 ForceCacheBilling")
|
|
||||||
|
|
||||||
// 2. 账号 100 超过重试上限 → TempUnschedule + 切换
|
// 2. 账号 100 超过重试上限 → TempUnschedule + 切换
|
||||||
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", maxSameAccountRetries, retryErr)
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", maxSameAccountRetries, retryErr)
|
||||||
require.Equal(t, FailoverContinue, action)
|
require.Equal(t, FailoverContinue, action)
|
||||||
require.Equal(t, 1, fs.SwitchCount)
|
require.Equal(t, 1, fs.SwitchCount)
|
||||||
|
require.True(t, fs.ForceCacheBilling, "实际切换账号时应设置 ForceCacheBilling")
|
||||||
require.Len(t, mock.calls, 1)
|
require.Len(t, mock.calls, 1)
|
||||||
|
|
||||||
// 3. 账号 200 遇到不可重试错误 → 直接切换
|
// 3. 账号 200 遇到不可重试错误 → 直接切换
|
||||||
|
|||||||
Reference in new issue
Block a user