mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-28 07:31:21 +00:00
Adds a use-it-or-lose-it scheduling strategy: prefer accounts whose session window resets soonest, so near-reset accounts get drained first instead of accounts whose reset is still far away. Both schedulers, opt-in, default behavior unchanged: - Anthropic (gateway_service.go): new GatewaySchedulingConfig .PreferSoonestReset flag. When on, the layered load-aware selection inserts a filterBySoonestReset stage (priority -> soonest-reset -> load -> LRU). Accounts with no active SessionWindowEnd are treated as lowest priority; ties fall through to LRU. - OpenAI/Codex (openai_account_scheduler.go): new "reset" score weight in GatewayOpenAIWSSchedulerScoreWeights. Soonest-reset accounts score higher; weight defaults to 0 (no effect). SessionWindowEnd (upstream 5h/quota ResetsAt) is already carried in the scheduler snapshot, so no snapshot changes are needed. Documented in deploy/config.example.yaml. Adds unit tests for the Anthropic filter and the OpenAI reset factor.
78 lines
2.6 KiB
Go
78 lines
2.6 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func openAIResetTestScheduler(reset float64) *defaultOpenAIAccountScheduler {
|
|
cfg := &config.Config{}
|
|
cfg.Gateway.OpenAIWS.SchedulerScoreWeights = config.GatewayOpenAIWSSchedulerScoreWeights{
|
|
Priority: 1.0,
|
|
Load: 1.0,
|
|
Queue: 0.7,
|
|
ErrorRate: 0.8,
|
|
TTFT: 0.5,
|
|
Reset: reset,
|
|
}
|
|
return &defaultOpenAIAccountScheduler{service: &OpenAIGatewayService{cfg: cfg}}
|
|
}
|
|
|
|
func openAIPlanScores(plan openAIAccountLoadPlan) map[int64]float64 {
|
|
scores := make(map[int64]float64, len(plan.candidates))
|
|
for _, c := range plan.candidates {
|
|
scores[c.account.ID] = c.score
|
|
}
|
|
return scores
|
|
}
|
|
|
|
// Reset 权重 > 0 时,会话窗口最早重置的账号应获得更高分。
|
|
func TestBuildOpenAIAccountLoadPlan_ResetWeightPrefersSoonestReset(t *testing.T) {
|
|
now := time.Now()
|
|
soon := now.Add(1 * time.Hour)
|
|
later := now.Add(20 * time.Hour)
|
|
filtered := []*Account{
|
|
{ID: 1, Priority: 0, SessionWindowEnd: &later},
|
|
{ID: 2, Priority: 0, SessionWindowEnd: &soon},
|
|
}
|
|
sched := openAIResetTestScheduler(5.0)
|
|
|
|
plan := sched.buildOpenAIAccountLoadPlan(OpenAIAccountScheduleRequest{}, filtered, map[int64]*AccountLoadInfo{})
|
|
scores := openAIPlanScores(plan)
|
|
require.Greater(t, scores[2], scores[1], "重置时间最早的账号(ID=2)得分更高")
|
|
}
|
|
|
|
// Reset 权重为 0(默认)时,窗口重置时间不应影响打分,保持原有行为。
|
|
func TestBuildOpenAIAccountLoadPlan_ResetWeightZeroNoEffect(t *testing.T) {
|
|
now := time.Now()
|
|
soon := now.Add(1 * time.Hour)
|
|
later := now.Add(20 * time.Hour)
|
|
filtered := []*Account{
|
|
{ID: 1, Priority: 0, SessionWindowEnd: &later},
|
|
{ID: 2, Priority: 0, SessionWindowEnd: &soon},
|
|
}
|
|
sched := openAIResetTestScheduler(0.0)
|
|
|
|
plan := sched.buildOpenAIAccountLoadPlan(OpenAIAccountScheduleRequest{}, filtered, map[int64]*AccountLoadInfo{})
|
|
scores := openAIPlanScores(plan)
|
|
require.Equal(t, scores[1], scores[2], "Reset 权重为 0 时两账号得分相同")
|
|
}
|
|
|
|
// 无活跃窗口的账号 reset 因子为 0,应低于拥有未来窗口的账号。
|
|
func TestBuildOpenAIAccountLoadPlan_ResetWeightIgnoresNilWindow(t *testing.T) {
|
|
now := time.Now()
|
|
soon := now.Add(2 * time.Hour)
|
|
filtered := []*Account{
|
|
{ID: 1, Priority: 0, SessionWindowEnd: nil},
|
|
{ID: 2, Priority: 0, SessionWindowEnd: &soon},
|
|
}
|
|
sched := openAIResetTestScheduler(5.0)
|
|
|
|
plan := sched.buildOpenAIAccountLoadPlan(OpenAIAccountScheduleRequest{}, filtered, map[int64]*AccountLoadInfo{})
|
|
scores := openAIPlanScores(plan)
|
|
require.Greater(t, scores[2], scores[1], "拥有活跃窗口的账号得分高于无窗口账号")
|
|
}
|