mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
7d_oi 窗口耗尽时 persistAnthropicFableWindowLimit 会给账号写入 model_rate_limits["claude-fable-5"],冷却到窗口重置时间(最长 7 天)。 写入之后没有任何路径会撤销它: - samplePassiveUsageFromHeaders / syncActiveToPassive 只更新 passive_usage_7d_oi_utilization,不与 model_rate_limits 联动; - applyAnthropicFableSchedulingThreshold 只设不清; - UpdateSessionWindow 的自动清除只看账号级 RateLimitResetAt, 而窗口耗尽分支刻意不设它(账号对其他模型仍然可调度)。 于是用户用重置卡让上游额度提前归零后,本地仍按旧的 rate_limit_reset_at 返回 503 No available accounts supporting model,只能人工删 extra 或点“清除限流”。 修复: - 上游明确回报 7d_oi utilization 低于阈值(重置卡生效/窗口滚动)、且该窗口未处于 rejected/exceeded 时,撤销窗口耗尽写入的限流;缺失数据一律不动,缺失不等于 0; - 只清 claude-fable-5 这一个 scope(新增 AccountRepository.ClearModelRateLimit, jsonb 单键删除),不影响其他模型与并发新写入的限流; - 只清 reason 为 anthropic_7d_oi_window_exhausted 的记录:credits required 与 管理端阈值来源语义不同,不能被窗口用量顺手清掉; - 写库成功后才同步本实例的账号快照;写库失败保持原样,避免本实例放行而调度快照仍拦截。 两个观测点都接上:请求响应头的被动采样,以及管理端主动刷新额度。
136 lines
3.7 KiB
Go
136 lines
3.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// sessionWindowSyncRepo 记录 syncActiveToPassive 触发的所有写操作。
|
|
type sessionWindowSyncRepo struct {
|
|
AccountRepository
|
|
|
|
mu sync.Mutex
|
|
extraUpdates []map[string]any
|
|
sessionWindowEnds []sessionWindowEndCall
|
|
}
|
|
|
|
type sessionWindowEndCall struct {
|
|
AccountID int64
|
|
End time.Time
|
|
}
|
|
|
|
func (r *sessionWindowSyncRepo) UpdateExtra(_ context.Context, _ int64, updates map[string]any) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
copied := make(map[string]any, len(updates))
|
|
for k, v := range updates {
|
|
copied[k] = v
|
|
}
|
|
r.extraUpdates = append(r.extraUpdates, copied)
|
|
return nil
|
|
}
|
|
|
|
func (r *sessionWindowSyncRepo) UpdateSessionWindowEnd(_ context.Context, id int64, end time.Time) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.sessionWindowEnds = append(r.sessionWindowEnds, sessionWindowEndCall{AccountID: id, End: end})
|
|
return nil
|
|
}
|
|
|
|
func TestEstimateSetupTokenUsage_ExpiredWindowZeroes(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
past := time.Now().Add(-2 * time.Hour)
|
|
svc := &AccountUsageService{}
|
|
info := svc.estimateSetupTokenUsage(&Account{
|
|
SessionWindowEnd: &past,
|
|
Extra: map[string]any{
|
|
"session_window_utilization": 0.53,
|
|
},
|
|
})
|
|
|
|
if info.FiveHour == nil {
|
|
t.Fatal("expected non-nil FiveHour info")
|
|
}
|
|
if info.FiveHour.Utilization != 0 {
|
|
t.Fatalf("expected Utilization=0 for expired window, got %v", info.FiveHour.Utilization)
|
|
}
|
|
if info.FiveHour.ResetsAt != nil {
|
|
t.Fatalf("expected ResetsAt=nil for expired window, got %v", info.FiveHour.ResetsAt)
|
|
}
|
|
if info.FiveHour.RemainingSeconds != 0 {
|
|
t.Fatalf("expected RemainingSeconds=0 for expired window, got %v", info.FiveHour.RemainingSeconds)
|
|
}
|
|
}
|
|
|
|
func TestEstimateSetupTokenUsage_ActiveWindowPreservesUtilization(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
future := time.Now().Add(3 * time.Hour)
|
|
svc := &AccountUsageService{}
|
|
info := svc.estimateSetupTokenUsage(&Account{
|
|
SessionWindowEnd: &future,
|
|
Extra: map[string]any{
|
|
"session_window_utilization": 0.53,
|
|
},
|
|
})
|
|
|
|
if info.FiveHour == nil {
|
|
t.Fatal("expected non-nil FiveHour info")
|
|
}
|
|
if info.FiveHour.Utilization != 53 {
|
|
t.Fatalf("expected Utilization=53, got %v", info.FiveHour.Utilization)
|
|
}
|
|
if info.FiveHour.ResetsAt == nil || !info.FiveHour.ResetsAt.Equal(future) {
|
|
t.Fatalf("expected ResetsAt=%v, got %v", future, info.FiveHour.ResetsAt)
|
|
}
|
|
if info.FiveHour.RemainingSeconds <= 0 {
|
|
t.Fatalf("expected positive RemainingSeconds, got %v", info.FiveHour.RemainingSeconds)
|
|
}
|
|
}
|
|
|
|
func TestSyncActiveToPassive_WritesFiveHourSessionWindowEnd(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &sessionWindowSyncRepo{}
|
|
svc := &AccountUsageService{accountRepo: repo}
|
|
resetsAt := time.Now().Add(3 * time.Hour).UTC().Truncate(time.Second)
|
|
svc.syncActiveToPassive(context.Background(), &Account{ID: 42}, &UsageInfo{
|
|
FiveHour: &UsageProgress{
|
|
Utilization: 53,
|
|
ResetsAt: &resetsAt,
|
|
},
|
|
})
|
|
|
|
repo.mu.Lock()
|
|
defer repo.mu.Unlock()
|
|
if len(repo.sessionWindowEnds) != 1 {
|
|
t.Fatalf("expected 1 UpdateSessionWindowEnd call, got %d", len(repo.sessionWindowEnds))
|
|
}
|
|
call := repo.sessionWindowEnds[0]
|
|
if call.AccountID != 42 {
|
|
t.Fatalf("expected AccountID=42, got %d", call.AccountID)
|
|
}
|
|
if !call.End.Equal(resetsAt) {
|
|
t.Fatalf("expected End=%v, got %v", resetsAt, call.End)
|
|
}
|
|
}
|
|
|
|
func TestSyncActiveToPassive_SkipsSessionWindowEndWhenResetMissing(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &sessionWindowSyncRepo{}
|
|
svc := &AccountUsageService{accountRepo: repo}
|
|
svc.syncActiveToPassive(context.Background(), &Account{ID: 99}, &UsageInfo{
|
|
FiveHour: &UsageProgress{Utilization: 10},
|
|
})
|
|
|
|
repo.mu.Lock()
|
|
defer repo.mu.Unlock()
|
|
if len(repo.sessionWindowEnds) != 0 {
|
|
t.Fatalf("expected no UpdateSessionWindowEnd calls when ResetsAt is nil, got %d", len(repo.sessionWindowEnds))
|
|
}
|
|
}
|