mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 15:06:36 +00:00
feat: add affinity scheduling controls
This commit is contained in:
1 parent
338e3df8d9
commit
44ec2626cf
44 files changed
+3711
-496
No files matched your search
@@ -1204,16 +1204,21 @@ func (a *Account) IsSessionIDMaskingEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsClientAffinityEnabled 检查是否启用客户端亲和调度
|
||||
// 仅适用于 Anthropic 账号(OAuth/SetupToken/APIKey)
|
||||
// 启用后,新会话会优先调度到之前使用过的账号
|
||||
func (a *Account) IsClientAffinityEnabled() bool {
|
||||
// IsAffinityEnabled 检查是否启用亲和调度(统一入口)
|
||||
// 仅适用于 Anthropic 账号,同时检查新字段 affinity_enabled 和旧字段 client_affinity_enabled(向后兼容)
|
||||
func (a *Account) IsAffinityEnabled() bool {
|
||||
if a.Platform != PlatformAnthropic {
|
||||
return false
|
||||
}
|
||||
if a.Extra == nil {
|
||||
return false
|
||||
}
|
||||
if v, ok := a.Extra["affinity_enabled"]; ok {
|
||||
if enabled, ok := v.(bool); ok {
|
||||
return enabled
|
||||
}
|
||||
return false
|
||||
}
|
||||
if v, ok := a.Extra["client_affinity_enabled"]; ok {
|
||||
if enabled, ok := v.(bool); ok {
|
||||
return enabled
|
||||
@@ -1222,6 +1227,11 @@ func (a *Account) IsClientAffinityEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsClientAffinityEnabled 向后兼容别名,内部调用 IsAffinityEnabled
|
||||
func (a *Account) IsClientAffinityEnabled() bool {
|
||||
return a.IsAffinityEnabled()
|
||||
}
|
||||
|
||||
// AffinityZone 表示账号的客户端亲和分区
|
||||
type AffinityZone int
|
||||
|
||||
@@ -1288,6 +1298,150 @@ func (a *Account) GetAffinityZone(clientCount int64) AffinityZone {
|
||||
return AffinityZoneRed
|
||||
}
|
||||
|
||||
// IsAffinityAllowSwitch 检查亲和账号是否允许在无绿区时切换到其他账号
|
||||
// 默认 true(允许切换),设为 false 时即使全部红区也不切换
|
||||
func (a *Account) IsAffinityAllowSwitch() bool {
|
||||
if a.Extra == nil {
|
||||
return true
|
||||
}
|
||||
if v, ok := a.Extra["affinity_allow_switch"]; ok {
|
||||
if allow, ok := v.(bool); ok {
|
||||
return allow
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// GetPinnedUsers 获取指定亲和用户列表
|
||||
func (a *Account) GetPinnedUsers() []int64 {
|
||||
if a.Extra == nil {
|
||||
return nil
|
||||
}
|
||||
v, ok := a.Extra["pinned_users"]
|
||||
if !ok || v == nil {
|
||||
return nil
|
||||
}
|
||||
arr, ok := v.([]any)
|
||||
if !ok || len(arr) == 0 {
|
||||
return nil
|
||||
}
|
||||
result := make([]int64, 0, len(arr))
|
||||
for _, item := range arr {
|
||||
switch id := item.(type) {
|
||||
case float64:
|
||||
result = append(result, int64(id))
|
||||
case int64:
|
||||
result = append(result, id)
|
||||
case int:
|
||||
result = append(result, int64(id))
|
||||
case json.Number:
|
||||
if i, err := id.Int64(); err == nil {
|
||||
result = append(result, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// IsPinnedUser 检查 userID 是否在指定亲和用户列表中
|
||||
func (a *Account) IsPinnedUser(userID int64) bool {
|
||||
for _, id := range a.GetPinnedUsers() {
|
||||
if id == userID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// GetAffinityUserBase 获取用户维度亲和基础限制(绿区上限),0 表示未配置
|
||||
func (a *Account) GetAffinityUserBase() int {
|
||||
if a.Extra == nil {
|
||||
return 0
|
||||
}
|
||||
if v, ok := a.Extra["affinity_user_base"]; ok {
|
||||
return parseExtraInt(v)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetAffinityUserBuffer 获取用户维度亲和缓冲区大小(黄区范围)
|
||||
// 返回 (value, configured),语义与 GetAffinityBuffer 相同
|
||||
func (a *Account) GetAffinityUserBuffer() (int, bool) {
|
||||
if a.Extra == nil {
|
||||
return 0, false
|
||||
}
|
||||
v, ok := a.Extra["affinity_user_buffer"]
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
if v == nil {
|
||||
return 0, false
|
||||
}
|
||||
return parseExtraInt(v), true
|
||||
}
|
||||
|
||||
// GetPerUserClientLimit 获取每用户客户端限制,0 表示不限制
|
||||
func (a *Account) GetPerUserClientLimit() int {
|
||||
if a.Extra == nil {
|
||||
return 0
|
||||
}
|
||||
if v, ok := a.Extra["per_user_client_limit"]; ok {
|
||||
return parseExtraInt(v)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// getAffinityZoneForDim 根据单一维度的计数和三区参数计算亲和分区
|
||||
func getAffinityZoneForDim(count int64, base int, buffer int, bufferConfigured bool) AffinityZone {
|
||||
if base <= 0 {
|
||||
return AffinityZoneGreen
|
||||
}
|
||||
if count <= int64(base) {
|
||||
return AffinityZoneGreen
|
||||
}
|
||||
if !bufferConfigured {
|
||||
return AffinityZoneYellow
|
||||
}
|
||||
if buffer == 0 {
|
||||
return AffinityZoneRed
|
||||
}
|
||||
if count <= int64(base+buffer) {
|
||||
return AffinityZoneYellow
|
||||
}
|
||||
return AffinityZoneRed
|
||||
}
|
||||
|
||||
// GetMultiDimAffinityZone 根据多维度计数计算亲和分区,取所有维度中最严格的区域。
|
||||
// 未开启亲和的账号永远返回绿区。
|
||||
func (a *Account) GetMultiDimAffinityZone(userCount, clientCount, perUserCount int64) AffinityZone {
|
||||
if !a.IsAffinityEnabled() {
|
||||
return AffinityZoneGreen
|
||||
}
|
||||
worst := AffinityZoneGreen
|
||||
|
||||
// 客户端维度
|
||||
clientBase := a.GetAffinityBase()
|
||||
clientBuffer, clientBufCfg := a.GetAffinityBuffer()
|
||||
if z := getAffinityZoneForDim(clientCount, clientBase, clientBuffer, clientBufCfg); z > worst {
|
||||
worst = z
|
||||
}
|
||||
|
||||
// 用户维度
|
||||
userBase := a.GetAffinityUserBase()
|
||||
userBuffer, userBufCfg := a.GetAffinityUserBuffer()
|
||||
if z := getAffinityZoneForDim(userCount, userBase, userBuffer, userBufCfg); z > worst {
|
||||
worst = z
|
||||
}
|
||||
|
||||
// 每用户客户端维度
|
||||
perUserLimit := a.GetPerUserClientLimit()
|
||||
if perUserLimit > 0 && perUserCount > int64(perUserLimit) {
|
||||
worst = AffinityZoneRed
|
||||
}
|
||||
|
||||
return worst
|
||||
}
|
||||
|
||||
// IsCacheTTLOverrideEnabled 检查是否启用缓存 TTL 强制替换
|
||||
// 仅适用于 Anthropic OAuth/SetupToken 类型账号
|
||||
// 启用后将所有 cache creation tokens 归入指定的 TTL 类型(5m 或 1h)
|
||||
|
||||
@@ -30,12 +30,15 @@ func (c *stubSmartRetryCache) DeleteSessionAccountID(_ context.Context, groupID
|
||||
c.deleteCalls = append(c.deleteCalls, deleteSessionCall{groupID: groupID, sessionHash: sessionHash})
|
||||
return nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) GetClientAffinityAccounts(_ context.Context, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
func (c *stubSmartRetryCache) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) UpdateClientAffinity(_ context.Context, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
func (c *stubSmartRetryCache) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
package service
|
||||
|
||||
import "context"
|
||||
|
||||
// gatewayAffinityFlow encapsulates affinity-specific scheduling steps so the
|
||||
// main account selection flow can stay focused on generic scheduling.
|
||||
type gatewayAffinityFlow struct {
|
||||
svc *GatewayService
|
||||
ctx context.Context
|
||||
groupID *int64
|
||||
sessionHash string
|
||||
requestedModel string
|
||||
affinityClientID string
|
||||
affinityUserID int64
|
||||
platform string
|
||||
useMixed bool
|
||||
accountByID map[int64]*Account
|
||||
isExcluded func(int64) bool
|
||||
}
|
||||
|
||||
type affinityWaitCandidate struct {
|
||||
account *Account
|
||||
}
|
||||
|
||||
func newGatewayAffinityFlow(
|
||||
svc *GatewayService,
|
||||
ctx context.Context,
|
||||
groupID *int64,
|
||||
sessionHash string,
|
||||
requestedModel string,
|
||||
affinityClientID string,
|
||||
affinityUserID int64,
|
||||
platform string,
|
||||
useMixed bool,
|
||||
accountByID map[int64]*Account,
|
||||
isExcluded func(int64) bool,
|
||||
) *gatewayAffinityFlow {
|
||||
return &gatewayAffinityFlow{
|
||||
svc: svc,
|
||||
ctx: ctx,
|
||||
groupID: groupID,
|
||||
sessionHash: sessionHash,
|
||||
requestedModel: requestedModel,
|
||||
affinityClientID: affinityClientID,
|
||||
affinityUserID: affinityUserID,
|
||||
platform: platform,
|
||||
useMixed: useMixed,
|
||||
accountByID: accountByID,
|
||||
isExcluded: isExcluded,
|
||||
}
|
||||
}
|
||||
|
||||
// shouldFilterAccountWithoutClientID excludes affinity-enabled Anthropic accounts
|
||||
// when metadata.user_id does not provide a usable client_id.
|
||||
func shouldFilterAccountWithoutClientID(account *Account, affinityClientID string) bool {
|
||||
if account == nil || affinityClientID != "" {
|
||||
return false
|
||||
}
|
||||
if account.Platform != PlatformAnthropic {
|
||||
return false
|
||||
}
|
||||
return account.IsAffinityEnabled()
|
||||
}
|
||||
|
||||
func filterAccountsWithoutClientID(accounts []Account, affinityClientID string) []Account {
|
||||
if affinityClientID != "" {
|
||||
return accounts
|
||||
}
|
||||
filtered := make([]Account, 0, len(accounts))
|
||||
for _, acc := range accounts {
|
||||
if shouldFilterAccountWithoutClientID(&acc, affinityClientID) {
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, acc)
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
func (f *gatewayAffinityFlow) preprocessPinnedUsers(accounts []Account) {
|
||||
if f.affinityUserID <= 0 || f.affinityClientID == "" || f.svc.cache == nil {
|
||||
return
|
||||
}
|
||||
for i := range accounts {
|
||||
if accounts[i].IsPinnedUser(f.affinityUserID) && accounts[i].IsAffinityEnabled() {
|
||||
_ = f.svc.cache.UpdateAffinity(
|
||||
f.ctx,
|
||||
derefGroupID(f.groupID),
|
||||
f.affinityUserID,
|
||||
f.affinityClientID,
|
||||
accounts[i].ID,
|
||||
ClientAffinityTTL,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// trySelectAffinityAccount runs Layer 1.4 and returns:
|
||||
// - result != nil: affinity path selected an account or wait plan
|
||||
// - affinityHit == true: an effective affinity-enabled record was considered and should suppress sticky fallback
|
||||
func (f *gatewayAffinityFlow) trySelectAffinityAccount() (*AccountSelectionResult, bool, error) {
|
||||
if f.affinityClientID == "" || f.affinityUserID <= 0 || f.svc.cache == nil {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
gid := derefGroupID(f.groupID)
|
||||
affinityAccountIDs, err := f.svc.cache.GetAffinityAccounts(f.ctx, gid, f.affinityUserID, f.affinityClientID, ClientAffinityTTL)
|
||||
if err != nil || len(affinityAccountIDs) == 0 {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
noSwitchBlocked := false
|
||||
anyAllowSwitch := false
|
||||
effectiveAffinityHit := false
|
||||
waitCandidates := make(map[int64]*affinityWaitCandidate)
|
||||
|
||||
for _, affinityAccID := range affinityAccountIDs {
|
||||
account, ok := f.accountByID[affinityAccID]
|
||||
|
||||
if f.isExcluded != nil && f.isExcluded(affinityAccID) {
|
||||
checkAcc := account
|
||||
if !ok && f.svc.accountRepo != nil {
|
||||
if acc, repoErr := f.svc.accountRepo.GetByID(f.ctx, affinityAccID); repoErr == nil && acc != nil {
|
||||
checkAcc = acc
|
||||
}
|
||||
}
|
||||
if checkAcc != nil && checkAcc.IsAffinityEnabled() {
|
||||
effectiveAffinityHit = true
|
||||
if !checkAcc.IsAffinityAllowSwitch() {
|
||||
noSwitchBlocked = true
|
||||
} else {
|
||||
anyAllowSwitch = true
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if !ok || !f.svc.isAccountSchedulableForSelection(account) {
|
||||
checkAcc := account
|
||||
if !ok && f.svc.accountRepo != nil {
|
||||
if acc, repoErr := f.svc.accountRepo.GetByID(f.ctx, affinityAccID); repoErr == nil && acc != nil {
|
||||
checkAcc = acc
|
||||
}
|
||||
}
|
||||
if checkAcc != nil && checkAcc.IsAffinityEnabled() {
|
||||
effectiveAffinityHit = true
|
||||
if !checkAcc.IsAffinityAllowSwitch() {
|
||||
noSwitchBlocked = true
|
||||
} else {
|
||||
anyAllowSwitch = true
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if !account.IsAffinityEnabled() {
|
||||
continue
|
||||
}
|
||||
effectiveAffinityHit = true
|
||||
if account.IsAffinityAllowSwitch() {
|
||||
anyAllowSwitch = true
|
||||
}
|
||||
|
||||
if !f.svc.isAccountAllowedForPlatform(account, f.platform, f.useMixed) ||
|
||||
(f.requestedModel != "" && !f.svc.isModelSupportedByAccountWithContext(f.ctx, account, f.requestedModel)) ||
|
||||
!f.svc.isAccountSchedulableForModelSelection(f.ctx, account, f.requestedModel) ||
|
||||
!f.svc.isAccountSchedulableForQuota(account) ||
|
||||
!f.svc.isAccountSchedulableForWindowCost(f.ctx, account, false) ||
|
||||
!f.svc.isAccountSchedulableForRPM(f.ctx, account, false) {
|
||||
if !account.IsAffinityAllowSwitch() {
|
||||
noSwitchBlocked = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
userCount, clientCount, perUserCount, multiErr := f.svc.cache.GetAffinityMultiCount(
|
||||
f.ctx, gid, affinityAccID, f.affinityUserID, ClientAffinityTTL,
|
||||
)
|
||||
if multiErr == nil {
|
||||
zone := account.GetMultiDimAffinityZone(userCount, clientCount, perUserCount)
|
||||
if zone == AffinityZoneRed {
|
||||
if !account.IsAffinityAllowSwitch() {
|
||||
noSwitchBlocked = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
result, acquireErr := f.svc.tryAcquireAccountSlot(f.ctx, affinityAccID, account.Concurrency)
|
||||
if acquireErr == nil && result.Acquired {
|
||||
if !f.svc.checkAndRegisterSession(f.ctx, account, f.sessionHash) {
|
||||
result.ReleaseFunc()
|
||||
continue
|
||||
}
|
||||
_ = f.svc.cache.UpdateAffinity(f.ctx, gid, f.affinityUserID, f.affinityClientID, affinityAccID, ClientAffinityTTL)
|
||||
if f.sessionHash != "" {
|
||||
_ = f.svc.cache.SetSessionAccountID(f.ctx, gid, f.sessionHash, affinityAccID, stickySessionTTL)
|
||||
}
|
||||
return &AccountSelectionResult{
|
||||
Account: account,
|
||||
Acquired: true,
|
||||
ReleaseFunc: result.ReleaseFunc,
|
||||
}, true, nil
|
||||
}
|
||||
if acquireErr == nil && !result.Acquired && !account.IsAffinityAllowSwitch() {
|
||||
noSwitchBlocked = true
|
||||
waitCandidates[affinityAccID] = &affinityWaitCandidate{account: account}
|
||||
}
|
||||
}
|
||||
|
||||
if noSwitchBlocked && !anyAllowSwitch && f.svc.concurrencyService != nil {
|
||||
for _, waitAccID := range affinityAccountIDs {
|
||||
candidate, ok := waitCandidates[waitAccID]
|
||||
if !ok || candidate == nil || candidate.account == nil {
|
||||
continue
|
||||
}
|
||||
acc := candidate.account
|
||||
waitingCount, _ := f.svc.concurrencyService.GetAccountWaitingCount(f.ctx, waitAccID)
|
||||
if waitingCount >= f.svc.schedulingConfig().StickySessionMaxWaiting {
|
||||
continue
|
||||
}
|
||||
if !f.svc.checkAndRegisterSession(f.ctx, acc, f.sessionHash) {
|
||||
continue
|
||||
}
|
||||
cfg := f.svc.schedulingConfig()
|
||||
return &AccountSelectionResult{
|
||||
Account: acc,
|
||||
WaitPlan: &AccountWaitPlan{
|
||||
AccountID: waitAccID,
|
||||
MaxConcurrency: acc.Concurrency,
|
||||
Timeout: cfg.StickySessionWaitTimeout,
|
||||
MaxWaiting: cfg.StickySessionMaxWaiting,
|
||||
},
|
||||
}, true, nil
|
||||
}
|
||||
return nil, effectiveAffinityHit, ErrAffinityNoSwitch
|
||||
}
|
||||
|
||||
return nil, effectiveAffinityHit, nil
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -36,12 +37,15 @@ func (m *mockAffinityCache) RefreshSessionTTL(_ context.Context, _ int64, _ stri
|
||||
func (m *mockAffinityCache) DeleteSessionAccountID(_ context.Context, _ int64, _ string) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockAffinityCache) GetClientAffinityAccounts(_ context.Context, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
func (m *mockAffinityCache) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *mockAffinityCache) UpdateClientAffinity(_ context.Context, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
func (m *mockAffinityCache) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockAffinityCache) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (m *mockAffinityCache) GetAccountAffinityCountBatch(ctx context.Context, groupID int64, accountIDs []int64, ttl time.Duration) (map[int64]int64, error) {
|
||||
m.getCountBatchCalls++
|
||||
if m.getCountBatchFunc != nil {
|
||||
@@ -613,6 +617,28 @@ func TestAffinityIsClientAffinityEnabled(t *testing.T) {
|
||||
}
|
||||
assert.False(t, acc.IsClientAffinityEnabled(), "string 'true' should not enable affinity")
|
||||
})
|
||||
|
||||
t.Run("new flag false overrides legacy flag true", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": false,
|
||||
"client_affinity_enabled": true,
|
||||
},
|
||||
}
|
||||
assert.False(t, acc.IsClientAffinityEnabled())
|
||||
})
|
||||
|
||||
t.Run("new flag true overrides legacy flag false", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"client_affinity_enabled": false,
|
||||
},
|
||||
}
|
||||
assert.True(t, acc.IsClientAffinityEnabled())
|
||||
})
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
@@ -798,3 +824,936 @@ func TestClassifyByAffinityZone(t *testing.T) {
|
||||
require.Len(t, result, 2)
|
||||
})
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// GetMultiDimAffinityZone 测试
|
||||
// ===========================================================================
|
||||
|
||||
func TestGetMultiDimAffinityZone(t *testing.T) {
|
||||
makeAccount := func(opts map[string]any) *Account {
|
||||
extra := map[string]any{"affinity_enabled": true}
|
||||
for k, v := range opts {
|
||||
extra[k] = v
|
||||
}
|
||||
return &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: extra,
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("user green + client green = green", func(t *testing.T) {
|
||||
acc := makeAccount(map[string]any{
|
||||
"affinity_base": 10,
|
||||
"affinity_buffer": 5,
|
||||
"affinity_user_base": 3,
|
||||
"affinity_user_buffer": 2,
|
||||
})
|
||||
assert.Equal(t, AffinityZoneGreen, acc.GetMultiDimAffinityZone(2, 5, 0))
|
||||
})
|
||||
|
||||
t.Run("user green + client yellow = yellow", func(t *testing.T) {
|
||||
acc := makeAccount(map[string]any{
|
||||
"affinity_base": 10,
|
||||
"affinity_buffer": 5,
|
||||
"affinity_user_base": 3,
|
||||
"affinity_user_buffer": 2,
|
||||
})
|
||||
assert.Equal(t, AffinityZoneYellow, acc.GetMultiDimAffinityZone(2, 12, 0))
|
||||
})
|
||||
|
||||
t.Run("user yellow + client green = yellow", func(t *testing.T) {
|
||||
acc := makeAccount(map[string]any{
|
||||
"affinity_base": 10,
|
||||
"affinity_buffer": 5,
|
||||
"affinity_user_base": 3,
|
||||
"affinity_user_buffer": 2,
|
||||
})
|
||||
assert.Equal(t, AffinityZoneYellow, acc.GetMultiDimAffinityZone(4, 5, 0))
|
||||
})
|
||||
|
||||
t.Run("user red + client green = red", func(t *testing.T) {
|
||||
acc := makeAccount(map[string]any{
|
||||
"affinity_base": 10,
|
||||
"affinity_buffer": 5,
|
||||
"affinity_user_base": 3,
|
||||
"affinity_user_buffer": 0, // no yellow, direct red
|
||||
})
|
||||
assert.Equal(t, AffinityZoneRed, acc.GetMultiDimAffinityZone(4, 5, 0))
|
||||
})
|
||||
|
||||
t.Run("user green + client red = red", func(t *testing.T) {
|
||||
acc := makeAccount(map[string]any{
|
||||
"affinity_base": 10,
|
||||
"affinity_buffer": 0, // no yellow, direct red
|
||||
"affinity_user_base": 3,
|
||||
"affinity_user_buffer": 2,
|
||||
})
|
||||
assert.Equal(t, AffinityZoneRed, acc.GetMultiDimAffinityZone(2, 11, 0))
|
||||
})
|
||||
|
||||
t.Run("user_base=0 only checks client dimension", func(t *testing.T) {
|
||||
acc := makeAccount(map[string]any{
|
||||
"affinity_base": 10,
|
||||
"affinity_buffer": 5,
|
||||
// no affinity_user_base → 0 → user dimension always green
|
||||
})
|
||||
// client in yellow range
|
||||
assert.Equal(t, AffinityZoneYellow, acc.GetMultiDimAffinityZone(100, 12, 0))
|
||||
})
|
||||
|
||||
t.Run("perUserCount exceeds limit = red", func(t *testing.T) {
|
||||
acc := makeAccount(map[string]any{
|
||||
"affinity_base": 10,
|
||||
"affinity_buffer": 5,
|
||||
"per_user_client_limit": 3,
|
||||
})
|
||||
// client green, user green, but perUser > limit
|
||||
assert.Equal(t, AffinityZoneRed, acc.GetMultiDimAffinityZone(1, 5, 4))
|
||||
})
|
||||
|
||||
t.Run("perUserCount within limit = green", func(t *testing.T) {
|
||||
acc := makeAccount(map[string]any{
|
||||
"affinity_base": 10,
|
||||
"affinity_buffer": 5,
|
||||
"per_user_client_limit": 3,
|
||||
})
|
||||
assert.Equal(t, AffinityZoneGreen, acc.GetMultiDimAffinityZone(1, 5, 3))
|
||||
})
|
||||
|
||||
t.Run("affinity disabled always green", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": false,
|
||||
"affinity_base": 1,
|
||||
"affinity_buffer": 0,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, AffinityZoneGreen, acc.GetMultiDimAffinityZone(100, 100, 100))
|
||||
})
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// IsAffinityAllowSwitch 测试
|
||||
// ===========================================================================
|
||||
|
||||
func TestIsAffinityAllowSwitch(t *testing.T) {
|
||||
t.Run("default true when no field in Extra", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{"affinity_enabled": true},
|
||||
}
|
||||
assert.True(t, acc.IsAffinityAllowSwitch())
|
||||
})
|
||||
|
||||
t.Run("explicit false", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{"affinity_allow_switch": false},
|
||||
}
|
||||
assert.False(t, acc.IsAffinityAllowSwitch())
|
||||
})
|
||||
|
||||
t.Run("Extra nil defaults to true", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: nil,
|
||||
}
|
||||
assert.True(t, acc.IsAffinityAllowSwitch())
|
||||
})
|
||||
|
||||
t.Run("explicit true", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{"affinity_allow_switch": true},
|
||||
}
|
||||
assert.True(t, acc.IsAffinityAllowSwitch())
|
||||
})
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// GetPinnedUsers / IsPinnedUser 测试
|
||||
// ===========================================================================
|
||||
|
||||
func TestGetPinnedUsers(t *testing.T) {
|
||||
t.Run("normal list", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{
|
||||
"pinned_users": []any{float64(10), float64(20), float64(30)},
|
||||
},
|
||||
}
|
||||
result := acc.GetPinnedUsers()
|
||||
assert.Equal(t, []int64{10, 20, 30}, result)
|
||||
})
|
||||
|
||||
t.Run("empty list", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{
|
||||
"pinned_users": []any{},
|
||||
},
|
||||
}
|
||||
result := acc.GetPinnedUsers()
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
|
||||
t.Run("Extra nil", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: nil,
|
||||
}
|
||||
result := acc.GetPinnedUsers()
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
|
||||
t.Run("pinned_users not set", func(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{},
|
||||
}
|
||||
result := acc.GetPinnedUsers()
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIsPinnedUser(t *testing.T) {
|
||||
acc := &Account{
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{
|
||||
"pinned_users": []any{float64(10), float64(20), float64(30)},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("hit", func(t *testing.T) {
|
||||
assert.True(t, acc.IsPinnedUser(20))
|
||||
})
|
||||
|
||||
t.Run("miss", func(t *testing.T) {
|
||||
assert.False(t, acc.IsPinnedUser(99))
|
||||
})
|
||||
|
||||
t.Run("nil Extra", func(t *testing.T) {
|
||||
nilAcc := &Account{Platform: PlatformAnthropic, Extra: nil}
|
||||
assert.False(t, nilAcc.IsPinnedUser(10))
|
||||
})
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// Enhanced mock for integration tests (supports affinity lookups + tracking)
|
||||
// ===========================================================================
|
||||
|
||||
type affinityIntegrationCache struct {
|
||||
mockAffinityCache
|
||||
getAffinityAccountsFunc func(ctx context.Context, groupID int64, userID int64, clientID string, ttl time.Duration) ([]int64, error)
|
||||
updateAffinityCalls []affinityUpdateCall
|
||||
getMultiCountFunc func(ctx context.Context, groupID int64, accountID int64, targetUserID int64, ttl time.Duration) (int64, int64, int64, error)
|
||||
sessionBindings map[string]int64
|
||||
getAffinityAccountsCalled bool // 标记 GetAffinityAccounts 是否已被调用,用于区分预处理/Layer 2 阶段
|
||||
}
|
||||
|
||||
type affinityUpdateCall struct {
|
||||
groupID int64
|
||||
userID int64
|
||||
clientID string
|
||||
accountID int64
|
||||
beforeAffinityLookup bool // true 表示该调用发生在 GetAffinityAccounts 之前(预处理阶段)
|
||||
}
|
||||
|
||||
func (c *affinityIntegrationCache) GetSessionAccountID(_ context.Context, _ int64, hash string) (int64, error) {
|
||||
if c.sessionBindings != nil {
|
||||
if id, ok := c.sessionBindings[hash]; ok {
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
return 0, errors.New("not found")
|
||||
}
|
||||
|
||||
func (c *affinityIntegrationCache) GetAffinityAccounts(ctx context.Context, groupID int64, userID int64, clientID string, ttl time.Duration) ([]int64, error) {
|
||||
c.getAffinityAccountsCalled = true
|
||||
if c.getAffinityAccountsFunc != nil {
|
||||
return c.getAffinityAccountsFunc(ctx, groupID, userID, clientID, ttl)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *affinityIntegrationCache) UpdateAffinity(_ context.Context, groupID int64, userID int64, clientID string, accountID int64, _ time.Duration) error {
|
||||
c.updateAffinityCalls = append(c.updateAffinityCalls, affinityUpdateCall{
|
||||
groupID: groupID, userID: userID, clientID: clientID, accountID: accountID,
|
||||
beforeAffinityLookup: !c.getAffinityAccountsCalled,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *affinityIntegrationCache) GetAffinityMultiCount(ctx context.Context, groupID int64, accountID int64, targetUserID int64, ttl time.Duration) (int64, int64, int64, error) {
|
||||
if c.getMultiCountFunc != nil {
|
||||
return c.getMultiCountFunc(ctx, groupID, accountID, targetUserID, ttl)
|
||||
}
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// TestAffinityPreprocessPinnedUsers — 验证 pinned_users 预处理逻辑
|
||||
// ===========================================================================
|
||||
|
||||
func TestAffinityPreprocessPinnedUsers(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testUserID := int64(42)
|
||||
testClientID := "user_" + "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2" + "_account_test"
|
||||
|
||||
t.Run("pinned user triggers UpdateAffinity", func(t *testing.T) {
|
||||
cache := &affinityIntegrationCache{}
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"pinned_users": []any{float64(42)},
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
Extra: map[string]any{"affinity_enabled": true},
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{1: true, 2: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
_, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.NoError(t, err)
|
||||
|
||||
// 验证 UpdateAffinity 至少被调用了一次(pinned preprocess for account 1)
|
||||
found := false
|
||||
for _, call := range cache.updateAffinityCalls {
|
||||
if call.accountID == 1 && call.userID == testUserID {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.True(t, found, "UpdateAffinity should be called for pinned account 1 with userID %d", testUserID)
|
||||
})
|
||||
|
||||
t.Run("non-pinned user does not trigger UpdateAffinity preprocess", func(t *testing.T) {
|
||||
cache := &affinityIntegrationCache{
|
||||
getAffinityAccountsFunc: func(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil // 无亲和记录,直接进入 Layer 2
|
||||
},
|
||||
}
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"pinned_users": []any{float64(99)}, // different user
|
||||
},
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{1: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
_, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.NoError(t, err)
|
||||
|
||||
// 验证预处理阶段(beforeAffinityLookup=true)没有为 userID=42 在 account 1 上调用 UpdateAffinity
|
||||
for _, call := range cache.updateAffinityCalls {
|
||||
if call.beforeAffinityLookup && call.accountID == 1 && call.userID == testUserID {
|
||||
t.Errorf("UpdateAffinity should NOT be called in preprocess for non-pinned user %d on account %d", testUserID, call.accountID)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// TestAffinityNoSwitchError — 验证 ErrAffinityNoSwitch 行为
|
||||
// ===========================================================================
|
||||
|
||||
func TestAffinityNoSwitchError(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testUserID := int64(42)
|
||||
testClientID := "user_" + "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2" + "_account_test"
|
||||
futureTime := time.Now().Add(1 * time.Hour)
|
||||
|
||||
t.Run("affinity hit but unschedulable + allow_switch=false returns ErrAffinityNoSwitch", func(t *testing.T) {
|
||||
cache := &affinityIntegrationCache{
|
||||
getAffinityAccountsFunc: func(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return []int64{1}, nil
|
||||
},
|
||||
}
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true,
|
||||
RateLimitResetAt: &futureTime, // rate limited = unschedulable for selection
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_allow_switch": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{2: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
_, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, ErrAffinityNoSwitch)
|
||||
})
|
||||
|
||||
t.Run("affinity hit but unschedulable + allow_switch=true continues to Layer 2", func(t *testing.T) {
|
||||
cache := &affinityIntegrationCache{
|
||||
getAffinityAccountsFunc: func(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return []int64{1}, nil
|
||||
},
|
||||
}
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true,
|
||||
RateLimitResetAt: &futureTime, // rate limited = unschedulable
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_allow_switch": true,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{2: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, int64(2), result.Account.ID, "should fall through to Layer 2 and select account 2")
|
||||
})
|
||||
|
||||
t.Run("no affinity records - not affected by allow_switch", func(t *testing.T) {
|
||||
cache := &affinityIntegrationCache{} // returns nil from GetAffinityAccounts
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_allow_switch": false,
|
||||
},
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{1: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, int64(1), result.Account.ID, "should select normally via Layer 2")
|
||||
})
|
||||
|
||||
t.Run("affinity disabled account ignores allow_switch=false", func(t *testing.T) {
|
||||
cache := &affinityIntegrationCache{
|
||||
getAffinityAccountsFunc: func(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return []int64{1}, nil
|
||||
},
|
||||
}
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": false,
|
||||
"affinity_allow_switch": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2, Platform: PlatformAnthropic, Priority: 2,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{2: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
assert.NotEqual(t, int64(0), result.Account.ID)
|
||||
})
|
||||
|
||||
t.Run("allow_switch=false + model_unsupported returns ErrAffinityNoSwitch", func(t *testing.T) {
|
||||
cache := &affinityIntegrationCache{
|
||||
getAffinityAccountsFunc: func(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return []int64{1}, nil
|
||||
},
|
||||
}
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_allow_switch": false,
|
||||
},
|
||||
// model_mapping 仅包含 claude-3-haiku → 请求 claude-3-5-sonnet 将被拒绝
|
||||
Credentials: map[string]any{
|
||||
"model_mapping": map[string]any{
|
||||
"claude-3-haiku-20240307": "claude-3-haiku-20240307",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{2: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
_, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, ErrAffinityNoSwitch)
|
||||
})
|
||||
|
||||
t.Run("allow_switch=false + red_zone returns ErrAffinityNoSwitch", func(t *testing.T) {
|
||||
cache := &affinityIntegrationCache{
|
||||
getAffinityAccountsFunc: func(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return []int64{1}, nil
|
||||
},
|
||||
getMultiCountFunc: func(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
// userCount=5, clientCount=5, perUserCount=0
|
||||
// 账号 affinity_base=1, affinity_buffer=0 → clientCount(5) > base(1) + buffer(0) → 红区
|
||||
return 5, 5, 0, nil
|
||||
},
|
||||
}
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_allow_switch": false,
|
||||
"affinity_base": 1,
|
||||
"affinity_buffer": 0, // buffer=0 → 超过 base 直接红区
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{2: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
_, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, ErrAffinityNoSwitch)
|
||||
})
|
||||
|
||||
t.Run("one_allow_switch=true overrides another allow_switch=false (一票放行)", func(t *testing.T) {
|
||||
// 账号 1: allow_switch=false + rate limited(不可调度)
|
||||
// 账号 3: allow_switch=true + rate limited(不可调度)
|
||||
// 一票放行:账号 3 允许切换 → 不阻断 → 降级到 Layer 2 选中账号 2
|
||||
cache := &affinityIntegrationCache{
|
||||
getAffinityAccountsFunc: func(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return []int64{1, 3}, nil
|
||||
},
|
||||
}
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true,
|
||||
RateLimitResetAt: &futureTime, // rate limited
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_allow_switch": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
},
|
||||
{
|
||||
ID: 3, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true,
|
||||
RateLimitResetAt: &futureTime, // rate limited
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_allow_switch": true,
|
||||
},
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{2: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, int64(2), result.Account.ID, "一票放行: should fall through to Layer 2")
|
||||
})
|
||||
|
||||
t.Run("excluded affinity account with allow_switch=false returns ErrAffinityNoSwitch", func(t *testing.T) {
|
||||
cache := &affinityIntegrationCache{
|
||||
getAffinityAccountsFunc: func(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return []int64{1}, nil
|
||||
},
|
||||
}
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_allow_switch": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{2: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
excluded := map[int64]struct{}{1: {}}
|
||||
_, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", excluded, testClientID, testUserID)
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, ErrAffinityNoSwitch)
|
||||
})
|
||||
|
||||
t.Run("disabled affinity record does not suppress sticky fallback", func(t *testing.T) {
|
||||
sessionHash := "sticky-disabled-affinity"
|
||||
cache := &affinityIntegrationCache{
|
||||
getAffinityAccountsFunc: func(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return []int64{1}, nil
|
||||
},
|
||||
sessionBindings: map[string]int64{sessionHash: 2},
|
||||
}
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{2: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, sessionHash, "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
assert.Equal(t, int64(2), result.Account.ID)
|
||||
})
|
||||
|
||||
t.Run("historical affinity record with disabled account still falls back to sticky account", func(t *testing.T) {
|
||||
sessionHash := "sticky-disabled-status-affinity"
|
||||
cache := &affinityIntegrationCache{
|
||||
getAffinityAccountsFunc: func(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return []int64{1}, nil
|
||||
},
|
||||
sessionBindings: map[string]int64{sessionHash: 2},
|
||||
}
|
||||
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusDisabled, Schedulable: true, Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2, Platform: PlatformAnthropic, Priority: 1,
|
||||
Status: StatusActive, Schedulable: true, Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
concCache := &mockConcurrencyCache{acquireResults: map[int64]bool{2: true}}
|
||||
concSvc := NewConcurrencyService(concCache)
|
||||
cfg := &config.Config{RunMode: config.RunModeStandard}
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: cache,
|
||||
cfg: cfg,
|
||||
concurrencyService: concSvc,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, sessionHash, "claude-3-5-sonnet-20241022", nil, testClientID, testUserID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
assert.Equal(t, int64(2), result.Account.ID)
|
||||
})
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// TestAffinityMultiDimClassify — 验证 classifyByAffinityZone 与多维度计数
|
||||
// ===========================================================================
|
||||
|
||||
func TestAffinityMultiDimClassify(t *testing.T) {
|
||||
t.Run("multi-dim zone classification with user and client dimensions", func(t *testing.T) {
|
||||
makeAWL := func(id int64, extra map[string]any, count int64) accountWithLoad {
|
||||
return accountWithLoad{
|
||||
account: &Account{ID: id, Platform: PlatformAnthropic, Extra: extra},
|
||||
loadInfo: &AccountLoadInfo{AccountID: id},
|
||||
affinityCount: count,
|
||||
}
|
||||
}
|
||||
|
||||
// Account 1: client green (3 <= 5), classified as green by old classifyByAffinityZone
|
||||
// Account 2: client red (10 > 8), classified as red
|
||||
accs := []accountWithLoad{
|
||||
makeAWL(1, map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_base": 5,
|
||||
"affinity_buffer": 3,
|
||||
}, 3),
|
||||
makeAWL(2, map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_base": 5,
|
||||
"affinity_buffer": 3,
|
||||
}, 10),
|
||||
}
|
||||
|
||||
result := classifyByAffinityZone(accs)
|
||||
require.Len(t, result, 1)
|
||||
assert.Equal(t, int64(1), result[0].account.ID, "only green account should remain")
|
||||
})
|
||||
|
||||
t.Run("all green returns all", func(t *testing.T) {
|
||||
makeAWL := func(id int64, count int64) accountWithLoad {
|
||||
return accountWithLoad{
|
||||
account: &Account{
|
||||
ID: id,
|
||||
Platform: PlatformAnthropic,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": true,
|
||||
"affinity_base": 10,
|
||||
"affinity_buffer": 5,
|
||||
},
|
||||
},
|
||||
loadInfo: &AccountLoadInfo{AccountID: id},
|
||||
affinityCount: count,
|
||||
}
|
||||
}
|
||||
|
||||
accs := []accountWithLoad{
|
||||
makeAWL(1, 3),
|
||||
makeAWL(2, 5),
|
||||
makeAWL(3, 10), // exactly at base
|
||||
}
|
||||
|
||||
result := classifyByAffinityZone(accs)
|
||||
require.Len(t, result, 3)
|
||||
})
|
||||
}
|
||||
@@ -143,12 +143,15 @@ func (s *stickyGatewayCacheHotpathStub) RefreshSessionTTL(ctx context.Context, g
|
||||
func (s *stickyGatewayCacheHotpathStub) DeleteSessionAccountID(ctx context.Context, groupID int64, sessionHash string) error {
|
||||
return nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) GetClientAffinityAccounts(_ context.Context, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
func (s *stickyGatewayCacheHotpathStub) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) UpdateClientAffinity(_ context.Context, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
func (s *stickyGatewayCacheHotpathStub) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
@@ -750,7 +753,7 @@ func TestSelectAccountWithLoadAwareness_StickyReadReuse(t *testing.T) {
|
||||
modelsListCacheTTL: time.Minute,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(baseCtx, nil, "sess-hash", "", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(baseCtx, nil, "sess-hash", "", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -772,7 +775,7 @@ func TestSelectAccountWithLoadAwareness_StickyReadReuse(t *testing.T) {
|
||||
|
||||
ctx := context.WithValue(baseCtx, ctxkey.PrefetchedStickyAccountID, account.ID)
|
||||
ctx = context.WithValue(ctx, ctxkey.PrefetchedStickyGroupID, int64(0))
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sess-hash", "", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sess-hash", "", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -794,7 +797,7 @@ func TestSelectAccountWithLoadAwareness_StickyReadReuse(t *testing.T) {
|
||||
|
||||
ctx := context.WithValue(baseCtx, ctxkey.PrefetchedStickyAccountID, int64(999))
|
||||
ctx = context.WithValue(ctx, ctxkey.PrefetchedStickyGroupID, int64(77))
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sess-hash", "", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sess-hash", "", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
|
||||
@@ -235,12 +235,15 @@ func (m *mockGatewayCacheForPlatform) DeleteSessionAccountID(ctx context.Context
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockGatewayCacheForPlatform) GetClientAffinityAccounts(_ context.Context, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
func (m *mockGatewayCacheForPlatform) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForPlatform) UpdateClientAffinity(_ context.Context, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
func (m *mockGatewayCacheForPlatform) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockGatewayCacheForPlatform) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForPlatform) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
@@ -2050,7 +2053,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: nil, // No concurrency service
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2103,7 +2106,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: nil, // legacy path
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, sessionHash, "claude-b", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, sessionHash, "claude-b", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2135,7 +2138,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: nil,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2167,13 +2170,314 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
}
|
||||
|
||||
excludedIDs := map[int64]struct{}{1: {}}
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", excludedIDs, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", excludedIDs, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
require.Equal(t, int64(2), result.Account.ID, "不应选择被排除的账号")
|
||||
})
|
||||
|
||||
t.Run("无客户端ID时过滤Anthropic OAuth亲和账号-load-aware路径", func(t *testing.T) {
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 1,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"client_affinity_enabled": true,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 2,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
cfg := testConfig()
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: &mockGatewayCacheForPlatform{},
|
||||
cfg: cfg,
|
||||
concurrencyService: NewConcurrencyService(&mockConcurrencyCache{}),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", 0)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
require.Equal(t, int64(2), result.Account.ID, "无客户端ID时应过滤亲和OAuth账号")
|
||||
})
|
||||
|
||||
t.Run("无客户端ID且亲和关闭时-load-aware路径恢复旧逻辑", func(t *testing.T) {
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 1,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"affinity_enabled": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 2,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
cfg := testConfig()
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: &mockGatewayCacheForPlatform{},
|
||||
cfg: cfg,
|
||||
concurrencyService: NewConcurrencyService(&mockConcurrencyCache{}),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", 0)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
require.Equal(t, int64(1), result.Account.ID, "关闭亲和后不应再被无clientID过滤")
|
||||
})
|
||||
|
||||
t.Run("有客户端ID时不过滤Anthropic OAuth亲和账号", func(t *testing.T) {
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 1,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"client_affinity_enabled": true,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 2,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
cfg := testConfig()
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: &mockGatewayCacheForPlatform{},
|
||||
cfg: cfg,
|
||||
concurrencyService: NewConcurrencyService(&mockConcurrencyCache{}),
|
||||
}
|
||||
|
||||
metadataUserID := "user_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef_account_test_session_test"
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, metadataUserID, 123)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
require.Equal(t, int64(1), result.Account.ID, "有客户端ID时应允许亲和OAuth账号参与调度")
|
||||
})
|
||||
|
||||
t.Run("过滤对Anthropic全类型生效-SetupToken也受影响", func(t *testing.T) {
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeSetupToken,
|
||||
Priority: 1,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"client_affinity_enabled": true,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 2,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"client_affinity_enabled": true,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 3,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 3,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
cfg := testConfig()
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = true
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: &mockGatewayCacheForPlatform{},
|
||||
cfg: cfg,
|
||||
concurrencyService: NewConcurrencyService(&mockConcurrencyCache{}),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", 0)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
require.Equal(t, int64(3), result.Account.ID, "无客户端ID时,Anthropic SetupToken/OAuth(开启亲和)都应被过滤")
|
||||
})
|
||||
|
||||
t.Run("无客户端ID时过滤Anthropic OAuth亲和账号-legacy路径", func(t *testing.T) {
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 1,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"client_affinity_enabled": true,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 2,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
cfg := testConfig()
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = false
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: &mockGatewayCacheForPlatform{},
|
||||
cfg: cfg,
|
||||
concurrencyService: nil,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", 0)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
require.Equal(t, int64(2), result.Account.ID, "legacy路径也应过滤亲和OAuth账号")
|
||||
})
|
||||
|
||||
t.Run("无客户端ID且亲和关闭时-legacy路径恢复旧逻辑", func(t *testing.T) {
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
{
|
||||
ID: 1,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 1,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
Extra: map[string]any{
|
||||
"client_affinity_enabled": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Priority: 2,
|
||||
Status: StatusActive,
|
||||
Schedulable: true,
|
||||
Concurrency: 5,
|
||||
},
|
||||
},
|
||||
accountsByID: map[int64]*Account{},
|
||||
}
|
||||
for i := range repo.accounts {
|
||||
repo.accountsByID[repo.accounts[i].ID] = &repo.accounts[i]
|
||||
}
|
||||
|
||||
cfg := testConfig()
|
||||
cfg.Gateway.Scheduling.LoadBatchEnabled = false
|
||||
|
||||
svc := &GatewayService{
|
||||
accountRepo: repo,
|
||||
cache: &mockGatewayCacheForPlatform{},
|
||||
cfg: cfg,
|
||||
concurrencyService: nil,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", 0)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
require.Equal(t, int64(1), result.Account.ID, "关闭亲和后 legacy 路径不应再被无clientID过滤")
|
||||
})
|
||||
|
||||
t.Run("粘性命中-不调用GetByID", func(t *testing.T) {
|
||||
repo := &mockAccountRepoForPlatform{
|
||||
accounts: []Account{
|
||||
@@ -2201,7 +2505,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sticky", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sticky", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2237,7 +2541,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sticky", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sticky", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2278,7 +2582,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(testCtx, nil, "sticky", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(testCtx, nil, "sticky", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2306,7 +2610,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: nil,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.Error(t, err)
|
||||
require.Nil(t, result)
|
||||
require.ErrorIs(t, err, ErrNoAvailableAccounts)
|
||||
@@ -2338,7 +2642,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: nil,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2371,7 +2675,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: nil,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2409,7 +2713,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sticky", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "sticky", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.WaitPlan)
|
||||
@@ -2445,7 +2749,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "legacy", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "legacy", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2504,7 +2808,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, sessionHash, "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, sessionHash, "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.WaitPlan)
|
||||
@@ -2558,7 +2862,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, sessionHash, "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, sessionHash, "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2612,7 +2916,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, sessionHash, "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, sessionHash, "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2670,7 +2974,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "route", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "route", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2728,7 +3032,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "route-full", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "route-full", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.WaitPlan)
|
||||
@@ -2786,7 +3090,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "fallback", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "fallback", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2823,7 +3127,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.WaitPlan)
|
||||
@@ -2875,7 +3179,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "gemini", "gemini-2.5-pro", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "gemini", "gemini-2.5-pro", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -2953,7 +3257,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
}
|
||||
|
||||
excluded := map[int64]struct{}{1: {}}
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "", "claude-3-5-sonnet-20241022", excluded, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "", "claude-3-5-sonnet-20241022", excluded, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -3007,7 +3311,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: nil,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "", "gemini-2.5-pro", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "", "gemini-2.5-pro", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
@@ -3040,7 +3344,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: nil,
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, &groupID, "", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.Error(t, err)
|
||||
require.Nil(t, result)
|
||||
require.ErrorIs(t, err, ErrClaudeCodeOnly)
|
||||
@@ -3078,7 +3382,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "wait", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "wait", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.WaitPlan)
|
||||
@@ -3116,7 +3420,7 @@ func TestGatewayService_SelectAccountWithLoadAwareness(t *testing.T) {
|
||||
concurrencyService: NewConcurrencyService(concurrencyCache),
|
||||
}
|
||||
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "missing-load", "claude-3-5-sonnet-20241022", nil, "")
|
||||
result, err := svc.SelectAccountWithLoadAwareness(ctx, nil, "missing-load", "claude-3-5-sonnet-20241022", nil, "", int64(0))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.Account)
|
||||
|
||||
@@ -364,6 +364,12 @@ var ErrNoAvailableAccounts = errors.New("no available accounts")
|
||||
// ErrClaudeCodeOnly 表示分组仅允许 Claude Code 客户端访问
|
||||
var ErrClaudeCodeOnly = errors.New("this group only allows Claude Code clients")
|
||||
|
||||
// ErrAffinityNoSwitch 表示亲和账号不可用且不允许切换到其他账号
|
||||
var ErrAffinityNoSwitch = errors.New("affinity account unavailable and switching is disabled")
|
||||
|
||||
// ErrAffinityLimitExceeded 表示亲和客户端限制已达上限
|
||||
var ErrAffinityLimitExceeded = errors.New("affinity client limit exceeded")
|
||||
|
||||
// allowedHeaders 白名单headers(参考CRS项目)
|
||||
var allowedHeaders = map[string]bool{
|
||||
"accept": true,
|
||||
@@ -405,24 +411,29 @@ type GatewayCache interface {
|
||||
// Delete sticky session binding, used to proactively clean up when account becomes unavailable
|
||||
DeleteSessionAccountID(ctx context.Context, groupID int64, sessionHash string) error
|
||||
|
||||
// GetClientAffinityAccounts 获取客户端亲和账号列表(按最近使用降序),同时清理过期成员
|
||||
GetClientAffinityAccounts(ctx context.Context, groupID int64, clientID string, ttl time.Duration) ([]int64, error)
|
||||
// UpdateClientAffinity 添加/更新客户端亲和关系(更新 score 为当前时间戳,刷新 key TTL)
|
||||
UpdateClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64, ttl time.Duration) error
|
||||
// GetAccountAffinityCountBatch 批量获取账号的亲和客户端数量(惰性清理过期成员)
|
||||
// GetAffinityAccounts 获取亲和账号列表(按最近使用降序),同时清理过期成员
|
||||
GetAffinityAccounts(ctx context.Context, groupID int64, userID int64, clientID string, ttl time.Duration) ([]int64, error)
|
||||
// UpdateAffinity 添加/更新亲和关系(更新 score 为当前时间戳,刷新 key TTL)
|
||||
UpdateAffinity(ctx context.Context, groupID int64, userID int64, clientID string, accountID int64, ttl time.Duration) error
|
||||
// GetAccountAffinityCountBatch 批量获取账号的亲和成员数量(惰性清理过期成员)
|
||||
GetAccountAffinityCountBatch(ctx context.Context, groupID int64, accountIDs []int64, ttl time.Duration) (map[int64]int64, error)
|
||||
// GetAccountAffinityClientsBatch 批量获取每个账号跨所有分组的亲和客户端列表(去重)
|
||||
// GetAccountAffinityClientsBatch 批量获取每个账号跨所有分组的亲和成员列表(去重)
|
||||
// accountGroups: map[accountID][]groupID
|
||||
// 返回值成员格式为 {userID}/{clientID}
|
||||
GetAccountAffinityClientsBatch(ctx context.Context, accountGroups map[int64][]int64, ttl time.Duration) (map[int64][]string, error)
|
||||
// GetAccountAffinityClientsWithScores 获取单个账号跨所有分组的亲和客户端列表(含最后活跃时间)
|
||||
GetAccountAffinityClientsWithScores(ctx context.Context, accountID int64, groupIDs []int64, ttl time.Duration) ([]AffinityClient, error)
|
||||
// ClearAccountAffinity 清除指定账号在所有分组的亲和记录(正向+反向索引)
|
||||
// 用于账号关闭客户端亲和时立即清理旧绑定
|
||||
// 用于账号关闭亲和时立即清理旧绑定
|
||||
ClearAccountAffinity(ctx context.Context, accountID int64, groupIDs []int64) error
|
||||
// GetAffinityMultiCount 获取账号的多维度亲和计数
|
||||
// 返回: uniqueUsers, uniqueClients, perUserClients
|
||||
GetAffinityMultiCount(ctx context.Context, groupID int64, accountID int64, targetUserID int64, ttl time.Duration) (users, clients, perUser int64, err error)
|
||||
}
|
||||
|
||||
// AffinityClient 亲和客户端信息(含最后活跃时间)
|
||||
// AffinityClient 亲和客户端信息(含用户 ID 和最后活跃时间)
|
||||
type AffinityClient struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
ClientID string `json:"client_id"`
|
||||
LastActive time.Time `json:"last_active"`
|
||||
}
|
||||
@@ -1148,8 +1159,10 @@ func (s *GatewayService) SelectAccountForModelWithExclusions(ctx context.Context
|
||||
}
|
||||
|
||||
// SelectAccountWithLoadAwareness selects account with load-awareness and wait plan.
|
||||
// 调度流程文档见 docs/ACCOUNT_SCHEDULING_FLOW.md 。
|
||||
// metadataUserID: 用于客户端亲和调度,从中提取客户端 ID
|
||||
func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, groupID *int64, sessionHash string, requestedModel string, excludedIDs map[int64]struct{}, metadataUserID string) (*AccountSelectionResult, error) {
|
||||
// sub2apiUserID: 系统用户 ID,用于二维亲和调度
|
||||
func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, groupID *int64, sessionHash string, requestedModel string, excludedIDs map[int64]struct{}, metadataUserID string, sub2apiUserID int64) (*AccountSelectionResult, error) {
|
||||
// 调试日志:记录调度入口参数
|
||||
excludedIDsList := make([]int64, 0, len(excludedIDs))
|
||||
for id := range excludedIDs {
|
||||
@@ -1181,6 +1194,7 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
|
||||
// 提取客户端 ID(用于客户端亲和调度)
|
||||
affinityClientID := extractClientIDFromMetadata(metadataUserID)
|
||||
affinityUserID := sub2apiUserID
|
||||
|
||||
if s.debugModelRoutingEnabled() && requestedModel != "" {
|
||||
groupPlatform := ""
|
||||
@@ -1203,6 +1217,10 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if shouldFilterAccountWithoutClientID(account, affinityClientID) {
|
||||
localExcluded[account.ID] = struct{}{}
|
||||
continue
|
||||
}
|
||||
|
||||
result, err := s.tryAcquireAccountSlot(ctx, account.ID, account.Concurrency)
|
||||
if err == nil && result.Acquired {
|
||||
@@ -1264,12 +1282,18 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
accounts = filterAccountsWithoutClientID(accounts, affinityClientID)
|
||||
if len(accounts) == 0 {
|
||||
return nil, ErrNoAvailableAccounts
|
||||
}
|
||||
ctx = s.withWindowCostPrefetch(ctx, accounts)
|
||||
ctx = s.withRPMPrefetch(ctx, accounts)
|
||||
|
||||
// 提前构建 accountByID(供 Layer 1 和 Layer 1.5 使用)
|
||||
accountByID := make(map[int64]*Account, len(accounts))
|
||||
for i := range accounts {
|
||||
accountByID[accounts[i].ID] = &accounts[i]
|
||||
}
|
||||
isExcluded := func(accountID int64) bool {
|
||||
if excludedIDs == nil {
|
||||
return false
|
||||
@@ -1277,12 +1301,19 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
_, excluded := excludedIDs[accountID]
|
||||
return excluded
|
||||
}
|
||||
|
||||
// 提前构建 accountByID(供 Layer 1 和 Layer 1.5 使用)
|
||||
accountByID := make(map[int64]*Account, len(accounts))
|
||||
for i := range accounts {
|
||||
accountByID[accounts[i].ID] = &accounts[i]
|
||||
}
|
||||
affinityFlow := newGatewayAffinityFlow(
|
||||
s,
|
||||
ctx,
|
||||
groupID,
|
||||
sessionHash,
|
||||
requestedModel,
|
||||
affinityClientID,
|
||||
affinityUserID,
|
||||
platform,
|
||||
useMixed,
|
||||
accountByID,
|
||||
isExcluded,
|
||||
)
|
||||
|
||||
// 获取模型路由配置(仅 anthropic 平台)
|
||||
var routingAccountIDs []int64
|
||||
@@ -1485,8 +1516,8 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
if sessionHash != "" && s.cache != nil {
|
||||
_ = s.cache.SetSessionAccountID(ctx, derefGroupID(groupID), sessionHash, item.account.ID, stickySessionTTL)
|
||||
}
|
||||
if affinityClientID != "" && s.cache != nil && item.account.IsClientAffinityEnabled() {
|
||||
_ = s.cache.UpdateClientAffinity(ctx, derefGroupID(groupID), affinityClientID, item.account.ID, ClientAffinityTTL)
|
||||
if affinityClientID != "" && affinityUserID > 0 && s.cache != nil && item.account.IsAffinityEnabled() {
|
||||
_ = s.cache.UpdateAffinity(ctx, derefGroupID(groupID), affinityUserID, affinityClientID, item.account.ID, ClientAffinityTTL)
|
||||
}
|
||||
if s.debugModelRoutingEnabled() {
|
||||
logger.LegacyPrintf("service.gateway", "[ModelRoutingDebug] routed select: group_id=%v model=%s session=%s account=%d", derefGroupID(groupID), requestedModel, shortSessionHash(sessionHash), item.account.ID)
|
||||
@@ -1525,14 +1556,27 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Layer 1.5: 粘性会话(仅在无模型路由配置时生效) ============
|
||||
if len(routingAccountIDs) == 0 && sessionHash != "" && stickyAccountID > 0 && !isExcluded(stickyAccountID) {
|
||||
// ============ Layer 1.3: 用户亲和预处理(pinned_users 自动注入) ============
|
||||
affinityFlow.preprocessPinnedUsers(accounts)
|
||||
|
||||
// ============ Layer 1.4: 客户端亲和调度(优先于粘性会话) ============
|
||||
affinityHit := false
|
||||
if affinityResult, hit, err := affinityFlow.trySelectAffinityAccount(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
affinityHit = hit
|
||||
if affinityResult != nil {
|
||||
return affinityResult, nil
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Layer 1.5: 粘性会话(仅在无模型路由配置 且 亲和未命中时生效) ============
|
||||
if !affinityHit && len(routingAccountIDs) == 0 && sessionHash != "" && stickyAccountID > 0 && !isExcluded(stickyAccountID) {
|
||||
accountID := stickyAccountID
|
||||
if accountID > 0 && !isExcluded(accountID) {
|
||||
account, ok := accountByID[accountID]
|
||||
if ok {
|
||||
// 检查账户是否需要清理粘性会话绑定
|
||||
// Check if the account needs sticky session cleanup
|
||||
clearSticky := shouldClearStickySession(account, requestedModel)
|
||||
if clearSticky {
|
||||
_ = s.cache.DeleteSessionAccountID(ctx, derefGroupID(groupID), sessionHash)
|
||||
@@ -1548,7 +1592,6 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
result, err := s.tryAcquireAccountSlot(ctx, accountID, account.Concurrency)
|
||||
if err == nil && result.Acquired {
|
||||
// 会话数量限制检查
|
||||
// Session count limit check
|
||||
if !s.checkAndRegisterSession(ctx, account, sessionHash) {
|
||||
result.ReleaseFunc() // 释放槽位,继续到 Layer 2
|
||||
} else {
|
||||
@@ -1563,10 +1606,8 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
waitingCount, _ := s.concurrencyService.GetAccountWaitingCount(ctx, accountID)
|
||||
if waitingCount < cfg.StickySessionMaxWaiting {
|
||||
// 会话数量限制检查(等待计划也需要占用会话配额)
|
||||
// Session count limit check (wait plan also requires session quota)
|
||||
if !s.checkAndRegisterSession(ctx, account, sessionHash) {
|
||||
// 会话限制已满,继续到 Layer 2
|
||||
// Session limit full, continue to Layer 2
|
||||
} else {
|
||||
return &AccountSelectionResult{
|
||||
Account: account,
|
||||
@@ -1584,76 +1625,6 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Layer 1.6: 客户端亲和(仅在粘性会话未命中时生效) ============
|
||||
if affinityClientID != "" && s.cache != nil && stickyAccountID <= 0 {
|
||||
affinityAccountIDs, err := s.cache.GetClientAffinityAccounts(ctx, derefGroupID(groupID), affinityClientID, ClientAffinityTTL)
|
||||
if err == nil && len(affinityAccountIDs) > 0 {
|
||||
for _, affinityAccID := range affinityAccountIDs {
|
||||
if isExcluded(affinityAccID) {
|
||||
continue
|
||||
}
|
||||
account, ok := accountByID[affinityAccID]
|
||||
if !ok || !s.isAccountSchedulableForSelection(account) {
|
||||
continue
|
||||
}
|
||||
if !account.IsClientAffinityEnabled() {
|
||||
continue
|
||||
}
|
||||
if !s.isAccountAllowedForPlatform(account, platform, useMixed) {
|
||||
continue
|
||||
}
|
||||
if requestedModel != "" && !s.isModelSupportedByAccountWithContext(ctx, account, requestedModel) {
|
||||
continue
|
||||
}
|
||||
if !s.isAccountSchedulableForModelSelection(ctx, account, requestedModel) {
|
||||
continue
|
||||
}
|
||||
if !s.isAccountSchedulableForQuota(account) {
|
||||
continue
|
||||
}
|
||||
if !s.isAccountSchedulableForWindowCost(ctx, account, false) {
|
||||
continue
|
||||
}
|
||||
if !s.isAccountSchedulableForRPM(ctx, account, false) {
|
||||
continue
|
||||
}
|
||||
// 亲和三区检查:红区账号不可通过亲和命中调度
|
||||
if account.GetAffinityBase() > 0 && s.cache != nil {
|
||||
countMap, err := s.cache.GetAccountAffinityCountBatch(ctx, derefGroupID(groupID), []int64{affinityAccID}, ClientAffinityTTL)
|
||||
if err == nil {
|
||||
zone := account.GetAffinityZone(countMap[affinityAccID])
|
||||
if zone == AffinityZoneRed {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result, err := s.tryAcquireAccountSlot(ctx, affinityAccID, account.Concurrency)
|
||||
if err == nil && result.Acquired {
|
||||
if !s.checkAndRegisterSession(ctx, account, sessionHash) {
|
||||
result.ReleaseFunc()
|
||||
continue
|
||||
}
|
||||
// 亲和命中:更新亲和 score + 绑定粘性会话
|
||||
_ = s.cache.UpdateClientAffinity(ctx, derefGroupID(groupID), affinityClientID, affinityAccID, ClientAffinityTTL)
|
||||
if sessionHash != "" {
|
||||
_ = s.cache.SetSessionAccountID(ctx, derefGroupID(groupID), sessionHash, affinityAccID, stickySessionTTL)
|
||||
}
|
||||
slog.Debug("client_affinity_hit",
|
||||
"group_id", derefGroupID(groupID),
|
||||
"client_id", affinityClientID[:8]+"...",
|
||||
"account_id", affinityAccID)
|
||||
return &AccountSelectionResult{
|
||||
Account: account,
|
||||
Acquired: true,
|
||||
ReleaseFunc: result.ReleaseFunc,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
// 所有亲和账号不可用,继续到 Layer 2
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Layer 2: 负载感知选择 ============
|
||||
candidates := make([]*Account, 0, len(accounts))
|
||||
for i := range accounts {
|
||||
@@ -1706,8 +1677,8 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
loadMap, err := s.concurrencyService.GetAccountsLoadBatch(ctx, accountLoads)
|
||||
if err != nil {
|
||||
if result, ok := s.tryAcquireByLegacyOrder(ctx, candidates, groupID, sessionHash, preferOAuth); ok {
|
||||
if affinityClientID != "" && s.cache != nil && result.Account != nil && result.Account.IsClientAffinityEnabled() {
|
||||
_ = s.cache.UpdateClientAffinity(ctx, derefGroupID(groupID), affinityClientID, result.Account.ID, ClientAffinityTTL)
|
||||
if affinityClientID != "" && affinityUserID > 0 && s.cache != nil && result.Account != nil && result.Account.IsAffinityEnabled() {
|
||||
_ = s.cache.UpdateAffinity(ctx, derefGroupID(groupID), affinityUserID, affinityClientID, result.Account.ID, ClientAffinityTTL)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -1771,9 +1742,9 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
if sessionHash != "" && s.cache != nil {
|
||||
_ = s.cache.SetSessionAccountID(ctx, derefGroupID(groupID), sessionHash, selected.account.ID, stickySessionTTL)
|
||||
}
|
||||
// 更新客户端亲和关系
|
||||
if affinityClientID != "" && s.cache != nil && selected.account.IsClientAffinityEnabled() {
|
||||
_ = s.cache.UpdateClientAffinity(ctx, derefGroupID(groupID), affinityClientID, selected.account.ID, ClientAffinityTTL)
|
||||
// 更新亲和关系
|
||||
if affinityClientID != "" && affinityUserID > 0 && s.cache != nil && selected.account.IsAffinityEnabled() {
|
||||
_ = s.cache.UpdateAffinity(ctx, derefGroupID(groupID), affinityUserID, affinityClientID, selected.account.ID, ClientAffinityTTL)
|
||||
}
|
||||
return &AccountSelectionResult{
|
||||
Account: selected.account,
|
||||
@@ -2541,7 +2512,7 @@ func (s *GatewayService) populateAffinityCounts(ctx context.Context, accounts []
|
||||
// 快速检查:是否有任何账号开启了亲和
|
||||
hasAffinity := false
|
||||
for _, acc := range accounts {
|
||||
if acc.account.IsClientAffinityEnabled() {
|
||||
if acc.account.IsAffinityEnabled() {
|
||||
hasAffinity = true
|
||||
break
|
||||
}
|
||||
@@ -2632,7 +2603,7 @@ func classifyByAffinityZone(accounts []accountWithLoad) []accountWithLoad {
|
||||
// 快速检查:是否有任何账号配置了 affinity_base
|
||||
hasZoneConfig := false
|
||||
for _, acc := range accounts {
|
||||
if acc.account.IsClientAffinityEnabled() && acc.account.GetAffinityBase() > 0 {
|
||||
if acc.account.IsAffinityEnabled() && acc.account.GetAffinityBase() > 0 {
|
||||
hasZoneConfig = true
|
||||
break
|
||||
}
|
||||
|
||||
@@ -288,12 +288,15 @@ func (m *mockGatewayCacheForGemini) DeleteSessionAccountID(ctx context.Context,
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockGatewayCacheForGemini) GetClientAffinityAccounts(_ context.Context, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
func (m *mockGatewayCacheForGemini) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForGemini) UpdateClientAffinity(_ context.Context, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
func (m *mockGatewayCacheForGemini) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockGatewayCacheForGemini) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForGemini) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
|
||||
@@ -282,12 +282,15 @@ func (c *stubGatewayCache) DeleteSessionAccountID(ctx context.Context, groupID i
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *stubGatewayCache) GetClientAffinityAccounts(_ context.Context, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
func (c *stubGatewayCache) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (c *stubGatewayCache) UpdateClientAffinity(_ context.Context, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
func (c *stubGatewayCache) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (c *stubGatewayCache) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (c *stubGatewayCache) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
|
||||
@@ -193,12 +193,15 @@ func (c *openAIWSStateStoreTimeoutProbeCache) DeleteSessionAccountID(ctx context
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) GetClientAffinityAccounts(_ context.Context, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) UpdateClientAffinity(_ context.Context, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
|
||||
@@ -519,7 +519,7 @@ func (s *OpsService) selectAccountForRetry(ctx context.Context, reqType opsRetry
|
||||
if s.gatewayService == nil {
|
||||
return nil, fmt.Errorf("gateway service not available")
|
||||
}
|
||||
return s.gatewayService.SelectAccountWithLoadAwareness(ctx, groupID, "", model, excludedIDs, "") // 重试不使用会话限制
|
||||
return s.gatewayService.SelectAccountWithLoadAwareness(ctx, groupID, "", model, excludedIDs, "", int64(0)) // 重试不使用会话限制
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported retry type: %s", reqType)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user