mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-22 15:44:46 +08:00
Merge branch 'develop' into release/custom-0.1.81
This commit is contained in:
2
.github/workflows/backend-ci.yml
vendored
2
.github/workflows/backend-ci.yml
vendored
@@ -17,6 +17,7 @@ jobs:
|
|||||||
go-version-file: backend/go.mod
|
go-version-file: backend/go.mod
|
||||||
check-latest: false
|
check-latest: false
|
||||||
cache: true
|
cache: true
|
||||||
|
cache-dependency-path: backend/go.sum
|
||||||
- name: Verify Go version
|
- name: Verify Go version
|
||||||
run: |
|
run: |
|
||||||
go version | grep -q 'go1.25.7'
|
go version | grep -q 'go1.25.7'
|
||||||
@@ -36,6 +37,7 @@ jobs:
|
|||||||
go-version-file: backend/go.mod
|
go-version-file: backend/go.mod
|
||||||
check-latest: false
|
check-latest: false
|
||||||
cache: true
|
cache: true
|
||||||
|
cache-dependency-path: backend/go.sum
|
||||||
- name: Verify Go version
|
- name: Verify Go version
|
||||||
run: |
|
run: |
|
||||||
go version | grep -q 'go1.25.7'
|
go version | grep -q 'go1.25.7'
|
||||||
|
|||||||
9
.gitignore
vendored
9
.gitignore
vendored
@@ -78,6 +78,7 @@ Desktop.ini
|
|||||||
# ===================
|
# ===================
|
||||||
tmp/
|
tmp/
|
||||||
temp/
|
temp/
|
||||||
|
logs/
|
||||||
*.tmp
|
*.tmp
|
||||||
*.temp
|
*.temp
|
||||||
*.log
|
*.log
|
||||||
@@ -130,3 +131,11 @@ deploy/docker-compose.override.yml
|
|||||||
vite.config.js
|
vite.config.js
|
||||||
docs/*
|
docs/*
|
||||||
.serena/
|
.serena/
|
||||||
|
|
||||||
|
# ===================
|
||||||
|
# 压测工具
|
||||||
|
# ===================
|
||||||
|
tools/loadtest/
|
||||||
|
# Antigravity Manager
|
||||||
|
Antigravity-Manager/
|
||||||
|
antigravity_projectid_fix.patch
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
0.1.76
|
0.1.80.1
|
||||||
|
|||||||
@@ -883,6 +883,7 @@ func setDefaults() {
|
|||||||
viper.SetDefault("gateway.max_account_switches", 10)
|
viper.SetDefault("gateway.max_account_switches", 10)
|
||||||
viper.SetDefault("gateway.max_account_switches_gemini", 3)
|
viper.SetDefault("gateway.max_account_switches_gemini", 3)
|
||||||
viper.SetDefault("gateway.antigravity_fallback_cooldown_minutes", 1)
|
viper.SetDefault("gateway.antigravity_fallback_cooldown_minutes", 1)
|
||||||
|
viper.SetDefault("gateway.antigravity_extra_retries", 10)
|
||||||
viper.SetDefault("gateway.max_body_size", int64(100*1024*1024))
|
viper.SetDefault("gateway.max_body_size", int64(100*1024*1024))
|
||||||
viper.SetDefault("gateway.connection_pool_isolation", ConnectionPoolIsolationAccountProxy)
|
viper.SetDefault("gateway.connection_pool_isolation", ConnectionPoolIsolationAccountProxy)
|
||||||
// HTTP 上游连接池配置(针对 5000+ 并发用户优化)
|
// HTTP 上游连接池配置(针对 5000+ 并发用户优化)
|
||||||
|
|||||||
@@ -65,6 +65,10 @@ func (h *OpsHandler) GetConcurrencyStats(c *gin.Context) {
|
|||||||
|
|
||||||
// GetUserConcurrencyStats returns real-time concurrency usage for all active users.
|
// GetUserConcurrencyStats returns real-time concurrency usage for all active users.
|
||||||
// GET /api/v1/admin/ops/user-concurrency
|
// GET /api/v1/admin/ops/user-concurrency
|
||||||
|
//
|
||||||
|
// Query params:
|
||||||
|
// - platform: optional, filter users by allowed platform
|
||||||
|
// - group_id: optional, filter users by allowed group
|
||||||
func (h *OpsHandler) GetUserConcurrencyStats(c *gin.Context) {
|
func (h *OpsHandler) GetUserConcurrencyStats(c *gin.Context) {
|
||||||
if h.opsService == nil {
|
if h.opsService == nil {
|
||||||
response.Error(c, http.StatusServiceUnavailable, "Ops service not available")
|
response.Error(c, http.StatusServiceUnavailable, "Ops service not available")
|
||||||
@@ -84,7 +88,18 @@ func (h *OpsHandler) GetUserConcurrencyStats(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
users, collectedAt, err := h.opsService.GetUserConcurrencyStats(c.Request.Context())
|
platformFilter := strings.TrimSpace(c.Query("platform"))
|
||||||
|
var groupID *int64
|
||||||
|
if v := strings.TrimSpace(c.Query("group_id")); v != "" {
|
||||||
|
id, err := strconv.ParseInt(v, 10, 64)
|
||||||
|
if err != nil || id <= 0 {
|
||||||
|
response.BadRequest(c, "Invalid group_id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
groupID = &id
|
||||||
|
}
|
||||||
|
|
||||||
|
users, collectedAt, err := h.opsService.GetUserConcurrencyStats(c.Request.Context(), platformFilter, groupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.ErrorFrom(c, err)
|
response.ErrorFrom(c, err)
|
||||||
return
|
return
|
||||||
|
|||||||
160
backend/internal/handler/failover_loop.go
Normal file
160
backend/internal/handler/failover_loop.go
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TempUnscheduler 用于 HandleFailoverError 中同账号重试耗尽后的临时封禁。
|
||||||
|
// GatewayService 隐式实现此接口。
|
||||||
|
type TempUnscheduler interface {
|
||||||
|
TempUnscheduleRetryableError(ctx context.Context, accountID int64, failoverErr *service.UpstreamFailoverError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FailoverAction 表示 failover 错误处理后的下一步动作
|
||||||
|
type FailoverAction int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// FailoverContinue 继续循环(同账号重试或切换账号,调用方统一 continue)
|
||||||
|
FailoverContinue FailoverAction = iota
|
||||||
|
// FailoverExhausted 切换次数耗尽(调用方应返回错误响应)
|
||||||
|
FailoverExhausted
|
||||||
|
// FailoverCanceled context 已取消(调用方应直接 return)
|
||||||
|
FailoverCanceled
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// maxSameAccountRetries 同账号重试次数上限(针对 RetryableOnSameAccount 错误)
|
||||||
|
maxSameAccountRetries = 2
|
||||||
|
// sameAccountRetryDelay 同账号重试间隔
|
||||||
|
sameAccountRetryDelay = 500 * time.Millisecond
|
||||||
|
// singleAccountBackoffDelay 单账号分组 503 退避重试固定延时。
|
||||||
|
// Service 层在 SingleAccountRetry 模式下已做充分原地重试(最多 3 次、总等待 30s),
|
||||||
|
// Handler 层只需短暂间隔后重新进入 Service 层即可。
|
||||||
|
singleAccountBackoffDelay = 2 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
// FailoverState 跨循环迭代共享的 failover 状态
|
||||||
|
type FailoverState struct {
|
||||||
|
SwitchCount int
|
||||||
|
MaxSwitches int
|
||||||
|
FailedAccountIDs map[int64]struct{}
|
||||||
|
SameAccountRetryCount map[int64]int
|
||||||
|
LastFailoverErr *service.UpstreamFailoverError
|
||||||
|
ForceCacheBilling bool
|
||||||
|
hasBoundSession bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFailoverState 创建 failover 状态
|
||||||
|
func NewFailoverState(maxSwitches int, hasBoundSession bool) *FailoverState {
|
||||||
|
return &FailoverState{
|
||||||
|
MaxSwitches: maxSwitches,
|
||||||
|
FailedAccountIDs: make(map[int64]struct{}),
|
||||||
|
SameAccountRetryCount: make(map[int64]int),
|
||||||
|
hasBoundSession: hasBoundSession,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleFailoverError 处理 UpstreamFailoverError,返回下一步动作。
|
||||||
|
// 包含:缓存计费判断、同账号重试、临时封禁、切换计数、Antigravity 延时。
|
||||||
|
func (s *FailoverState) HandleFailoverError(
|
||||||
|
ctx context.Context,
|
||||||
|
gatewayService TempUnscheduler,
|
||||||
|
accountID int64,
|
||||||
|
platform string,
|
||||||
|
failoverErr *service.UpstreamFailoverError,
|
||||||
|
) FailoverAction {
|
||||||
|
s.LastFailoverErr = failoverErr
|
||||||
|
|
||||||
|
// 缓存计费判断
|
||||||
|
if needForceCacheBilling(s.hasBoundSession, failoverErr) {
|
||||||
|
s.ForceCacheBilling = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// 同账号重试:对 RetryableOnSameAccount 的临时性错误,先在同一账号上重试
|
||||||
|
if failoverErr.RetryableOnSameAccount && s.SameAccountRetryCount[accountID] < maxSameAccountRetries {
|
||||||
|
s.SameAccountRetryCount[accountID]++
|
||||||
|
log.Printf("Account %d: retryable error %d, same-account retry %d/%d",
|
||||||
|
accountID, failoverErr.StatusCode, s.SameAccountRetryCount[accountID], maxSameAccountRetries)
|
||||||
|
if !sleepWithContext(ctx, sameAccountRetryDelay) {
|
||||||
|
return FailoverCanceled
|
||||||
|
}
|
||||||
|
return FailoverContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 同账号重试用尽,执行临时封禁
|
||||||
|
if failoverErr.RetryableOnSameAccount {
|
||||||
|
gatewayService.TempUnscheduleRetryableError(ctx, accountID, failoverErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加入失败列表
|
||||||
|
s.FailedAccountIDs[accountID] = struct{}{}
|
||||||
|
|
||||||
|
// 检查是否耗尽
|
||||||
|
if s.SwitchCount >= s.MaxSwitches {
|
||||||
|
return FailoverExhausted
|
||||||
|
}
|
||||||
|
|
||||||
|
// 递增切换计数
|
||||||
|
s.SwitchCount++
|
||||||
|
log.Printf("Account %d: upstream error %d, switching account %d/%d",
|
||||||
|
accountID, failoverErr.StatusCode, s.SwitchCount, s.MaxSwitches)
|
||||||
|
|
||||||
|
// Antigravity 平台换号线性递增延时
|
||||||
|
if platform == service.PlatformAntigravity {
|
||||||
|
delay := time.Duration(s.SwitchCount-1) * time.Second
|
||||||
|
if !sleepWithContext(ctx, delay) {
|
||||||
|
return FailoverCanceled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return FailoverContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleSelectionExhausted 处理选号失败(所有候选账号都在排除列表中)时的退避重试决策。
|
||||||
|
// 针对 Antigravity 单账号分组的 503 (MODEL_CAPACITY_EXHAUSTED) 场景:
|
||||||
|
// 清除排除列表、等待退避后重新选号。
|
||||||
|
//
|
||||||
|
// 返回 FailoverContinue 时,调用方应设置 SingleAccountRetry context 并 continue。
|
||||||
|
// 返回 FailoverExhausted 时,调用方应返回错误响应。
|
||||||
|
// 返回 FailoverCanceled 时,调用方应直接 return。
|
||||||
|
func (s *FailoverState) HandleSelectionExhausted(ctx context.Context) FailoverAction {
|
||||||
|
if s.LastFailoverErr != nil &&
|
||||||
|
s.LastFailoverErr.StatusCode == http.StatusServiceUnavailable &&
|
||||||
|
s.SwitchCount <= s.MaxSwitches {
|
||||||
|
|
||||||
|
log.Printf("Antigravity single-account 503 backoff: waiting %v before retry (attempt %d)",
|
||||||
|
singleAccountBackoffDelay, s.SwitchCount)
|
||||||
|
if !sleepWithContext(ctx, singleAccountBackoffDelay) {
|
||||||
|
return FailoverCanceled
|
||||||
|
}
|
||||||
|
log.Printf("Antigravity single-account 503 retry: clearing failed accounts, retry %d/%d",
|
||||||
|
s.SwitchCount, s.MaxSwitches)
|
||||||
|
s.FailedAccountIDs = make(map[int64]struct{})
|
||||||
|
return FailoverContinue
|
||||||
|
}
|
||||||
|
return FailoverExhausted
|
||||||
|
}
|
||||||
|
|
||||||
|
// needForceCacheBilling 判断 failover 时是否需要强制缓存计费。
|
||||||
|
// 粘性会话切换账号、或上游明确标记时,将 input_tokens 转为 cache_read 计费。
|
||||||
|
func needForceCacheBilling(hasBoundSession bool, failoverErr *service.UpstreamFailoverError) bool {
|
||||||
|
return hasBoundSession || (failoverErr != nil && failoverErr.ForceCacheBilling)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sleepWithContext 等待指定时长,返回 false 表示 context 已取消。
|
||||||
|
func sleepWithContext(ctx context.Context, d time.Duration) bool {
|
||||||
|
if d <= 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return false
|
||||||
|
case <-time.After(d):
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
732
backend/internal/handler/failover_loop_test.go
Normal file
732
backend/internal/handler/failover_loop_test.go
Normal file
@@ -0,0 +1,732 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Mock
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// mockTempUnscheduler 记录 TempUnscheduleRetryableError 的调用信息。
|
||||||
|
type mockTempUnscheduler struct {
|
||||||
|
calls []tempUnscheduleCall
|
||||||
|
}
|
||||||
|
|
||||||
|
type tempUnscheduleCall struct {
|
||||||
|
accountID int64
|
||||||
|
failoverErr *service.UpstreamFailoverError
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockTempUnscheduler) TempUnscheduleRetryableError(_ context.Context, accountID int64, failoverErr *service.UpstreamFailoverError) {
|
||||||
|
m.calls = append(m.calls, tempUnscheduleCall{accountID: accountID, failoverErr: failoverErr})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Helper
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func newTestFailoverErr(statusCode int, retryable, forceBilling bool) *service.UpstreamFailoverError {
|
||||||
|
return &service.UpstreamFailoverError{
|
||||||
|
StatusCode: statusCode,
|
||||||
|
RetryableOnSameAccount: retryable,
|
||||||
|
ForceCacheBilling: forceBilling,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// NewFailoverState 测试
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestNewFailoverState(t *testing.T) {
|
||||||
|
t.Run("初始化字段正确", func(t *testing.T) {
|
||||||
|
fs := NewFailoverState(5, true)
|
||||||
|
require.Equal(t, 5, fs.MaxSwitches)
|
||||||
|
require.Equal(t, 0, fs.SwitchCount)
|
||||||
|
require.NotNil(t, fs.FailedAccountIDs)
|
||||||
|
require.Empty(t, fs.FailedAccountIDs)
|
||||||
|
require.NotNil(t, fs.SameAccountRetryCount)
|
||||||
|
require.Empty(t, fs.SameAccountRetryCount)
|
||||||
|
require.Nil(t, fs.LastFailoverErr)
|
||||||
|
require.False(t, fs.ForceCacheBilling)
|
||||||
|
require.True(t, fs.hasBoundSession)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("无绑定会话", func(t *testing.T) {
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
require.Equal(t, 3, fs.MaxSwitches)
|
||||||
|
require.False(t, fs.hasBoundSession)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("零最大切换次数", func(t *testing.T) {
|
||||||
|
fs := NewFailoverState(0, false)
|
||||||
|
require.Equal(t, 0, fs.MaxSwitches)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// sleepWithContext 测试
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestSleepWithContext(t *testing.T) {
|
||||||
|
t.Run("零时长立即返回true", func(t *testing.T) {
|
||||||
|
start := time.Now()
|
||||||
|
ok := sleepWithContext(context.Background(), 0)
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Less(t, time.Since(start), 50*time.Millisecond)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("负时长立即返回true", func(t *testing.T) {
|
||||||
|
start := time.Now()
|
||||||
|
ok := sleepWithContext(context.Background(), -1*time.Second)
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Less(t, time.Since(start), 50*time.Millisecond)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("正常等待后返回true", func(t *testing.T) {
|
||||||
|
start := time.Now()
|
||||||
|
ok := sleepWithContext(context.Background(), 50*time.Millisecond)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
require.True(t, ok)
|
||||||
|
require.GreaterOrEqual(t, elapsed, 40*time.Millisecond)
|
||||||
|
require.Less(t, elapsed, 500*time.Millisecond)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("已取消context立即返回false", func(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
ok := sleepWithContext(ctx, 5*time.Second)
|
||||||
|
require.False(t, ok)
|
||||||
|
require.Less(t, time.Since(start), 50*time.Millisecond)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("等待期间context取消返回false", func(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
go func() {
|
||||||
|
time.Sleep(30 * time.Millisecond)
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
ok := sleepWithContext(ctx, 5*time.Second)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
require.False(t, ok)
|
||||||
|
require.Less(t, elapsed, 500*time.Millisecond)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HandleFailoverError — 基本切换流程
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestHandleFailoverError_BasicSwitch(t *testing.T) {
|
||||||
|
t.Run("非重试错误_非Antigravity_直接切换", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(500, false, false)
|
||||||
|
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SwitchCount)
|
||||||
|
require.Contains(t, fs.FailedAccountIDs, int64(100))
|
||||||
|
require.Equal(t, err, fs.LastFailoverErr)
|
||||||
|
require.False(t, fs.ForceCacheBilling)
|
||||||
|
require.Empty(t, mock.calls, "不应调用 TempUnschedule")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("非重试错误_Antigravity_第一次切换无延迟", func(t *testing.T) {
|
||||||
|
// switchCount 从 0→1 时,sleepFailoverDelay(ctx, 1) 的延时 = (1-1)*1s = 0
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(500, false, false)
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, service.PlatformAntigravity, err)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SwitchCount)
|
||||||
|
require.Less(t, elapsed, 200*time.Millisecond, "第一次切换延迟应为 0")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("非重试错误_Antigravity_第二次切换有1秒延迟", func(t *testing.T) {
|
||||||
|
// switchCount 从 1→2 时,sleepFailoverDelay(ctx, 2) 的延时 = (2-1)*1s = 1s
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
fs.SwitchCount = 1 // 模拟已切换一次
|
||||||
|
|
||||||
|
err := newTestFailoverErr(500, false, false)
|
||||||
|
start := time.Now()
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 200, service.PlatformAntigravity, err)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 2, fs.SwitchCount)
|
||||||
|
require.GreaterOrEqual(t, elapsed, 800*time.Millisecond, "第二次切换延迟应约 1s")
|
||||||
|
require.Less(t, elapsed, 3*time.Second)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("连续切换直到耗尽", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(2, false)
|
||||||
|
|
||||||
|
// 第一次切换:0→1
|
||||||
|
err1 := newTestFailoverErr(500, false, false)
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", err1)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SwitchCount)
|
||||||
|
|
||||||
|
// 第二次切换:1→2
|
||||||
|
err2 := newTestFailoverErr(502, false, false)
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 200, "openai", err2)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 2, fs.SwitchCount)
|
||||||
|
|
||||||
|
// 第三次已耗尽:SwitchCount(2) >= MaxSwitches(2)
|
||||||
|
err3 := newTestFailoverErr(503, false, false)
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 300, "openai", err3)
|
||||||
|
require.Equal(t, FailoverExhausted, action)
|
||||||
|
require.Equal(t, 2, fs.SwitchCount, "耗尽时不应继续递增")
|
||||||
|
|
||||||
|
// 验证失败账号列表
|
||||||
|
require.Len(t, fs.FailedAccountIDs, 3)
|
||||||
|
require.Contains(t, fs.FailedAccountIDs, int64(100))
|
||||||
|
require.Contains(t, fs.FailedAccountIDs, int64(200))
|
||||||
|
require.Contains(t, fs.FailedAccountIDs, int64(300))
|
||||||
|
|
||||||
|
// LastFailoverErr 应为最后一次的错误
|
||||||
|
require.Equal(t, err3, fs.LastFailoverErr)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("MaxSwitches为0时首次即耗尽", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(0, false)
|
||||||
|
err := newTestFailoverErr(500, false, false)
|
||||||
|
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Equal(t, FailoverExhausted, action)
|
||||||
|
require.Equal(t, 0, fs.SwitchCount)
|
||||||
|
require.Contains(t, fs.FailedAccountIDs, int64(100))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HandleFailoverError — 缓存计费 (ForceCacheBilling)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestHandleFailoverError_CacheBilling(t *testing.T) {
|
||||||
|
t.Run("hasBoundSession为true时设置ForceCacheBilling", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, true) // hasBoundSession=true
|
||||||
|
err := newTestFailoverErr(500, false, false)
|
||||||
|
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.True(t, fs.ForceCacheBilling)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("failoverErr.ForceCacheBilling为true时设置", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(500, false, true) // ForceCacheBilling=true
|
||||||
|
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.True(t, fs.ForceCacheBilling)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("两者均为false时不设置", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(500, false, false)
|
||||||
|
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.False(t, fs.ForceCacheBilling)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("一旦设置不会被后续错误重置", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
|
||||||
|
// 第一次:ForceCacheBilling=true → 设置
|
||||||
|
err1 := newTestFailoverErr(500, false, true)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err1)
|
||||||
|
require.True(t, fs.ForceCacheBilling)
|
||||||
|
|
||||||
|
// 第二次:ForceCacheBilling=false → 仍然保持 true
|
||||||
|
err2 := newTestFailoverErr(502, false, false)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 200, "openai", err2)
|
||||||
|
require.True(t, fs.ForceCacheBilling, "ForceCacheBilling 一旦设置不应被重置")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HandleFailoverError — 同账号重试 (RetryableOnSameAccount)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestHandleFailoverError_SameAccountRetry(t *testing.T) {
|
||||||
|
t.Run("第一次重试返回FailoverContinue", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(400, true, false)
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SameAccountRetryCount[100])
|
||||||
|
require.Equal(t, 0, fs.SwitchCount, "同账号重试不应增加切换计数")
|
||||||
|
require.NotContains(t, fs.FailedAccountIDs, int64(100), "同账号重试不应加入失败列表")
|
||||||
|
require.Empty(t, mock.calls, "同账号重试期间不应调用 TempUnschedule")
|
||||||
|
// 验证等待了 sameAccountRetryDelay (500ms)
|
||||||
|
require.GreaterOrEqual(t, elapsed, 400*time.Millisecond)
|
||||||
|
require.Less(t, elapsed, 2*time.Second)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("第二次重试仍返回FailoverContinue", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(400, true, false)
|
||||||
|
|
||||||
|
// 第一次
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SameAccountRetryCount[100])
|
||||||
|
|
||||||
|
// 第二次
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 2, fs.SameAccountRetryCount[100])
|
||||||
|
|
||||||
|
require.Empty(t, mock.calls, "两次重试期间均不应调用 TempUnschedule")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("第三次重试耗尽_触发TempUnschedule并切换", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(400, true, false)
|
||||||
|
|
||||||
|
// 第一次、第二次重试
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Equal(t, 2, fs.SameAccountRetryCount[100])
|
||||||
|
|
||||||
|
// 第三次:重试已达到 maxSameAccountRetries(2),应切换账号
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SwitchCount)
|
||||||
|
require.Contains(t, fs.FailedAccountIDs, int64(100))
|
||||||
|
|
||||||
|
// 验证 TempUnschedule 被调用
|
||||||
|
require.Len(t, mock.calls, 1)
|
||||||
|
require.Equal(t, int64(100), mock.calls[0].accountID)
|
||||||
|
require.Equal(t, err, mock.calls[0].failoverErr)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("不同账号独立跟踪重试次数", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(5, false)
|
||||||
|
err := newTestFailoverErr(400, true, false)
|
||||||
|
|
||||||
|
// 账号 100 第一次重试
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SameAccountRetryCount[100])
|
||||||
|
|
||||||
|
// 账号 200 第一次重试(独立计数)
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 200, "openai", err)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SameAccountRetryCount[200])
|
||||||
|
require.Equal(t, 1, fs.SameAccountRetryCount[100], "账号 100 的计数不应受影响")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("重试耗尽后再次遇到同账号_直接切换", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(5, false)
|
||||||
|
err := newTestFailoverErr(400, true, false)
|
||||||
|
|
||||||
|
// 耗尽账号 100 的重试
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
// 第三次: 重试耗尽 → 切换
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
|
||||||
|
// 再次遇到账号 100,计数仍为 2,条件不满足 → 直接切换
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Len(t, mock.calls, 2, "第二次耗尽也应调用 TempUnschedule")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HandleFailoverError — TempUnschedule 调用验证
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestHandleFailoverError_TempUnschedule(t *testing.T) {
|
||||||
|
t.Run("非重试错误不调用TempUnschedule", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(500, false, false) // RetryableOnSameAccount=false
|
||||||
|
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Empty(t, mock.calls)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("重试错误耗尽后调用TempUnschedule_传入正确参数", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(502, true, false)
|
||||||
|
|
||||||
|
// 耗尽重试
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 42, "openai", err)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 42, "openai", err)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 42, "openai", err)
|
||||||
|
|
||||||
|
require.Len(t, mock.calls, 1)
|
||||||
|
require.Equal(t, int64(42), mock.calls[0].accountID)
|
||||||
|
require.Equal(t, 502, mock.calls[0].failoverErr.StatusCode)
|
||||||
|
require.True(t, mock.calls[0].failoverErr.RetryableOnSameAccount)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HandleFailoverError — Context 取消
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestHandleFailoverError_ContextCanceled(t *testing.T) {
|
||||||
|
t.Run("同账号重试sleep期间context取消", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(400, true, false)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel() // 立即取消
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
action := fs.HandleFailoverError(ctx, mock, 100, "openai", err)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
require.Equal(t, FailoverCanceled, action)
|
||||||
|
require.Less(t, elapsed, 100*time.Millisecond, "应立即返回")
|
||||||
|
// 重试计数仍应递增
|
||||||
|
require.Equal(t, 1, fs.SameAccountRetryCount[100])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Antigravity延迟期间context取消", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
fs.SwitchCount = 1 // 下一次 switchCount=2 → delay = 1s
|
||||||
|
err := newTestFailoverErr(500, false, false)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel() // 立即取消
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
action := fs.HandleFailoverError(ctx, mock, 100, service.PlatformAntigravity, err)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
require.Equal(t, FailoverCanceled, action)
|
||||||
|
require.Less(t, elapsed, 100*time.Millisecond, "应立即返回而非等待 1s")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HandleFailoverError — FailedAccountIDs 跟踪
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestHandleFailoverError_FailedAccountIDs(t *testing.T) {
|
||||||
|
t.Run("切换时添加到失败列表", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", newTestFailoverErr(500, false, false))
|
||||||
|
require.Contains(t, fs.FailedAccountIDs, int64(100))
|
||||||
|
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 200, "openai", newTestFailoverErr(502, false, false))
|
||||||
|
require.Contains(t, fs.FailedAccountIDs, int64(200))
|
||||||
|
require.Len(t, fs.FailedAccountIDs, 2)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("耗尽时也添加到失败列表", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(0, false)
|
||||||
|
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", newTestFailoverErr(500, false, false))
|
||||||
|
require.Equal(t, FailoverExhausted, action)
|
||||||
|
require.Contains(t, fs.FailedAccountIDs, int64(100))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("同账号重试期间不添加到失败列表", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", newTestFailoverErr(400, true, false))
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.NotContains(t, fs.FailedAccountIDs, int64(100))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("同一账号多次切换不重复添加", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(5, false)
|
||||||
|
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", newTestFailoverErr(500, false, false))
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", newTestFailoverErr(500, false, false))
|
||||||
|
require.Len(t, fs.FailedAccountIDs, 1, "map 天然去重")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HandleFailoverError — LastFailoverErr 更新
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestHandleFailoverError_LastFailoverErr(t *testing.T) {
|
||||||
|
t.Run("每次调用都更新LastFailoverErr", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
|
||||||
|
err1 := newTestFailoverErr(500, false, false)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err1)
|
||||||
|
require.Equal(t, err1, fs.LastFailoverErr)
|
||||||
|
|
||||||
|
err2 := newTestFailoverErr(502, false, false)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 200, "openai", err2)
|
||||||
|
require.Equal(t, err2, fs.LastFailoverErr)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("同账号重试时也更新LastFailoverErr", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
|
||||||
|
err := newTestFailoverErr(400, true, false)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Equal(t, err, fs.LastFailoverErr)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HandleFailoverError — 综合集成场景
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestHandleFailoverError_IntegrationScenario(t *testing.T) {
|
||||||
|
t.Run("模拟完整failover流程_多账号混合重试与切换", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, true) // hasBoundSession=true
|
||||||
|
|
||||||
|
// 1. 账号 100 遇到可重试错误,同账号重试 2 次
|
||||||
|
retryErr := newTestFailoverErr(400, true, false)
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", retryErr)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.True(t, fs.ForceCacheBilling, "hasBoundSession=true 应设置 ForceCacheBilling")
|
||||||
|
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 100, "openai", retryErr)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
|
||||||
|
// 2. 账号 100 重试耗尽 → TempUnschedule + 切换
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 100, "openai", retryErr)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SwitchCount)
|
||||||
|
require.Len(t, mock.calls, 1)
|
||||||
|
|
||||||
|
// 3. 账号 200 遇到不可重试错误 → 直接切换
|
||||||
|
switchErr := newTestFailoverErr(500, false, false)
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 200, "openai", switchErr)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 2, fs.SwitchCount)
|
||||||
|
|
||||||
|
// 4. 账号 300 遇到不可重试错误 → 再切换
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 300, "openai", switchErr)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 3, fs.SwitchCount)
|
||||||
|
|
||||||
|
// 5. 账号 400 → 已耗尽 (SwitchCount=3 >= MaxSwitches=3)
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 400, "openai", switchErr)
|
||||||
|
require.Equal(t, FailoverExhausted, action)
|
||||||
|
|
||||||
|
// 最终状态验证
|
||||||
|
require.Equal(t, 3, fs.SwitchCount, "耗尽时不再递增")
|
||||||
|
require.Len(t, fs.FailedAccountIDs, 4, "4个不同账号都在失败列表中")
|
||||||
|
require.True(t, fs.ForceCacheBilling)
|
||||||
|
require.Len(t, mock.calls, 1, "只有账号 100 触发了 TempUnschedule")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("模拟Antigravity平台完整流程", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(2, false)
|
||||||
|
|
||||||
|
err := newTestFailoverErr(500, false, false)
|
||||||
|
|
||||||
|
// 第一次切换:delay = 0s
|
||||||
|
start := time.Now()
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, service.PlatformAntigravity, err)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Less(t, elapsed, 200*time.Millisecond, "第一次切换延迟为 0")
|
||||||
|
|
||||||
|
// 第二次切换:delay = 1s
|
||||||
|
start = time.Now()
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 200, service.PlatformAntigravity, err)
|
||||||
|
elapsed = time.Since(start)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.GreaterOrEqual(t, elapsed, 800*time.Millisecond, "第二次切换延迟约 1s")
|
||||||
|
|
||||||
|
// 第三次:耗尽(无延迟,因为在检查延迟之前就返回了)
|
||||||
|
start = time.Now()
|
||||||
|
action = fs.HandleFailoverError(context.Background(), mock, 300, service.PlatformAntigravity, err)
|
||||||
|
elapsed = time.Since(start)
|
||||||
|
require.Equal(t, FailoverExhausted, action)
|
||||||
|
require.Less(t, elapsed, 200*time.Millisecond, "耗尽时不应有延迟")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("ForceCacheBilling通过错误标志设置", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false) // hasBoundSession=false
|
||||||
|
|
||||||
|
// 第一次:ForceCacheBilling=false
|
||||||
|
err1 := newTestFailoverErr(500, false, false)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 100, "openai", err1)
|
||||||
|
require.False(t, fs.ForceCacheBilling)
|
||||||
|
|
||||||
|
// 第二次:ForceCacheBilling=true(Antigravity 粘性会话切换)
|
||||||
|
err2 := newTestFailoverErr(500, false, true)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 200, "openai", err2)
|
||||||
|
require.True(t, fs.ForceCacheBilling, "错误标志应触发 ForceCacheBilling")
|
||||||
|
|
||||||
|
// 第三次:ForceCacheBilling=false,但状态仍保持 true
|
||||||
|
err3 := newTestFailoverErr(500, false, false)
|
||||||
|
fs.HandleFailoverError(context.Background(), mock, 300, "openai", err3)
|
||||||
|
require.True(t, fs.ForceCacheBilling, "不应重置")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HandleFailoverError — 边界条件
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestHandleFailoverError_EdgeCases(t *testing.T) {
|
||||||
|
t.Run("StatusCode为0的错误也能正常处理", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(0, false, false)
|
||||||
|
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "openai", err)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("AccountID为0也能正常跟踪", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(500, true, false)
|
||||||
|
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 0, "openai", err)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SameAccountRetryCount[0])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("负AccountID也能正常跟踪", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
err := newTestFailoverErr(500, true, false)
|
||||||
|
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, -1, "openai", err)
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Equal(t, 1, fs.SameAccountRetryCount[-1])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("空平台名称不触发Antigravity延迟", func(t *testing.T) {
|
||||||
|
mock := &mockTempUnscheduler{}
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
fs.SwitchCount = 1
|
||||||
|
err := newTestFailoverErr(500, false, false)
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
action := fs.HandleFailoverError(context.Background(), mock, 100, "", err)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Less(t, elapsed, 200*time.Millisecond, "空平台不应触发 Antigravity 延迟")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// HandleSelectionExhausted 测试
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestHandleSelectionExhausted(t *testing.T) {
|
||||||
|
t.Run("无LastFailoverErr时返回Exhausted", func(t *testing.T) {
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
// LastFailoverErr 为 nil
|
||||||
|
|
||||||
|
action := fs.HandleSelectionExhausted(context.Background())
|
||||||
|
require.Equal(t, FailoverExhausted, action)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("非503错误返回Exhausted", func(t *testing.T) {
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
fs.LastFailoverErr = newTestFailoverErr(500, false, false)
|
||||||
|
|
||||||
|
action := fs.HandleSelectionExhausted(context.Background())
|
||||||
|
require.Equal(t, FailoverExhausted, action)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("503且未耗尽_等待后返回Continue并清除失败列表", func(t *testing.T) {
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
fs.LastFailoverErr = newTestFailoverErr(503, false, false)
|
||||||
|
fs.FailedAccountIDs[100] = struct{}{}
|
||||||
|
fs.SwitchCount = 1
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
action := fs.HandleSelectionExhausted(context.Background())
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
require.Empty(t, fs.FailedAccountIDs, "应清除失败账号列表")
|
||||||
|
require.GreaterOrEqual(t, elapsed, 1500*time.Millisecond, "应等待约 2s")
|
||||||
|
require.Less(t, elapsed, 5*time.Second)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("503但SwitchCount已超过MaxSwitches_返回Exhausted", func(t *testing.T) {
|
||||||
|
fs := NewFailoverState(2, false)
|
||||||
|
fs.LastFailoverErr = newTestFailoverErr(503, false, false)
|
||||||
|
fs.SwitchCount = 3 // > MaxSwitches(2)
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
action := fs.HandleSelectionExhausted(context.Background())
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
require.Equal(t, FailoverExhausted, action)
|
||||||
|
require.Less(t, elapsed, 100*time.Millisecond, "不应等待")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("503但context已取消_返回Canceled", func(t *testing.T) {
|
||||||
|
fs := NewFailoverState(3, false)
|
||||||
|
fs.LastFailoverErr = newTestFailoverErr(503, false, false)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
action := fs.HandleSelectionExhausted(ctx)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
require.Equal(t, FailoverCanceled, action)
|
||||||
|
require.Less(t, elapsed, 100*time.Millisecond, "应立即返回")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("503且SwitchCount等于MaxSwitches_仍可重试", func(t *testing.T) {
|
||||||
|
fs := NewFailoverState(2, false)
|
||||||
|
fs.LastFailoverErr = newTestFailoverErr(503, false, false)
|
||||||
|
fs.SwitchCount = 2 // == MaxSwitches,条件是 <=,仍可重试
|
||||||
|
|
||||||
|
action := fs.HandleSelectionExhausted(context.Background())
|
||||||
|
require.Equal(t, FailoverContinue, action)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -232,12 +232,7 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
hasBoundSession := sessionKey != "" && sessionBoundAccountID > 0
|
hasBoundSession := sessionKey != "" && sessionBoundAccountID > 0
|
||||||
|
|
||||||
if platform == service.PlatformGemini {
|
if platform == service.PlatformGemini {
|
||||||
maxAccountSwitches := h.maxAccountSwitchesGemini
|
fs := NewFailoverState(h.maxAccountSwitchesGemini, hasBoundSession)
|
||||||
switchCount := 0
|
|
||||||
failedAccountIDs := make(map[int64]struct{})
|
|
||||||
sameAccountRetryCount := make(map[int64]int) // 同账号重试计数
|
|
||||||
var lastFailoverErr *service.UpstreamFailoverError
|
|
||||||
var forceCacheBilling bool // 粘性会话切换时的缓存计费标记
|
|
||||||
|
|
||||||
// 单账号分组提前设置 SingleAccountRetry 标记,让 Service 层首次 503 就不设模型限流标记。
|
// 单账号分组提前设置 SingleAccountRetry 标记,让 Service 层首次 503 就不设模型限流标记。
|
||||||
// 避免单账号分组收到 503 (MODEL_CAPACITY_EXHAUSTED) 时设 29s 限流,导致后续请求连续快速失败。
|
// 避免单账号分组收到 503 (MODEL_CAPACITY_EXHAUSTED) 时设 29s 限流,导致后续请求连续快速失败。
|
||||||
@@ -247,31 +242,28 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
selection, err := h.gatewayService.SelectAccountWithLoadAwareness(c.Request.Context(), apiKey.GroupID, sessionKey, reqModel, failedAccountIDs, "") // Gemini 不使用会话限制
|
selection, err := h.gatewayService.SelectAccountWithLoadAwareness(c.Request.Context(), apiKey.GroupID, sessionKey, reqModel, fs.FailedAccountIDs, "") // Gemini 不使用会话限制
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(failedAccountIDs) == 0 {
|
if len(fs.FailedAccountIDs) == 0 {
|
||||||
h.handleStreamingAwareError(c, http.StatusServiceUnavailable, "api_error", "No available accounts: "+err.Error(), streamStarted)
|
h.handleStreamingAwareError(c, http.StatusServiceUnavailable, "api_error", "No available accounts: "+err.Error(), streamStarted)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Antigravity 单账号退避重试:分组内没有其他可用账号时,
|
action := fs.HandleSelectionExhausted(c.Request.Context())
|
||||||
// 对 503 错误不直接返回,而是清除排除列表、等待退避后重试同一个账号。
|
switch action {
|
||||||
// 谷歌上游 503 (MODEL_CAPACITY_EXHAUSTED) 通常是暂时性的,等几秒就能恢复。
|
case FailoverContinue:
|
||||||
if lastFailoverErr != nil && lastFailoverErr.StatusCode == http.StatusServiceUnavailable && switchCount <= maxAccountSwitches {
|
ctx := context.WithValue(c.Request.Context(), ctxkey.SingleAccountRetry, true)
|
||||||
if sleepAntigravitySingleAccountBackoff(c.Request.Context(), switchCount) {
|
c.Request = c.Request.WithContext(ctx)
|
||||||
log.Printf("Antigravity single-account 503 retry: clearing failed accounts, retry %d/%d", switchCount, maxAccountSwitches)
|
continue
|
||||||
failedAccountIDs = make(map[int64]struct{})
|
case FailoverCanceled:
|
||||||
// 设置 context 标记,让 Service 层预检查等待限流过期而非直接切换
|
return
|
||||||
ctx := context.WithValue(c.Request.Context(), ctxkey.SingleAccountRetry, true)
|
default: // FailoverExhausted
|
||||||
c.Request = c.Request.WithContext(ctx)
|
if fs.LastFailoverErr != nil {
|
||||||
continue
|
h.handleFailoverExhausted(c, fs.LastFailoverErr, service.PlatformGemini, streamStarted)
|
||||||
|
} else {
|
||||||
|
h.handleFailoverExhaustedSimple(c, 502, streamStarted)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if lastFailoverErr != nil {
|
|
||||||
h.handleFailoverExhausted(c, lastFailoverErr, service.PlatformGemini, streamStarted)
|
|
||||||
} else {
|
|
||||||
h.handleFailoverExhaustedSimple(c, 502, streamStarted)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
account := selection.Account
|
account := selection.Account
|
||||||
setOpsSelectedAccount(c, account.ID)
|
setOpsSelectedAccount(c, account.ID)
|
||||||
@@ -346,8 +338,8 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
// 转发请求 - 根据账号平台分流
|
// 转发请求 - 根据账号平台分流
|
||||||
var result *service.ForwardResult
|
var result *service.ForwardResult
|
||||||
requestCtx := c.Request.Context()
|
requestCtx := c.Request.Context()
|
||||||
if switchCount > 0 {
|
if fs.SwitchCount > 0 {
|
||||||
requestCtx = context.WithValue(requestCtx, ctxkey.AccountSwitchCount, switchCount)
|
requestCtx = context.WithValue(requestCtx, ctxkey.AccountSwitchCount, fs.SwitchCount)
|
||||||
}
|
}
|
||||||
if account.Platform == service.PlatformAntigravity {
|
if account.Platform == service.PlatformAntigravity {
|
||||||
result, err = h.antigravityGatewayService.ForwardGemini(requestCtx, c, account, reqModel, "generateContent", reqStream, body, hasBoundSession)
|
result, err = h.antigravityGatewayService.ForwardGemini(requestCtx, c, account, reqModel, "generateContent", reqStream, body, hasBoundSession)
|
||||||
@@ -360,40 +352,16 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
var failoverErr *service.UpstreamFailoverError
|
var failoverErr *service.UpstreamFailoverError
|
||||||
if errors.As(err, &failoverErr) {
|
if errors.As(err, &failoverErr) {
|
||||||
lastFailoverErr = failoverErr
|
action := fs.HandleFailoverError(c.Request.Context(), h.gatewayService, account.ID, account.Platform, failoverErr)
|
||||||
if needForceCacheBilling(hasBoundSession, failoverErr) {
|
switch action {
|
||||||
forceCacheBilling = true
|
case FailoverContinue:
|
||||||
}
|
|
||||||
|
|
||||||
// 同账号重试:对 RetryableOnSameAccount 的临时性错误,先在同一账号上重试
|
|
||||||
if failoverErr.RetryableOnSameAccount && sameAccountRetryCount[account.ID] < maxSameAccountRetries {
|
|
||||||
sameAccountRetryCount[account.ID]++
|
|
||||||
log.Printf("Account %d: retryable error %d, same-account retry %d/%d",
|
|
||||||
account.ID, failoverErr.StatusCode, sameAccountRetryCount[account.ID], maxSameAccountRetries)
|
|
||||||
if !sleepSameAccountRetryDelay(c.Request.Context()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
case FailoverExhausted:
|
||||||
|
h.handleFailoverExhausted(c, fs.LastFailoverErr, service.PlatformGemini, streamStarted)
|
||||||
// 同账号重试用尽,执行临时封禁并切换账号
|
return
|
||||||
if failoverErr.RetryableOnSameAccount {
|
case FailoverCanceled:
|
||||||
h.gatewayService.TempUnscheduleRetryableError(c.Request.Context(), account.ID, failoverErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
failedAccountIDs[account.ID] = struct{}{}
|
|
||||||
if switchCount >= maxAccountSwitches {
|
|
||||||
h.handleFailoverExhausted(c, failoverErr, service.PlatformGemini, streamStarted)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switchCount++
|
|
||||||
log.Printf("Account %d: upstream error %d, switching account %d/%d", account.ID, failoverErr.StatusCode, switchCount, maxAccountSwitches)
|
|
||||||
if account.Platform == service.PlatformAntigravity {
|
|
||||||
if !sleepFailoverDelay(c.Request.Context(), switchCount) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
// 错误响应已在Forward中处理,这里只记录日志
|
// 错误响应已在Forward中处理,这里只记录日志
|
||||||
log.Printf("Forward request failed: %v", err)
|
log.Printf("Forward request failed: %v", err)
|
||||||
@@ -421,7 +389,7 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Printf("Record usage failed: %v", err)
|
log.Printf("Record usage failed: %v", err)
|
||||||
}
|
}
|
||||||
}(result, account, userAgent, clientIP, forceCacheBilling)
|
}(result, account, userAgent, clientIP, fs.ForceCacheBilling)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -442,41 +410,33 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
maxAccountSwitches := h.maxAccountSwitches
|
fs := NewFailoverState(h.maxAccountSwitches, hasBoundSession)
|
||||||
switchCount := 0
|
|
||||||
failedAccountIDs := make(map[int64]struct{})
|
|
||||||
sameAccountRetryCount := make(map[int64]int) // 同账号重试计数
|
|
||||||
var lastFailoverErr *service.UpstreamFailoverError
|
|
||||||
retryWithFallback := false
|
retryWithFallback := false
|
||||||
var forceCacheBilling bool // 粘性会话切换时的缓存计费标记
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// 选择支持该模型的账号
|
// 选择支持该模型的账号
|
||||||
selection, err := h.gatewayService.SelectAccountWithLoadAwareness(c.Request.Context(), currentAPIKey.GroupID, sessionKey, reqModel, failedAccountIDs, parsedReq.MetadataUserID)
|
selection, err := h.gatewayService.SelectAccountWithLoadAwareness(c.Request.Context(), currentAPIKey.GroupID, sessionKey, reqModel, fs.FailedAccountIDs, parsedReq.MetadataUserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(failedAccountIDs) == 0 {
|
if len(fs.FailedAccountIDs) == 0 {
|
||||||
h.handleStreamingAwareError(c, http.StatusServiceUnavailable, "api_error", "No available accounts: "+err.Error(), streamStarted)
|
h.handleStreamingAwareError(c, http.StatusServiceUnavailable, "api_error", "No available accounts: "+err.Error(), streamStarted)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Antigravity 单账号退避重试:分组内没有其他可用账号时,
|
action := fs.HandleSelectionExhausted(c.Request.Context())
|
||||||
// 对 503 错误不直接返回,而是清除排除列表、等待退避后重试同一个账号。
|
switch action {
|
||||||
// 谷歌上游 503 (MODEL_CAPACITY_EXHAUSTED) 通常是暂时性的,等几秒就能恢复。
|
case FailoverContinue:
|
||||||
if lastFailoverErr != nil && lastFailoverErr.StatusCode == http.StatusServiceUnavailable && switchCount <= maxAccountSwitches {
|
ctx := context.WithValue(c.Request.Context(), ctxkey.SingleAccountRetry, true)
|
||||||
if sleepAntigravitySingleAccountBackoff(c.Request.Context(), switchCount) {
|
c.Request = c.Request.WithContext(ctx)
|
||||||
log.Printf("Antigravity single-account 503 retry: clearing failed accounts, retry %d/%d", switchCount, maxAccountSwitches)
|
continue
|
||||||
failedAccountIDs = make(map[int64]struct{})
|
case FailoverCanceled:
|
||||||
// 设置 context 标记,让 Service 层预检查等待限流过期而非直接切换
|
return
|
||||||
ctx := context.WithValue(c.Request.Context(), ctxkey.SingleAccountRetry, true)
|
default: // FailoverExhausted
|
||||||
c.Request = c.Request.WithContext(ctx)
|
if fs.LastFailoverErr != nil {
|
||||||
continue
|
h.handleFailoverExhausted(c, fs.LastFailoverErr, platform, streamStarted)
|
||||||
|
} else {
|
||||||
|
h.handleFailoverExhaustedSimple(c, 502, streamStarted)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if lastFailoverErr != nil {
|
|
||||||
h.handleFailoverExhausted(c, lastFailoverErr, platform, streamStarted)
|
|
||||||
} else {
|
|
||||||
h.handleFailoverExhaustedSimple(c, 502, streamStarted)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
account := selection.Account
|
account := selection.Account
|
||||||
setOpsSelectedAccount(c, account.ID)
|
setOpsSelectedAccount(c, account.ID)
|
||||||
@@ -549,8 +509,8 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
// 转发请求 - 根据账号平台分流
|
// 转发请求 - 根据账号平台分流
|
||||||
var result *service.ForwardResult
|
var result *service.ForwardResult
|
||||||
requestCtx := c.Request.Context()
|
requestCtx := c.Request.Context()
|
||||||
if switchCount > 0 {
|
if fs.SwitchCount > 0 {
|
||||||
requestCtx = context.WithValue(requestCtx, ctxkey.AccountSwitchCount, switchCount)
|
requestCtx = context.WithValue(requestCtx, ctxkey.AccountSwitchCount, fs.SwitchCount)
|
||||||
}
|
}
|
||||||
if account.Platform == service.PlatformAntigravity && account.Type != service.AccountTypeAPIKey {
|
if account.Platform == service.PlatformAntigravity && account.Type != service.AccountTypeAPIKey {
|
||||||
result, err = h.antigravityGatewayService.Forward(requestCtx, c, account, body, hasBoundSession)
|
result, err = h.antigravityGatewayService.Forward(requestCtx, c, account, body, hasBoundSession)
|
||||||
@@ -598,40 +558,16 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
var failoverErr *service.UpstreamFailoverError
|
var failoverErr *service.UpstreamFailoverError
|
||||||
if errors.As(err, &failoverErr) {
|
if errors.As(err, &failoverErr) {
|
||||||
lastFailoverErr = failoverErr
|
action := fs.HandleFailoverError(c.Request.Context(), h.gatewayService, account.ID, account.Platform, failoverErr)
|
||||||
if needForceCacheBilling(hasBoundSession, failoverErr) {
|
switch action {
|
||||||
forceCacheBilling = true
|
case FailoverContinue:
|
||||||
}
|
|
||||||
|
|
||||||
// 同账号重试:对 RetryableOnSameAccount 的临时性错误,先在同一账号上重试
|
|
||||||
if failoverErr.RetryableOnSameAccount && sameAccountRetryCount[account.ID] < maxSameAccountRetries {
|
|
||||||
sameAccountRetryCount[account.ID]++
|
|
||||||
log.Printf("Account %d: retryable error %d, same-account retry %d/%d",
|
|
||||||
account.ID, failoverErr.StatusCode, sameAccountRetryCount[account.ID], maxSameAccountRetries)
|
|
||||||
if !sleepSameAccountRetryDelay(c.Request.Context()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
case FailoverExhausted:
|
||||||
|
h.handleFailoverExhausted(c, fs.LastFailoverErr, account.Platform, streamStarted)
|
||||||
// 同账号重试用尽,执行临时封禁并切换账号
|
return
|
||||||
if failoverErr.RetryableOnSameAccount {
|
case FailoverCanceled:
|
||||||
h.gatewayService.TempUnscheduleRetryableError(c.Request.Context(), account.ID, failoverErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
failedAccountIDs[account.ID] = struct{}{}
|
|
||||||
if switchCount >= maxAccountSwitches {
|
|
||||||
h.handleFailoverExhausted(c, failoverErr, account.Platform, streamStarted)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switchCount++
|
|
||||||
log.Printf("Account %d: upstream error %d, switching account %d/%d", account.ID, failoverErr.StatusCode, switchCount, maxAccountSwitches)
|
|
||||||
if account.Platform == service.PlatformAntigravity {
|
|
||||||
if !sleepFailoverDelay(c.Request.Context(), switchCount) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
// 错误响应已在Forward中处理,这里只记录日志
|
// 错误响应已在Forward中处理,这里只记录日志
|
||||||
log.Printf("Account %d: Forward request failed: %v", account.ID, err)
|
log.Printf("Account %d: Forward request failed: %v", account.ID, err)
|
||||||
@@ -659,7 +595,7 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Printf("Record usage failed: %v", err)
|
log.Printf("Record usage failed: %v", err)
|
||||||
}
|
}
|
||||||
}(result, account, userAgent, clientIP, forceCacheBilling)
|
}(result, account, userAgent, clientIP, fs.ForceCacheBilling)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !retryWithFallback {
|
if !retryWithFallback {
|
||||||
@@ -893,65 +829,6 @@ func (h *GatewayHandler) handleConcurrencyError(c *gin.Context, err error, slotT
|
|||||||
fmt.Sprintf("Concurrency limit exceeded for %s, please retry later", slotType), streamStarted)
|
fmt.Sprintf("Concurrency limit exceeded for %s, please retry later", slotType), streamStarted)
|
||||||
}
|
}
|
||||||
|
|
||||||
// needForceCacheBilling 判断 failover 时是否需要强制缓存计费
|
|
||||||
// 粘性会话切换账号、或上游明确标记时,将 input_tokens 转为 cache_read 计费
|
|
||||||
func needForceCacheBilling(hasBoundSession bool, failoverErr *service.UpstreamFailoverError) bool {
|
|
||||||
return hasBoundSession || (failoverErr != nil && failoverErr.ForceCacheBilling)
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
// maxSameAccountRetries 同账号重试次数上限(针对 RetryableOnSameAccount 错误)
|
|
||||||
maxSameAccountRetries = 2
|
|
||||||
// sameAccountRetryDelay 同账号重试间隔
|
|
||||||
sameAccountRetryDelay = 500 * time.Millisecond
|
|
||||||
)
|
|
||||||
|
|
||||||
// sleepSameAccountRetryDelay 同账号重试固定延时,返回 false 表示 context 已取消。
|
|
||||||
func sleepSameAccountRetryDelay(ctx context.Context) bool {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return false
|
|
||||||
case <-time.After(sameAccountRetryDelay):
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sleepFailoverDelay 账号切换线性递增延时:第1次0s、第2次1s、第3次2s…
|
|
||||||
// 返回 false 表示 context 已取消。
|
|
||||||
func sleepFailoverDelay(ctx context.Context, switchCount int) bool {
|
|
||||||
delay := time.Duration(switchCount-1) * time.Second
|
|
||||||
if delay <= 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return false
|
|
||||||
case <-time.After(delay):
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sleepAntigravitySingleAccountBackoff Antigravity 平台单账号分组的 503 退避重试延时。
|
|
||||||
// 当分组内只有一个可用账号且上游返回 503(MODEL_CAPACITY_EXHAUSTED)时使用,
|
|
||||||
// 采用短固定延时策略。Service 层在 SingleAccountRetry 模式下已经做了充分的原地重试
|
|
||||||
// (最多 3 次、总等待 30s),所以 Handler 层的退避只需短暂等待即可。
|
|
||||||
// 返回 false 表示 context 已取消。
|
|
||||||
func sleepAntigravitySingleAccountBackoff(ctx context.Context, retryCount int) bool {
|
|
||||||
// 固定短延时:2s
|
|
||||||
// Service 层已经在原地等待了足够长的时间(retryDelay × 重试次数),
|
|
||||||
// Handler 层只需短暂间隔后重新进入 Service 层即可。
|
|
||||||
const delay = 2 * time.Second
|
|
||||||
|
|
||||||
log.Printf("Antigravity single-account 503 backoff: waiting %v before retry (attempt %d)", delay, retryCount)
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return false
|
|
||||||
case <-time.After(delay):
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *GatewayHandler) handleFailoverExhausted(c *gin.Context, failoverErr *service.UpstreamFailoverError, platform string, streamStarted bool) {
|
func (h *GatewayHandler) handleFailoverExhausted(c *gin.Context, failoverErr *service.UpstreamFailoverError, platform string, streamStarted bool) {
|
||||||
statusCode := failoverErr.StatusCode
|
statusCode := failoverErr.StatusCode
|
||||||
responseBody := failoverErr.ResponseBody
|
responseBody := failoverErr.ResponseBody
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// sleepAntigravitySingleAccountBackoff 测试
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
func TestSleepAntigravitySingleAccountBackoff_ReturnsTrue(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
start := time.Now()
|
|
||||||
ok := sleepAntigravitySingleAccountBackoff(ctx, 1)
|
|
||||||
elapsed := time.Since(start)
|
|
||||||
|
|
||||||
require.True(t, ok, "should return true when context is not canceled")
|
|
||||||
// 固定延迟 2s
|
|
||||||
require.GreaterOrEqual(t, elapsed, 1500*time.Millisecond, "should wait approximately 2s")
|
|
||||||
require.Less(t, elapsed, 5*time.Second, "should not wait too long")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSleepAntigravitySingleAccountBackoff_ContextCanceled(t *testing.T) {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
cancel() // 立即取消
|
|
||||||
|
|
||||||
start := time.Now()
|
|
||||||
ok := sleepAntigravitySingleAccountBackoff(ctx, 1)
|
|
||||||
elapsed := time.Since(start)
|
|
||||||
|
|
||||||
require.False(t, ok, "should return false when context is canceled")
|
|
||||||
require.Less(t, elapsed, 500*time.Millisecond, "should return immediately on cancel")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSleepAntigravitySingleAccountBackoff_FixedDelay(t *testing.T) {
|
|
||||||
// 验证不同 retryCount 都使用固定 2s 延迟
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
start := time.Now()
|
|
||||||
ok := sleepAntigravitySingleAccountBackoff(ctx, 5)
|
|
||||||
elapsed := time.Since(start)
|
|
||||||
|
|
||||||
require.True(t, ok)
|
|
||||||
// 即使 retryCount=5,延迟仍然是固定的 2s
|
|
||||||
require.GreaterOrEqual(t, elapsed, 1500*time.Millisecond)
|
|
||||||
require.Less(t, elapsed, 5*time.Second)
|
|
||||||
}
|
|
||||||
@@ -321,11 +321,7 @@ func (h *GatewayHandler) GeminiV1BetaModels(c *gin.Context) {
|
|||||||
hasBoundSession := sessionKey != "" && sessionBoundAccountID > 0
|
hasBoundSession := sessionKey != "" && sessionBoundAccountID > 0
|
||||||
cleanedForUnknownBinding := false
|
cleanedForUnknownBinding := false
|
||||||
|
|
||||||
maxAccountSwitches := h.maxAccountSwitchesGemini
|
fs := NewFailoverState(h.maxAccountSwitchesGemini, hasBoundSession)
|
||||||
switchCount := 0
|
|
||||||
failedAccountIDs := make(map[int64]struct{})
|
|
||||||
var lastFailoverErr *service.UpstreamFailoverError
|
|
||||||
var forceCacheBilling bool // 粘性会话切换时的缓存计费标记
|
|
||||||
|
|
||||||
// 单账号分组提前设置 SingleAccountRetry 标记,让 Service 层首次 503 就不设模型限流标记。
|
// 单账号分组提前设置 SingleAccountRetry 标记,让 Service 层首次 503 就不设模型限流标记。
|
||||||
// 避免单账号分组收到 503 (MODEL_CAPACITY_EXHAUSTED) 时设 29s 限流,导致后续请求连续快速失败。
|
// 避免单账号分组收到 503 (MODEL_CAPACITY_EXHAUSTED) 时设 29s 限流,导致后续请求连续快速失败。
|
||||||
@@ -335,27 +331,24 @@ func (h *GatewayHandler) GeminiV1BetaModels(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
selection, err := h.gatewayService.SelectAccountWithLoadAwareness(c.Request.Context(), apiKey.GroupID, sessionKey, modelName, failedAccountIDs, "") // Gemini 不使用会话限制
|
selection, err := h.gatewayService.SelectAccountWithLoadAwareness(c.Request.Context(), apiKey.GroupID, sessionKey, modelName, fs.FailedAccountIDs, "") // Gemini 不使用会话限制
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(failedAccountIDs) == 0 {
|
if len(fs.FailedAccountIDs) == 0 {
|
||||||
googleError(c, http.StatusServiceUnavailable, "No available Gemini accounts: "+err.Error())
|
googleError(c, http.StatusServiceUnavailable, "No available Gemini accounts: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Antigravity 单账号退避重试:分组内没有其他可用账号时,
|
action := fs.HandleSelectionExhausted(c.Request.Context())
|
||||||
// 对 503 错误不直接返回,而是清除排除列表、等待退避后重试同一个账号。
|
switch action {
|
||||||
// 谷歌上游 503 (MODEL_CAPACITY_EXHAUSTED) 通常是暂时性的,等几秒就能恢复。
|
case FailoverContinue:
|
||||||
if lastFailoverErr != nil && lastFailoverErr.StatusCode == http.StatusServiceUnavailable && switchCount <= maxAccountSwitches {
|
ctx := context.WithValue(c.Request.Context(), ctxkey.SingleAccountRetry, true)
|
||||||
if sleepAntigravitySingleAccountBackoff(c.Request.Context(), switchCount) {
|
c.Request = c.Request.WithContext(ctx)
|
||||||
log.Printf("Antigravity single-account 503 retry: clearing failed accounts, retry %d/%d", switchCount, maxAccountSwitches)
|
continue
|
||||||
failedAccountIDs = make(map[int64]struct{})
|
case FailoverCanceled:
|
||||||
// 设置 context 标记,让 Service 层预检查等待限流过期而非直接切换
|
return
|
||||||
ctx := context.WithValue(c.Request.Context(), ctxkey.SingleAccountRetry, true)
|
default: // FailoverExhausted
|
||||||
c.Request = c.Request.WithContext(ctx)
|
h.handleGeminiFailoverExhausted(c, fs.LastFailoverErr)
|
||||||
continue
|
return
|
||||||
}
|
|
||||||
}
|
}
|
||||||
h.handleGeminiFailoverExhausted(c, lastFailoverErr)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
account := selection.Account
|
account := selection.Account
|
||||||
setOpsSelectedAccount(c, account.ID)
|
setOpsSelectedAccount(c, account.ID)
|
||||||
@@ -429,8 +422,8 @@ func (h *GatewayHandler) GeminiV1BetaModels(c *gin.Context) {
|
|||||||
// 5) forward (根据平台分流)
|
// 5) forward (根据平台分流)
|
||||||
var result *service.ForwardResult
|
var result *service.ForwardResult
|
||||||
requestCtx := c.Request.Context()
|
requestCtx := c.Request.Context()
|
||||||
if switchCount > 0 {
|
if fs.SwitchCount > 0 {
|
||||||
requestCtx = context.WithValue(requestCtx, ctxkey.AccountSwitchCount, switchCount)
|
requestCtx = context.WithValue(requestCtx, ctxkey.AccountSwitchCount, fs.SwitchCount)
|
||||||
}
|
}
|
||||||
if account.Platform == service.PlatformAntigravity && account.Type != service.AccountTypeAPIKey {
|
if account.Platform == service.PlatformAntigravity && account.Type != service.AccountTypeAPIKey {
|
||||||
result, err = h.antigravityGatewayService.ForwardGemini(requestCtx, c, account, modelName, action, stream, body, hasBoundSession)
|
result, err = h.antigravityGatewayService.ForwardGemini(requestCtx, c, account, modelName, action, stream, body, hasBoundSession)
|
||||||
@@ -443,24 +436,16 @@ func (h *GatewayHandler) GeminiV1BetaModels(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
var failoverErr *service.UpstreamFailoverError
|
var failoverErr *service.UpstreamFailoverError
|
||||||
if errors.As(err, &failoverErr) {
|
if errors.As(err, &failoverErr) {
|
||||||
failedAccountIDs[account.ID] = struct{}{}
|
action := fs.HandleFailoverError(c.Request.Context(), h.gatewayService, account.ID, account.Platform, failoverErr)
|
||||||
if needForceCacheBilling(hasBoundSession, failoverErr) {
|
switch action {
|
||||||
forceCacheBilling = true
|
case FailoverContinue:
|
||||||
}
|
continue
|
||||||
if switchCount >= maxAccountSwitches {
|
case FailoverExhausted:
|
||||||
lastFailoverErr = failoverErr
|
h.handleGeminiFailoverExhausted(c, fs.LastFailoverErr)
|
||||||
h.handleGeminiFailoverExhausted(c, lastFailoverErr)
|
return
|
||||||
|
case FailoverCanceled:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
lastFailoverErr = failoverErr
|
|
||||||
switchCount++
|
|
||||||
log.Printf("Gemini account %d: upstream error %d, switching account %d/%d", account.ID, failoverErr.StatusCode, switchCount, maxAccountSwitches)
|
|
||||||
if account.Platform == service.PlatformAntigravity {
|
|
||||||
if !sleepFailoverDelay(c.Request.Context(), switchCount) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
// ForwardNative already wrote the response
|
// ForwardNative already wrote the response
|
||||||
log.Printf("Gemini native forward failed: %v", err)
|
log.Printf("Gemini native forward failed: %v", err)
|
||||||
@@ -506,7 +491,7 @@ func (h *GatewayHandler) GeminiV1BetaModels(c *gin.Context) {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Printf("Record usage failed: %v", err)
|
log.Printf("Record usage failed: %v", err)
|
||||||
}
|
}
|
||||||
}(result, account, userAgent, clientIP, forceCacheBilling)
|
}(result, account, userAgent, clientIP, fs.ForceCacheBilling)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1372,6 +1372,10 @@ func (s *AntigravityGatewayService) Forward(ctx context.Context, c *gin.Context,
|
|||||||
ForceCacheBilling: switchErr.IsStickySession,
|
ForceCacheBilling: switchErr.IsStickySession,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 区分客户端取消和真正的上游失败,返回更准确的错误消息
|
||||||
|
if c.Request.Context().Err() != nil {
|
||||||
|
return nil, s.writeClaudeError(c, http.StatusBadGateway, "client_disconnected", "Client disconnected before upstream response")
|
||||||
|
}
|
||||||
return nil, s.writeClaudeError(c, http.StatusBadGateway, "upstream_error", "Upstream request failed after retries")
|
return nil, s.writeClaudeError(c, http.StatusBadGateway, "upstream_error", "Upstream request failed after retries")
|
||||||
}
|
}
|
||||||
resp := result.resp
|
resp := result.resp
|
||||||
@@ -2044,6 +2048,10 @@ func (s *AntigravityGatewayService) ForwardGemini(ctx context.Context, c *gin.Co
|
|||||||
ForceCacheBilling: switchErr.IsStickySession,
|
ForceCacheBilling: switchErr.IsStickySession,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 区分客户端取消和真正的上游失败,返回更准确的错误消息
|
||||||
|
if c.Request.Context().Err() != nil {
|
||||||
|
return nil, s.writeGoogleError(c, http.StatusBadGateway, "Client disconnected before upstream response")
|
||||||
|
}
|
||||||
return nil, s.writeGoogleError(c, http.StatusBadGateway, "Upstream request failed after retries")
|
return nil, s.writeGoogleError(c, http.StatusBadGateway, "Upstream request failed after retries")
|
||||||
}
|
}
|
||||||
resp := result.resp
|
resp := result.resp
|
||||||
|
|||||||
@@ -220,7 +220,7 @@ func TestApplyErrorPassthroughRule_SkipMonitoringSetsContextKey(t *testing.T) {
|
|||||||
v, exists := c.Get(OpsSkipPassthroughKey)
|
v, exists := c.Get(OpsSkipPassthroughKey)
|
||||||
assert.True(t, exists, "OpsSkipPassthroughKey should be set when skip_monitoring=true")
|
assert.True(t, exists, "OpsSkipPassthroughKey should be set when skip_monitoring=true")
|
||||||
boolVal, ok := v.(bool)
|
boolVal, ok := v.(bool)
|
||||||
assert.True(t, ok, "value should be bool")
|
assert.True(t, ok, "value should be a bool")
|
||||||
assert.True(t, boolVal)
|
assert.True(t, boolVal)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -344,8 +344,16 @@ func (s *OpsService) getUsersLoadMapBestEffort(ctx context.Context, users []User
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserConcurrencyStats returns real-time concurrency usage for all active users.
|
// GetUserConcurrencyStats returns real-time concurrency usage for active users.
|
||||||
func (s *OpsService) GetUserConcurrencyStats(ctx context.Context) (map[int64]*UserConcurrencyInfo, *time.Time, error) {
|
//
|
||||||
|
// Optional filters:
|
||||||
|
// - platformFilter: only include users who have access to groups belonging to that platform
|
||||||
|
// - groupIDFilter: only include users who have access to that specific group
|
||||||
|
func (s *OpsService) GetUserConcurrencyStats(
|
||||||
|
ctx context.Context,
|
||||||
|
platformFilter string,
|
||||||
|
groupIDFilter *int64,
|
||||||
|
) (map[int64]*UserConcurrencyInfo, *time.Time, error) {
|
||||||
if err := s.RequireMonitoringEnabled(ctx); err != nil {
|
if err := s.RequireMonitoringEnabled(ctx); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -355,6 +363,15 @@ func (s *OpsService) GetUserConcurrencyStats(ctx context.Context) (map[int64]*Us
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build a set of allowed group IDs when filtering is requested.
|
||||||
|
var allowedGroupIDs map[int64]struct{}
|
||||||
|
if platformFilter != "" || (groupIDFilter != nil && *groupIDFilter > 0) {
|
||||||
|
allowedGroupIDs, err = s.buildAllowedGroupIDsForFilter(ctx, platformFilter, groupIDFilter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
collectedAt := time.Now()
|
collectedAt := time.Now()
|
||||||
loadMap := s.getUsersLoadMapBestEffort(ctx, users)
|
loadMap := s.getUsersLoadMapBestEffort(ctx, users)
|
||||||
|
|
||||||
@@ -365,6 +382,12 @@ func (s *OpsService) GetUserConcurrencyStats(ctx context.Context) (map[int64]*Us
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply group/platform filter: skip users whose AllowedGroups
|
||||||
|
// have no intersection with the matching group IDs.
|
||||||
|
if allowedGroupIDs != nil && !userMatchesGroupFilter(u.AllowedGroups, allowedGroupIDs) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
load := loadMap[u.ID]
|
load := loadMap[u.ID]
|
||||||
currentInUse := int64(0)
|
currentInUse := int64(0)
|
||||||
waiting := int64(0)
|
waiting := int64(0)
|
||||||
@@ -394,3 +417,46 @@ func (s *OpsService) GetUserConcurrencyStats(ctx context.Context) (map[int64]*Us
|
|||||||
|
|
||||||
return result, &collectedAt, nil
|
return result, &collectedAt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// buildAllowedGroupIDsForFilter returns the set of group IDs that match the given
|
||||||
|
// platform and/or group ID filter. It reuses listAllAccountsForOps (which already
|
||||||
|
// supports platform filtering at the DB level) to collect group IDs from accounts.
|
||||||
|
func (s *OpsService) buildAllowedGroupIDsForFilter(ctx context.Context, platformFilter string, groupIDFilter *int64) (map[int64]struct{}, error) {
|
||||||
|
// Fast path: only group ID filter, no platform filter needed.
|
||||||
|
if platformFilter == "" && groupIDFilter != nil && *groupIDFilter > 0 {
|
||||||
|
return map[int64]struct{}{*groupIDFilter: {}}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the same account-based approach as GetConcurrencyStats to collect group IDs.
|
||||||
|
accounts, err := s.listAllAccountsForOps(ctx, platformFilter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
groupIDs := make(map[int64]struct{})
|
||||||
|
for _, acc := range accounts {
|
||||||
|
for _, grp := range acc.Groups {
|
||||||
|
if grp == nil || grp.ID <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// If groupIDFilter is set, only include that specific group.
|
||||||
|
if groupIDFilter != nil && *groupIDFilter > 0 && grp.ID != *groupIDFilter {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
groupIDs[grp.ID] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return groupIDs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// userMatchesGroupFilter returns true if the user's AllowedGroups contains
|
||||||
|
// at least one group ID in the allowed set.
|
||||||
|
func userMatchesGroupFilter(userGroups []int64, allowedGroupIDs map[int64]struct{}) bool {
|
||||||
|
for _, gid := range userGroups {
|
||||||
|
if _, ok := allowedGroupIDs[gid]; ok {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -47,13 +47,15 @@ services:
|
|||||||
|
|
||||||
# =======================================================================
|
# =======================================================================
|
||||||
# Database Configuration (PostgreSQL)
|
# Database Configuration (PostgreSQL)
|
||||||
|
# Default: uses local postgres container
|
||||||
|
# External DB: set DATABASE_HOST and DATABASE_SSLMODE in .env
|
||||||
# =======================================================================
|
# =======================================================================
|
||||||
- DATABASE_HOST=postgres
|
- DATABASE_HOST=${DATABASE_HOST:-postgres}
|
||||||
- DATABASE_PORT=5432
|
- DATABASE_PORT=${DATABASE_PORT:-5432}
|
||||||
- DATABASE_USER=${POSTGRES_USER:-sub2api}
|
- DATABASE_USER=${POSTGRES_USER:-sub2api}
|
||||||
- DATABASE_PASSWORD=${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
|
- DATABASE_PASSWORD=${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
|
||||||
- DATABASE_DBNAME=${POSTGRES_DB:-sub2api}
|
- DATABASE_DBNAME=${POSTGRES_DB:-sub2api}
|
||||||
- DATABASE_SSLMODE=disable
|
- DATABASE_SSLMODE=${DATABASE_SSLMODE:-disable}
|
||||||
|
|
||||||
# =======================================================================
|
# =======================================================================
|
||||||
# Redis Configuration
|
# Redis Configuration
|
||||||
@@ -128,8 +130,6 @@ services:
|
|||||||
# Examples: http://host:port, socks5://host:port
|
# Examples: http://host:port, socks5://host:port
|
||||||
- UPDATE_PROXY_URL=${UPDATE_PROXY_URL:-}
|
- UPDATE_PROXY_URL=${UPDATE_PROXY_URL:-}
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
|
||||||
condition: service_healthy
|
|
||||||
redis:
|
redis:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
networks:
|
networks:
|
||||||
@@ -141,35 +141,6 @@ services:
|
|||||||
retries: 3
|
retries: 3
|
||||||
start_period: 30s
|
start_period: 30s
|
||||||
|
|
||||||
# ===========================================================================
|
|
||||||
# PostgreSQL Database
|
|
||||||
# ===========================================================================
|
|
||||||
postgres:
|
|
||||||
image: postgres:18-alpine
|
|
||||||
container_name: sub2api-postgres
|
|
||||||
restart: unless-stopped
|
|
||||||
ulimits:
|
|
||||||
nofile:
|
|
||||||
soft: 100000
|
|
||||||
hard: 100000
|
|
||||||
volumes:
|
|
||||||
- postgres_data:/var/lib/postgresql/data
|
|
||||||
environment:
|
|
||||||
- POSTGRES_USER=${POSTGRES_USER:-sub2api}
|
|
||||||
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
|
|
||||||
- POSTGRES_DB=${POSTGRES_DB:-sub2api}
|
|
||||||
- TZ=${TZ:-Asia/Shanghai}
|
|
||||||
networks:
|
|
||||||
- sub2api-network
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-sub2api} -d ${POSTGRES_DB:-sub2api}"]
|
|
||||||
interval: 10s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
start_period: 10s
|
|
||||||
# 注意:不暴露端口到宿主机,应用通过内部网络连接
|
|
||||||
# 如需调试,可临时添加:ports: ["127.0.0.1:5433:5432"]
|
|
||||||
|
|
||||||
# ===========================================================================
|
# ===========================================================================
|
||||||
# Redis Cache
|
# Redis Cache
|
||||||
# ===========================================================================
|
# ===========================================================================
|
||||||
@@ -209,8 +180,6 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
sub2api_data:
|
sub2api_data:
|
||||||
driver: local
|
driver: local
|
||||||
postgres_data:
|
|
||||||
driver: local
|
|
||||||
redis_data:
|
redis_data:
|
||||||
driver: local
|
driver: local
|
||||||
|
|
||||||
|
|||||||
BIN
frontend/public/wechat-qr.jpg
Normal file
BIN
frontend/public/wechat-qr.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 148 KiB |
@@ -366,8 +366,16 @@ export async function getConcurrencyStats(platform?: string, groupId?: number |
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUserConcurrencyStats(): Promise<OpsUserConcurrencyStatsResponse> {
|
export async function getUserConcurrencyStats(platform?: string, groupId?: number | null): Promise<OpsUserConcurrencyStatsResponse> {
|
||||||
const { data } = await apiClient.get<OpsUserConcurrencyStatsResponse>('/admin/ops/user-concurrency')
|
const params: Record<string, any> = {}
|
||||||
|
if (platform) {
|
||||||
|
params.platform = platform
|
||||||
|
}
|
||||||
|
if (typeof groupId === 'number' && groupId > 0) {
|
||||||
|
params.group_id = groupId
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await apiClient.get<OpsUserConcurrencyStatsResponse>('/admin/ops/user-concurrency', { params })
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -77,7 +77,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Model Rate Limit Indicators (Antigravity OAuth Smart Retry) -->
|
<!-- Model Rate Limit Indicators (Antigravity OAuth Smart Retry) -->
|
||||||
<template v-if="activeModelRateLimits.length > 0">
|
<div v-if="activeModelRateLimits.length > 0" class="grid grid-cols-3 gap-1">
|
||||||
<div v-for="item in activeModelRateLimits" :key="item.model" class="group relative">
|
<div v-for="item in activeModelRateLimits" :key="item.model" class="group relative">
|
||||||
<span
|
<span
|
||||||
class="inline-flex items-center gap-1 rounded bg-purple-100 px-1.5 py-0.5 text-xs font-medium text-purple-700 dark:bg-purple-900/30 dark:text-purple-400"
|
class="inline-flex items-center gap-1 rounded bg-purple-100 px-1.5 py-0.5 text-xs font-medium text-purple-700 dark:bg-purple-900/30 dark:text-purple-400"
|
||||||
@@ -95,7 +95,7 @@
|
|||||||
></div>
|
></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</div>
|
||||||
|
|
||||||
<!-- Overload Indicator (529) -->
|
<!-- Overload Indicator (529) -->
|
||||||
<div v-if="isOverloaded" class="group relative">
|
<div v-if="isOverloaded" class="group relative">
|
||||||
|
|||||||
104
frontend/src/components/common/WechatServiceButton.vue
Normal file
104
frontend/src/components/common/WechatServiceButton.vue
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
<template>
|
||||||
|
<!-- 悬浮按钮 - 使用主题色 -->
|
||||||
|
<button
|
||||||
|
@click="showModal = true"
|
||||||
|
class="fixed bottom-6 right-6 z-50 flex items-center gap-2 rounded-full bg-gradient-to-r from-primary-500 to-primary-600 px-4 py-3 text-white shadow-lg shadow-primary-500/25 transition-all hover:from-primary-600 hover:to-primary-700 hover:shadow-xl hover:shadow-primary-500/30"
|
||||||
|
>
|
||||||
|
<svg class="h-5 w-5" viewBox="0 0 24 24" fill="currentColor">
|
||||||
|
<path d="M8.691 2.188C3.891 2.188 0 5.476 0 9.53c0 2.212 1.17 4.203 3.002 5.55a.59.59 0 01.213.665l-.39 1.48c-.019.07-.048.141-.048.213 0 .163.13.295.29.295a.328.328 0 00.186-.059l2.114-1.225a.87.87 0 01.415-.106.807.807 0 01.213.026 10.07 10.07 0 002.696.37c.262 0 .52-.011.776-.028a5.91 5.91 0 01-.193-1.479c0-3.644 3.374-6.6 7.536-6.6.262 0 .52.011.776.028-.628-3.513-4.27-6.472-8.885-6.472zM5.785 5.97a1.1 1.1 0 110 2.2 1.1 1.1 0 010-2.2zm5.813 0a1.1 1.1 0 110 2.2 1.1 1.1 0 010-2.2zm5.192 2.642c-3.703 0-6.71 2.567-6.71 5.73 0 3.163 3.007 5.73 6.71 5.73a7.9 7.9 0 002.126-.288.644.644 0 01.17-.022.69.69 0 01.329.085l1.672.97a.262.262 0 00.147.046c.128 0 .23-.104.23-.233a.403.403 0 00-.038-.168l-.309-1.17a.468.468 0 01.168-.527c1.449-1.065 2.374-2.643 2.374-4.423 0-3.163-3.007-5.73-6.71-5.73h-.159zm-2.434 3.34a.88.88 0 110 1.76.88.88 0 010-1.76zm4.868 0a.88.88 0 110 1.76.88.88 0 010-1.76z"/>
|
||||||
|
</svg>
|
||||||
|
<span class="text-sm font-medium">客服</span>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<!-- 弹窗 -->
|
||||||
|
<Teleport to="body">
|
||||||
|
<Transition name="fade">
|
||||||
|
<div
|
||||||
|
v-if="showModal"
|
||||||
|
class="fixed inset-0 z-[100] flex items-center justify-center bg-black/50 p-4 backdrop-blur-sm"
|
||||||
|
@click.self="showModal = false"
|
||||||
|
>
|
||||||
|
<Transition name="scale">
|
||||||
|
<div
|
||||||
|
v-if="showModal"
|
||||||
|
class="relative w-full max-w-sm rounded-2xl bg-white p-6 shadow-2xl dark:bg-dark-700"
|
||||||
|
>
|
||||||
|
<!-- 关闭按钮 -->
|
||||||
|
<button
|
||||||
|
@click="showModal = false"
|
||||||
|
class="absolute right-4 top-4 text-gray-400 transition-colors hover:text-gray-600 dark:text-dark-400 dark:hover:text-dark-200"
|
||||||
|
>
|
||||||
|
<svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<!-- 标题 -->
|
||||||
|
<div class="mb-4 flex items-center gap-3">
|
||||||
|
<div class="flex h-10 w-10 items-center justify-center rounded-full bg-gradient-to-br from-primary-500 to-primary-600">
|
||||||
|
<svg class="h-6 w-6 text-white" viewBox="0 0 24 24" fill="currentColor">
|
||||||
|
<path d="M8.691 2.188C3.891 2.188 0 5.476 0 9.53c0 2.212 1.17 4.203 3.002 5.55a.59.59 0 01.213.665l-.39 1.48c-.019.07-.048.141-.048.213 0 .163.13.295.29.295a.328.328 0 00.186-.059l2.114-1.225a.87.87 0 01.415-.106.807.807 0 01.213.026 10.07 10.07 0 002.696.37c.262 0 .52-.011.776-.028a5.91 5.91 0 01-.193-1.479c0-3.644 3.374-6.6 7.536-6.6.262 0 .52.011.776.028-.628-3.513-4.27-6.472-8.885-6.472zM5.785 5.97a1.1 1.1 0 110 2.2 1.1 1.1 0 010-2.2zm5.813 0a1.1 1.1 0 110 2.2 1.1 1.1 0 010-2.2zm5.192 2.642c-3.703 0-6.71 2.567-6.71 5.73 0 3.163 3.007 5.73 6.71 5.73a7.9 7.9 0 002.126-.288.644.644 0 01.17-.022.69.69 0 01.329.085l1.672.97a.262.262 0 00.147.046c.128 0 .23-.104.23-.233a.403.403 0 00-.038-.168l-.309-1.17a.468.468 0 01.168-.527c1.449-1.065 2.374-2.643 2.374-4.423 0-3.163-3.007-5.73-6.71-5.73h-.159zm-2.434 3.34a.88.88 0 110 1.76.88.88 0 010-1.76zm4.868 0a.88.88 0 110 1.76.88.88 0 010-1.76z"/>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h3 class="text-lg font-semibold text-gray-900 dark:text-white">联系客服</h3>
|
||||||
|
<p class="text-sm text-gray-500 dark:text-dark-400">扫码添加好友</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 二维码卡片 -->
|
||||||
|
<div class="mb-4 overflow-hidden rounded-xl border border-primary-100 bg-gradient-to-br from-primary-50 to-white p-3 dark:border-primary-800/30 dark:from-primary-900/10 dark:to-dark-800">
|
||||||
|
<img
|
||||||
|
src="/wechat-qr.jpg"
|
||||||
|
alt="微信二维码"
|
||||||
|
class="w-full rounded-lg"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 提示文字 -->
|
||||||
|
<div class="text-center">
|
||||||
|
<p class="mb-2 text-sm font-medium text-primary-600 dark:text-primary-400">
|
||||||
|
微信扫码添加客服
|
||||||
|
</p>
|
||||||
|
<p class="flex items-center justify-center gap-1 text-xs text-gray-500 dark:text-dark-400">
|
||||||
|
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
|
||||||
|
</svg>
|
||||||
|
工作时间:周一至周五 9:00-18:00
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Transition>
|
||||||
|
</div>
|
||||||
|
</Transition>
|
||||||
|
</Teleport>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
const showModal = ref(false)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.fade-enter-active,
|
||||||
|
.fade-leave-active {
|
||||||
|
transition: opacity 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.fade-enter-from,
|
||||||
|
.fade-leave-to {
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.scale-enter-active,
|
||||||
|
.scale-leave-active {
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.scale-enter-from,
|
||||||
|
.scale-leave-to {
|
||||||
|
opacity: 0;
|
||||||
|
transform: scale(0.95);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -121,23 +121,6 @@
|
|||||||
<Icon name="key" size="sm" />
|
<Icon name="key" size="sm" />
|
||||||
{{ t('nav.apiKeys') }}
|
{{ t('nav.apiKeys') }}
|
||||||
</router-link>
|
</router-link>
|
||||||
|
|
||||||
<a
|
|
||||||
href="https://github.com/Wei-Shaw/sub2api"
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
|
||||||
@click="closeDropdown"
|
|
||||||
class="dropdown-item"
|
|
||||||
>
|
|
||||||
<svg class="h-4 w-4" fill="currentColor" viewBox="0 0 24 24">
|
|
||||||
<path
|
|
||||||
fill-rule="evenodd"
|
|
||||||
clip-rule="evenodd"
|
|
||||||
d="M12 2C6.477 2 2 6.477 2 12c0 4.42 2.865 8.17 6.839 9.49.5.092.682-.217.682-.482 0-.237-.008-.866-.013-1.7-2.782.604-3.369-1.34-3.369-1.34-.454-1.156-1.11-1.464-1.11-1.464-.908-.62.069-.608.069-.608 1.003.07 1.531 1.03 1.531 1.03.892 1.529 2.341 1.087 2.91.831.092-.646.35-1.086.636-1.336-2.22-.253-4.555-1.11-4.555-4.943 0-1.091.39-1.984 1.029-2.683-.103-.253-.446-1.27.098-2.647 0 0 .84-.269 2.75 1.025A9.578 9.578 0 0112 6.836c.85.004 1.705.114 2.504.336 1.909-1.294 2.747-1.025 2.747-1.025.546 1.377.203 2.394.1 2.647.64.699 1.028 1.592 1.028 2.683 0 3.842-2.339 4.687-4.566 4.935.359.309.678.919.678 1.852 0 1.336-.012 2.415-.012 2.743 0 .267.18.578.688.48C19.138 20.167 22 16.418 22 12c0-5.523-4.477-10-10-10z"
|
|
||||||
/>
|
|
||||||
</svg>
|
|
||||||
{{ t('nav.github') }}
|
|
||||||
</a>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Contact Support (only show if configured) -->
|
<!-- Contact Support (only show if configured) -->
|
||||||
|
|||||||
@@ -122,8 +122,11 @@
|
|||||||
>
|
>
|
||||||
{{ siteName }}
|
{{ siteName }}
|
||||||
</h1>
|
</h1>
|
||||||
<p class="mb-8 text-lg text-gray-600 dark:text-dark-300 md:text-xl">
|
<p class="mb-3 text-xl font-semibold text-primary-600 dark:text-primary-400 md:text-2xl">
|
||||||
{{ siteSubtitle }}
|
{{ t('home.heroSubtitle') }}
|
||||||
|
</p>
|
||||||
|
<p class="mb-8 text-base text-gray-600 dark:text-dark-300 md:text-lg">
|
||||||
|
{{ t('home.heroDescription') }}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<!-- CTA Button -->
|
<!-- CTA Button -->
|
||||||
@@ -177,7 +180,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Feature Tags - Centered -->
|
<!-- Feature Tags - Centered -->
|
||||||
<div class="mb-12 flex flex-wrap items-center justify-center gap-4 md:gap-6">
|
<div class="mb-16 flex flex-wrap items-center justify-center gap-4 md:gap-6">
|
||||||
<div
|
<div
|
||||||
class="inline-flex items-center gap-2.5 rounded-full border border-gray-200/50 bg-white/80 px-5 py-2.5 shadow-sm backdrop-blur-sm dark:border-dark-700/50 dark:bg-dark-800/80"
|
class="inline-flex items-center gap-2.5 rounded-full border border-gray-200/50 bg-white/80 px-5 py-2.5 shadow-sm backdrop-blur-sm dark:border-dark-700/50 dark:bg-dark-800/80"
|
||||||
>
|
>
|
||||||
@@ -204,6 +207,63 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Pain Points Section -->
|
||||||
|
<div class="mb-16">
|
||||||
|
<h2 class="mb-8 text-center text-2xl font-bold text-gray-900 dark:text-white md:text-3xl">
|
||||||
|
{{ t('home.painPoints.title') }}
|
||||||
|
</h2>
|
||||||
|
<div class="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
||||||
|
<!-- Pain Point 1: Expensive -->
|
||||||
|
<div class="rounded-xl border border-red-200/50 bg-red-50/50 p-5 dark:border-red-900/30 dark:bg-red-950/20">
|
||||||
|
<div class="mb-3 flex h-10 w-10 items-center justify-center rounded-lg bg-red-100 dark:bg-red-900/30">
|
||||||
|
<svg class="h-5 w-5 text-red-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<h3 class="mb-1.5 font-semibold text-gray-900 dark:text-white">{{ t('home.painPoints.items.expensive.title') }}</h3>
|
||||||
|
<p class="text-sm text-gray-600 dark:text-dark-400">{{ t('home.painPoints.items.expensive.desc') }}</p>
|
||||||
|
</div>
|
||||||
|
<!-- Pain Point 2: Complex -->
|
||||||
|
<div class="rounded-xl border border-orange-200/50 bg-orange-50/50 p-5 dark:border-orange-900/30 dark:bg-orange-950/20">
|
||||||
|
<div class="mb-3 flex h-10 w-10 items-center justify-center rounded-lg bg-orange-100 dark:bg-orange-900/30">
|
||||||
|
<svg class="h-5 w-5 text-orange-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<h3 class="mb-1.5 font-semibold text-gray-900 dark:text-white">{{ t('home.painPoints.items.complex.title') }}</h3>
|
||||||
|
<p class="text-sm text-gray-600 dark:text-dark-400">{{ t('home.painPoints.items.complex.desc') }}</p>
|
||||||
|
</div>
|
||||||
|
<!-- Pain Point 3: Unstable -->
|
||||||
|
<div class="rounded-xl border border-yellow-200/50 bg-yellow-50/50 p-5 dark:border-yellow-900/30 dark:bg-yellow-950/20">
|
||||||
|
<div class="mb-3 flex h-10 w-10 items-center justify-center rounded-lg bg-yellow-100 dark:bg-yellow-900/30">
|
||||||
|
<svg class="h-5 w-5 text-yellow-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<h3 class="mb-1.5 font-semibold text-gray-900 dark:text-white">{{ t('home.painPoints.items.unstable.title') }}</h3>
|
||||||
|
<p class="text-sm text-gray-600 dark:text-dark-400">{{ t('home.painPoints.items.unstable.desc') }}</p>
|
||||||
|
</div>
|
||||||
|
<!-- Pain Point 4: No Control -->
|
||||||
|
<div class="rounded-xl border border-gray-200/50 bg-gray-50/50 p-5 dark:border-dark-700/50 dark:bg-dark-800/50">
|
||||||
|
<div class="mb-3 flex h-10 w-10 items-center justify-center rounded-lg bg-gray-100 dark:bg-dark-700">
|
||||||
|
<svg class="h-5 w-5 text-gray-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" d="M18.364 18.364A9 9 0 005.636 5.636m12.728 12.728A9 9 0 015.636 5.636m12.728 12.728L5.636 5.636" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<h3 class="mb-1.5 font-semibold text-gray-900 dark:text-white">{{ t('home.painPoints.items.noControl.title') }}</h3>
|
||||||
|
<p class="text-sm text-gray-600 dark:text-dark-400">{{ t('home.painPoints.items.noControl.desc') }}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Solutions Section Title -->
|
||||||
|
<div class="mb-8 text-center">
|
||||||
|
<h2 class="mb-2 text-2xl font-bold text-gray-900 dark:text-white md:text-3xl">
|
||||||
|
{{ t('home.solutions.title') }}
|
||||||
|
</h2>
|
||||||
|
<p class="text-gray-600 dark:text-dark-400">{{ t('home.solutions.subtitle') }}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Features Grid -->
|
<!-- Features Grid -->
|
||||||
<div class="mb-12 grid gap-6 md:grid-cols-3">
|
<div class="mb-12 grid gap-6 md:grid-cols-3">
|
||||||
<!-- Feature 1: Unified Gateway -->
|
<!-- Feature 1: Unified Gateway -->
|
||||||
@@ -369,6 +429,77 @@
|
|||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Comparison Table -->
|
||||||
|
<div class="mb-16">
|
||||||
|
<h2 class="mb-8 text-center text-2xl font-bold text-gray-900 dark:text-white md:text-3xl">
|
||||||
|
{{ t('home.comparison.title') }}
|
||||||
|
</h2>
|
||||||
|
<div class="overflow-x-auto">
|
||||||
|
<table class="w-full rounded-xl border border-gray-200/50 bg-white/60 backdrop-blur-sm dark:border-dark-700/50 dark:bg-dark-800/60">
|
||||||
|
<thead>
|
||||||
|
<tr class="border-b border-gray-200/50 dark:border-dark-700/50">
|
||||||
|
<th class="px-6 py-4 text-left text-sm font-semibold text-gray-900 dark:text-white">{{ t('home.comparison.headers.feature') }}</th>
|
||||||
|
<th class="px-6 py-4 text-center text-sm font-semibold text-gray-500 dark:text-dark-400">{{ t('home.comparison.headers.official') }}</th>
|
||||||
|
<th class="px-6 py-4 text-center text-sm font-semibold text-primary-600 dark:text-primary-400">{{ t('home.comparison.headers.us') }}</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody class="divide-y divide-gray-200/50 dark:divide-dark-700/50">
|
||||||
|
<tr>
|
||||||
|
<td class="px-6 py-4 text-sm font-medium text-gray-900 dark:text-white">{{ t('home.comparison.items.pricing.feature') }}</td>
|
||||||
|
<td class="px-6 py-4 text-center text-sm text-gray-500 dark:text-dark-400">{{ t('home.comparison.items.pricing.official') }}</td>
|
||||||
|
<td class="px-6 py-4 text-center text-sm font-medium text-primary-600 dark:text-primary-400">{{ t('home.comparison.items.pricing.us') }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td class="px-6 py-4 text-sm font-medium text-gray-900 dark:text-white">{{ t('home.comparison.items.models.feature') }}</td>
|
||||||
|
<td class="px-6 py-4 text-center text-sm text-gray-500 dark:text-dark-400">{{ t('home.comparison.items.models.official') }}</td>
|
||||||
|
<td class="px-6 py-4 text-center text-sm font-medium text-primary-600 dark:text-primary-400">{{ t('home.comparison.items.models.us') }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td class="px-6 py-4 text-sm font-medium text-gray-900 dark:text-white">{{ t('home.comparison.items.management.feature') }}</td>
|
||||||
|
<td class="px-6 py-4 text-center text-sm text-gray-500 dark:text-dark-400">{{ t('home.comparison.items.management.official') }}</td>
|
||||||
|
<td class="px-6 py-4 text-center text-sm font-medium text-primary-600 dark:text-primary-400">{{ t('home.comparison.items.management.us') }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td class="px-6 py-4 text-sm font-medium text-gray-900 dark:text-white">{{ t('home.comparison.items.stability.feature') }}</td>
|
||||||
|
<td class="px-6 py-4 text-center text-sm text-gray-500 dark:text-dark-400">{{ t('home.comparison.items.stability.official') }}</td>
|
||||||
|
<td class="px-6 py-4 text-center text-sm font-medium text-primary-600 dark:text-primary-400">{{ t('home.comparison.items.stability.us') }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td class="px-6 py-4 text-sm font-medium text-gray-900 dark:text-white">{{ t('home.comparison.items.control.feature') }}</td>
|
||||||
|
<td class="px-6 py-4 text-center text-sm text-gray-500 dark:text-dark-400">{{ t('home.comparison.items.control.official') }}</td>
|
||||||
|
<td class="px-6 py-4 text-center text-sm font-medium text-primary-600 dark:text-primary-400">{{ t('home.comparison.items.control.us') }}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- CTA Section -->
|
||||||
|
<div class="mb-8 rounded-2xl bg-gradient-to-r from-primary-500 to-primary-600 p-8 text-center shadow-xl shadow-primary-500/20 md:p-12">
|
||||||
|
<h2 class="mb-3 text-2xl font-bold text-white md:text-3xl">
|
||||||
|
{{ t('home.cta.title') }}
|
||||||
|
</h2>
|
||||||
|
<p class="mb-6 text-primary-100">
|
||||||
|
{{ t('home.cta.description') }}
|
||||||
|
</p>
|
||||||
|
<router-link
|
||||||
|
v-if="!isAuthenticated"
|
||||||
|
to="/register"
|
||||||
|
class="inline-flex items-center gap-2 rounded-full bg-white px-8 py-3 font-semibold text-primary-600 shadow-lg transition-all hover:bg-gray-50 hover:shadow-xl"
|
||||||
|
>
|
||||||
|
{{ t('home.cta.button') }}
|
||||||
|
<Icon name="arrowRight" size="md" :stroke-width="2" />
|
||||||
|
</router-link>
|
||||||
|
<router-link
|
||||||
|
v-else
|
||||||
|
:to="dashboardPath"
|
||||||
|
class="inline-flex items-center gap-2 rounded-full bg-white px-8 py-3 font-semibold text-primary-600 shadow-lg transition-all hover:bg-gray-50 hover:shadow-xl"
|
||||||
|
>
|
||||||
|
{{ t('home.goToDashboard') }}
|
||||||
|
<Icon name="arrowRight" size="md" :stroke-width="2" />
|
||||||
|
</router-link>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
@@ -380,27 +511,20 @@
|
|||||||
<p class="text-sm text-gray-500 dark:text-dark-400">
|
<p class="text-sm text-gray-500 dark:text-dark-400">
|
||||||
© {{ currentYear }} {{ siteName }}. {{ t('home.footer.allRightsReserved') }}
|
© {{ currentYear }} {{ siteName }}. {{ t('home.footer.allRightsReserved') }}
|
||||||
</p>
|
</p>
|
||||||
<div class="flex items-center gap-4">
|
<a
|
||||||
<a
|
v-if="docUrl"
|
||||||
v-if="docUrl"
|
:href="docUrl"
|
||||||
:href="docUrl"
|
target="_blank"
|
||||||
target="_blank"
|
rel="noopener noreferrer"
|
||||||
rel="noopener noreferrer"
|
class="text-sm text-gray-500 transition-colors hover:text-gray-700 dark:text-dark-400 dark:hover:text-white"
|
||||||
class="text-sm text-gray-500 transition-colors hover:text-gray-700 dark:text-dark-400 dark:hover:text-white"
|
>
|
||||||
>
|
{{ t('home.docs') }}
|
||||||
{{ t('home.docs') }}
|
</a>
|
||||||
</a>
|
|
||||||
<a
|
|
||||||
:href="githubUrl"
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
|
||||||
class="text-sm text-gray-500 transition-colors hover:text-gray-700 dark:text-dark-400 dark:hover:text-white"
|
|
||||||
>
|
|
||||||
GitHub
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
|
<!-- 微信客服悬浮按钮 -->
|
||||||
|
<WechatServiceButton />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -410,6 +534,7 @@ import { useI18n } from 'vue-i18n'
|
|||||||
import { useAuthStore, useAppStore } from '@/stores'
|
import { useAuthStore, useAppStore } from '@/stores'
|
||||||
import LocaleSwitcher from '@/components/common/LocaleSwitcher.vue'
|
import LocaleSwitcher from '@/components/common/LocaleSwitcher.vue'
|
||||||
import Icon from '@/components/icons/Icon.vue'
|
import Icon from '@/components/icons/Icon.vue'
|
||||||
|
import WechatServiceButton from '@/components/common/WechatServiceButton.vue'
|
||||||
|
|
||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
|
|
||||||
@@ -419,7 +544,6 @@ const appStore = useAppStore()
|
|||||||
// Site settings - directly from appStore (already initialized from injected config)
|
// Site settings - directly from appStore (already initialized from injected config)
|
||||||
const siteName = computed(() => appStore.cachedPublicSettings?.site_name || appStore.siteName || 'Sub2API')
|
const siteName = computed(() => appStore.cachedPublicSettings?.site_name || appStore.siteName || 'Sub2API')
|
||||||
const siteLogo = computed(() => appStore.cachedPublicSettings?.site_logo || appStore.siteLogo || '')
|
const siteLogo = computed(() => appStore.cachedPublicSettings?.site_logo || appStore.siteLogo || '')
|
||||||
const siteSubtitle = computed(() => appStore.cachedPublicSettings?.site_subtitle || 'AI API Gateway Platform')
|
|
||||||
const docUrl = computed(() => appStore.cachedPublicSettings?.doc_url || appStore.docUrl || '')
|
const docUrl = computed(() => appStore.cachedPublicSettings?.doc_url || appStore.docUrl || '')
|
||||||
const homeContent = computed(() => appStore.cachedPublicSettings?.home_content || '')
|
const homeContent = computed(() => appStore.cachedPublicSettings?.home_content || '')
|
||||||
|
|
||||||
@@ -432,9 +556,6 @@ const isHomeContentUrl = computed(() => {
|
|||||||
// Theme
|
// Theme
|
||||||
const isDark = ref(document.documentElement.classList.contains('dark'))
|
const isDark = ref(document.documentElement.classList.contains('dark'))
|
||||||
|
|
||||||
// GitHub URL
|
|
||||||
const githubUrl = 'https://github.com/Wei-Shaw/sub2api'
|
|
||||||
|
|
||||||
// Auth state
|
// Auth state
|
||||||
const isAuthenticated = computed(() => authStore.isAuthenticated)
|
const isAuthenticated = computed(() => authStore.isAuthenticated)
|
||||||
const isAdmin = computed(() => authStore.isAdmin)
|
const isAdmin = computed(() => authStore.isAdmin)
|
||||||
|
|||||||
@@ -122,6 +122,7 @@ const platformRows = computed((): SummaryRow[] => {
|
|||||||
available_accounts: availableAccounts,
|
available_accounts: availableAccounts,
|
||||||
rate_limited_accounts: safeNumber(avail.rate_limit_count),
|
rate_limited_accounts: safeNumber(avail.rate_limit_count),
|
||||||
|
|
||||||
|
|
||||||
error_accounts: safeNumber(avail.error_count),
|
error_accounts: safeNumber(avail.error_count),
|
||||||
total_concurrency: totalConcurrency,
|
total_concurrency: totalConcurrency,
|
||||||
used_concurrency: usedConcurrency,
|
used_concurrency: usedConcurrency,
|
||||||
@@ -161,7 +162,6 @@ const groupRows = computed((): SummaryRow[] => {
|
|||||||
total_accounts: totalAccounts,
|
total_accounts: totalAccounts,
|
||||||
available_accounts: availableAccounts,
|
available_accounts: availableAccounts,
|
||||||
rate_limited_accounts: safeNumber(avail.rate_limit_count),
|
rate_limited_accounts: safeNumber(avail.rate_limit_count),
|
||||||
|
|
||||||
error_accounts: safeNumber(avail.error_count),
|
error_accounts: safeNumber(avail.error_count),
|
||||||
total_concurrency: totalConcurrency,
|
total_concurrency: totalConcurrency,
|
||||||
used_concurrency: usedConcurrency,
|
used_concurrency: usedConcurrency,
|
||||||
@@ -265,7 +265,7 @@ async function loadData() {
|
|||||||
try {
|
try {
|
||||||
if (showByUser.value) {
|
if (showByUser.value) {
|
||||||
// 用户视图模式只加载用户并发数据
|
// 用户视图模式只加载用户并发数据
|
||||||
const userData = await opsAPI.getUserConcurrencyStats()
|
const userData = await opsAPI.getUserConcurrencyStats(props.platformFilter, props.groupIdFilter)
|
||||||
userConcurrency.value = userData
|
userConcurrency.value = userData
|
||||||
} else {
|
} else {
|
||||||
// 常规模式加载账号/平台/分组数据
|
// 常规模式加载账号/平台/分组数据
|
||||||
@@ -301,6 +301,14 @@ watch(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 过滤条件变化时重新加载数据
|
||||||
|
watch(
|
||||||
|
[() => props.platformFilter, () => props.groupIdFilter],
|
||||||
|
() => {
|
||||||
|
loadData()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
function getLoadBarClass(loadPct: number): string {
|
function getLoadBarClass(loadPct: number): string {
|
||||||
if (loadPct >= 90) return 'bg-red-500 dark:bg-red-600'
|
if (loadPct >= 90) return 'bg-red-500 dark:bg-red-600'
|
||||||
if (loadPct >= 70) return 'bg-orange-500 dark:bg-orange-600'
|
if (loadPct >= 70) return 'bg-orange-500 dark:bg-orange-600'
|
||||||
@@ -329,6 +337,7 @@ function formatDuration(seconds: number): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => realtimeEnabled.value,
|
() => realtimeEnabled.value,
|
||||||
async (enabled) => {
|
async (enabled) => {
|
||||||
|
|||||||
127
stress_test_gemini_session.sh
Normal file
127
stress_test_gemini_session.sh
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Gemini 粘性会话压力测试脚本
|
||||||
|
# 测试目标:验证不同会话分配不同账号,同一会话保持同一账号
|
||||||
|
|
||||||
|
BASE_URL="http://host.clicodeplus.com:8080"
|
||||||
|
API_KEY="sk-32ad0a3197e528c840ea84f0dc6b2056dd3fead03526b5c605a60709bd408f7e"
|
||||||
|
MODEL="gemini-2.5-flash"
|
||||||
|
|
||||||
|
# 创建临时目录存放结果
|
||||||
|
RESULT_DIR="/tmp/gemini_stress_test_$(date +%s)"
|
||||||
|
mkdir -p "$RESULT_DIR"
|
||||||
|
|
||||||
|
echo "=========================================="
|
||||||
|
echo "Gemini 粘性会话压力测试"
|
||||||
|
echo "结果目录: $RESULT_DIR"
|
||||||
|
echo "=========================================="
|
||||||
|
|
||||||
|
# 函数:发送请求并记录
|
||||||
|
send_request() {
|
||||||
|
local session_id=$1
|
||||||
|
local round=$2
|
||||||
|
local system_prompt=$3
|
||||||
|
local contents=$4
|
||||||
|
local output_file="$RESULT_DIR/session_${session_id}_round_${round}.json"
|
||||||
|
|
||||||
|
local request_body=$(cat <<EOF
|
||||||
|
{
|
||||||
|
"systemInstruction": {
|
||||||
|
"parts": [{"text": "$system_prompt"}]
|
||||||
|
},
|
||||||
|
"contents": $contents
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
|
||||||
|
curl -s -X POST "${BASE_URL}/v1beta/models/${MODEL}:generateContent" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "x-goog-api-key: ${API_KEY}" \
|
||||||
|
-d "$request_body" > "$output_file" 2>&1
|
||||||
|
|
||||||
|
echo "[Session $session_id Round $round] 完成"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 会话1:数学计算器(累加序列)
|
||||||
|
run_session_1() {
|
||||||
|
local sys_prompt="你是一个数学计算器,只返回计算结果数字,不要任何解释"
|
||||||
|
|
||||||
|
# Round 1: 1+1=?
|
||||||
|
send_request 1 1 "$sys_prompt" '[{"role":"user","parts":[{"text":"1+1=?"}]}]'
|
||||||
|
|
||||||
|
# Round 2: 继续 2+2=?(累加历史)
|
||||||
|
send_request 1 2 "$sys_prompt" '[{"role":"user","parts":[{"text":"1+1=?"}]},{"role":"model","parts":[{"text":"2"}]},{"role":"user","parts":[{"text":"2+2=?"}]}]'
|
||||||
|
|
||||||
|
# Round 3: 继续 3+3=?
|
||||||
|
send_request 1 3 "$sys_prompt" '[{"role":"user","parts":[{"text":"1+1=?"}]},{"role":"model","parts":[{"text":"2"}]},{"role":"user","parts":[{"text":"2+2=?"}]},{"role":"model","parts":[{"text":"4"}]},{"role":"user","parts":[{"text":"3+3=?"}]}]'
|
||||||
|
|
||||||
|
# Round 4: 批量计算 10+10, 20+20, 30+30
|
||||||
|
send_request 1 4 "$sys_prompt" '[{"role":"user","parts":[{"text":"1+1=?"}]},{"role":"model","parts":[{"text":"2"}]},{"role":"user","parts":[{"text":"2+2=?"}]},{"role":"model","parts":[{"text":"4"}]},{"role":"user","parts":[{"text":"3+3=?"}]},{"role":"model","parts":[{"text":"6"}]},{"role":"user","parts":[{"text":"计算: 10+10=? 20+20=? 30+30=?"}]}]'
|
||||||
|
}
|
||||||
|
|
||||||
|
# 会话2:英文翻译器(不同系统提示词 = 不同会话)
|
||||||
|
run_session_2() {
|
||||||
|
local sys_prompt="你是一个英文翻译器,将中文翻译成英文,只返回翻译结果"
|
||||||
|
|
||||||
|
send_request 2 1 "$sys_prompt" '[{"role":"user","parts":[{"text":"你好"}]}]'
|
||||||
|
send_request 2 2 "$sys_prompt" '[{"role":"user","parts":[{"text":"你好"}]},{"role":"model","parts":[{"text":"Hello"}]},{"role":"user","parts":[{"text":"世界"}]}]'
|
||||||
|
send_request 2 3 "$sys_prompt" '[{"role":"user","parts":[{"text":"你好"}]},{"role":"model","parts":[{"text":"Hello"}]},{"role":"user","parts":[{"text":"世界"}]},{"role":"model","parts":[{"text":"World"}]},{"role":"user","parts":[{"text":"早上好"}]}]'
|
||||||
|
}
|
||||||
|
|
||||||
|
# 会话3:日文翻译器
|
||||||
|
run_session_3() {
|
||||||
|
local sys_prompt="你是一个日文翻译器,将中文翻译成日文,只返回翻译结果"
|
||||||
|
|
||||||
|
send_request 3 1 "$sys_prompt" '[{"role":"user","parts":[{"text":"你好"}]}]'
|
||||||
|
send_request 3 2 "$sys_prompt" '[{"role":"user","parts":[{"text":"你好"}]},{"role":"model","parts":[{"text":"こんにちは"}]},{"role":"user","parts":[{"text":"谢谢"}]}]'
|
||||||
|
send_request 3 3 "$sys_prompt" '[{"role":"user","parts":[{"text":"你好"}]},{"role":"model","parts":[{"text":"こんにちは"}]},{"role":"user","parts":[{"text":"谢谢"}]},{"role":"model","parts":[{"text":"ありがとう"}]},{"role":"user","parts":[{"text":"再见"}]}]'
|
||||||
|
}
|
||||||
|
|
||||||
|
# 会话4:乘法计算器(另一个数学会话,但系统提示词不同)
|
||||||
|
run_session_4() {
|
||||||
|
local sys_prompt="你是一个乘法专用计算器,只计算乘法,返回数字结果"
|
||||||
|
|
||||||
|
send_request 4 1 "$sys_prompt" '[{"role":"user","parts":[{"text":"2*3=?"}]}]'
|
||||||
|
send_request 4 2 "$sys_prompt" '[{"role":"user","parts":[{"text":"2*3=?"}]},{"role":"model","parts":[{"text":"6"}]},{"role":"user","parts":[{"text":"4*5=?"}]}]'
|
||||||
|
send_request 4 3 "$sys_prompt" '[{"role":"user","parts":[{"text":"2*3=?"}]},{"role":"model","parts":[{"text":"6"}]},{"role":"user","parts":[{"text":"4*5=?"}]},{"role":"model","parts":[{"text":"20"}]},{"role":"user","parts":[{"text":"计算: 10*10=? 20*20=?"}]}]'
|
||||||
|
}
|
||||||
|
|
||||||
|
# 会话5:诗人(完全不同的角色)
|
||||||
|
run_session_5() {
|
||||||
|
local sys_prompt="你是一位诗人,用简短的诗句回应每个话题,每次只写一句诗"
|
||||||
|
|
||||||
|
send_request 5 1 "$sys_prompt" '[{"role":"user","parts":[{"text":"春天"}]}]'
|
||||||
|
send_request 5 2 "$sys_prompt" '[{"role":"user","parts":[{"text":"春天"}]},{"role":"model","parts":[{"text":"春风拂面花满枝"}]},{"role":"user","parts":[{"text":"夏天"}]}]'
|
||||||
|
send_request 5 3 "$sys_prompt" '[{"role":"user","parts":[{"text":"春天"}]},{"role":"model","parts":[{"text":"春风拂面花满枝"}]},{"role":"user","parts":[{"text":"夏天"}]},{"role":"model","parts":[{"text":"蝉鸣蛙声伴荷香"}]},{"role":"user","parts":[{"text":"秋天"}]}]'
|
||||||
|
}
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "开始并发测试 5 个独立会话..."
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# 并发运行所有会话
|
||||||
|
run_session_1 &
|
||||||
|
run_session_2 &
|
||||||
|
run_session_3 &
|
||||||
|
run_session_4 &
|
||||||
|
run_session_5 &
|
||||||
|
|
||||||
|
# 等待所有后台任务完成
|
||||||
|
wait
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "=========================================="
|
||||||
|
echo "所有请求完成,结果保存在: $RESULT_DIR"
|
||||||
|
echo "=========================================="
|
||||||
|
|
||||||
|
# 显示结果摘要
|
||||||
|
echo ""
|
||||||
|
echo "响应摘要:"
|
||||||
|
for f in "$RESULT_DIR"/*.json; do
|
||||||
|
filename=$(basename "$f")
|
||||||
|
response=$(cat "$f" | head -c 200)
|
||||||
|
echo "[$filename]: ${response}..."
|
||||||
|
done
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "请检查服务器日志确认账号分配情况"
|
||||||
Reference in New Issue
Block a user