mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-10 01:54:46 +08:00
Squash of 124 commits from the legacy develop branch (develop-0.1.75) onto a clean v0.1.75 upstream base, to simplify future upstream merges. Key changes included: - Refactor scope-level rate limiting to model-level rate limiting - Antigravity gateway service improvements (smart retry, error policy) - Digest session store (flat cache replacing Trie-based store) - Client disconnect detection during streaming - Gemini messages compatibility service enhancements - Scheduler shuffle for thundering herd prevention - Session hash generation improvements - Frontend customizations (WeChat service, HomeView, etc.) - Ops monitoring scope cleanup
54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func normalizeAntigravityModelName(model string) string {
|
|
normalized := strings.ToLower(strings.TrimSpace(model))
|
|
normalized = strings.TrimPrefix(normalized, "models/")
|
|
return normalized
|
|
}
|
|
|
|
// resolveAntigravityModelKey 根据请求的模型名解析限流 key
|
|
// 返回空字符串表示无法解析
|
|
func resolveAntigravityModelKey(requestedModel string) string {
|
|
return normalizeAntigravityModelName(requestedModel)
|
|
}
|
|
|
|
// IsSchedulableForModel 结合模型级限流判断是否可调度。
|
|
// 保持旧签名以兼容既有调用方;默认使用 context.Background()。
|
|
func (a *Account) IsSchedulableForModel(requestedModel string) bool {
|
|
return a.IsSchedulableForModelWithContext(context.Background(), requestedModel)
|
|
}
|
|
|
|
func (a *Account) IsSchedulableForModelWithContext(ctx context.Context, requestedModel string) bool {
|
|
if a == nil {
|
|
return false
|
|
}
|
|
if !a.IsSchedulable() {
|
|
return false
|
|
}
|
|
if a.isModelRateLimitedWithContext(ctx, requestedModel) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// GetRateLimitRemainingTime 获取限流剩余时间(模型级限流)
|
|
// 返回 0 表示未限流或已过期
|
|
func (a *Account) GetRateLimitRemainingTime(requestedModel string) time.Duration {
|
|
return a.GetRateLimitRemainingTimeWithContext(context.Background(), requestedModel)
|
|
}
|
|
|
|
// GetRateLimitRemainingTimeWithContext 获取限流剩余时间(模型级限流)
|
|
// 返回 0 表示未限流或已过期
|
|
func (a *Account) GetRateLimitRemainingTimeWithContext(ctx context.Context, requestedModel string) time.Duration {
|
|
if a == nil {
|
|
return 0
|
|
}
|
|
return a.GetModelRateLimitRemainingTimeWithContext(ctx, requestedModel)
|
|
}
|