mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 15:06:36 +00:00
merge: integrate upstream v0.1.105 into release/custom-0.1.105
Merge upstream v0.1.105 (94 commits) with our customizations from release/custom-0.1.104. Key upstream additions: - TLS fingerprint profile management - OpenAI→Anthropic endpoint compatibility routing - Privacy mode for OpenAI/Antigravity accounts - Requested model tracking in usage logs - Error observability enhancements - Various bug fixes Conflict resolutions (all "keep both sides"): - handler.go/wire.go: added both TLSFingerprintProfile and GDriveOAuth - wire_gen.go: combined internal500CounterCache with TLS fingerprint objects - accounts.ts: kept setPrivacy + affinity functions - BulkEditAccountModal.vue: kept OpenAI WS mode + Allow Overages sections Additional fixes: - gateway_handler_chat_completions.go, gateway_handler_responses.go: added missing sub2apiUserID parameter to SelectAccountWithLoadAwareness - antigravity_internal500_penalty.go: gofmt fix
This commit is contained in:
commit
0393981397
176 files changed
+16218
-834
No files matched your search
@@ -164,6 +164,7 @@ func (r *apiKeyRepository) GetByKeyForAuth(ctx context.Context, key string) (*se
|
||||
group.FieldModelRoutingEnabled,
|
||||
group.FieldModelRouting,
|
||||
group.FieldMcpXMLInject,
|
||||
group.FieldSimulateClaudeMaxEnabled,
|
||||
group.FieldSupportedModelScopes,
|
||||
group.FieldAllowMessagesDispatch,
|
||||
group.FieldDefaultMappedModel,
|
||||
@@ -655,6 +656,7 @@ func groupEntityToService(g *dbent.Group) *service.Group {
|
||||
ModelRouting: g.ModelRouting,
|
||||
ModelRoutingEnabled: g.ModelRoutingEnabled,
|
||||
MCPXMLInject: g.McpXMLInject,
|
||||
SimulateClaudeMaxEnabled: g.SimulateClaudeMaxEnabled,
|
||||
SupportedModelScopes: g.SupportedModelScopes,
|
||||
SortOrder: g.SortOrder,
|
||||
AllowMessagesDispatch: g.AllowMessagesDispatch,
|
||||
|
||||
@@ -2,14 +2,46 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const stickySessionPrefix = "sticky_session:"
|
||||
const (
|
||||
stickySessionPrefix = "sticky_session:"
|
||||
affinityKeyPrefix = "affinity:"
|
||||
affinityRevKeyPrefix = "affinity_rev:"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed lua/get_affinity.lua
|
||||
getAffinityLua string
|
||||
//go:embed lua/update_affinity.lua
|
||||
updateAffinityLua string
|
||||
//go:embed lua/get_affinity_count.lua
|
||||
getAffinityCountLua string
|
||||
//go:embed lua/get_affinity_clients.lua
|
||||
getAffinityClientsLua string
|
||||
//go:embed lua/get_affinity_clients_with_scores.lua
|
||||
getAffinityClientsWithScoresLua string
|
||||
//go:embed lua/clear_account_affinity.lua
|
||||
clearAccountAffinityLua string
|
||||
//go:embed lua/get_affinity_multi_count.lua
|
||||
getAffinityMultiCountLua string
|
||||
|
||||
getAffinityScript = redis.NewScript(getAffinityLua)
|
||||
updateAffinityScript = redis.NewScript(updateAffinityLua)
|
||||
getAffinityCountScript = redis.NewScript(getAffinityCountLua)
|
||||
getAffinityClientsScript = redis.NewScript(getAffinityClientsLua)
|
||||
getAffinityClientsWithScoresScript = redis.NewScript(getAffinityClientsWithScoresLua)
|
||||
clearAccountAffinityScript = redis.NewScript(clearAccountAffinityLua)
|
||||
getAffinityMultiCountScript = redis.NewScript(getAffinityMultiCountLua)
|
||||
)
|
||||
|
||||
type gatewayCache struct {
|
||||
rdb *redis.Client
|
||||
@@ -19,6 +51,16 @@ func NewGatewayCache(rdb *redis.Client) service.GatewayCache {
|
||||
return &gatewayCache{rdb: rdb}
|
||||
}
|
||||
|
||||
// ensureScriptLoaded 确保 Lua 脚本已加载到 Redis 服务器的脚本缓存中。
|
||||
// Pipeline 中的 Script.Run 只发送 EVALSHA,如果 Redis 重启过导致脚本缓存丢失,
|
||||
// EVALSHA 会返回 NOSCRIPT 错误。此方法提前加载脚本以避免该问题。
|
||||
func ensureScriptLoaded(ctx context.Context, rdb *redis.Client, script *redis.Script) {
|
||||
exists, err := script.Exists(ctx, rdb).Result()
|
||||
if err != nil || len(exists) == 0 || !exists[0] {
|
||||
_ = script.Load(ctx, rdb).Err()
|
||||
}
|
||||
}
|
||||
|
||||
// buildSessionKey 构建 session key,包含 groupID 实现分组隔离
|
||||
// 格式: sticky_session:{groupID}:{sessionHash}
|
||||
func buildSessionKey(groupID int64, sessionHash string) string {
|
||||
@@ -41,13 +83,281 @@ func (c *gatewayCache) RefreshSessionTTL(ctx context.Context, groupID int64, ses
|
||||
}
|
||||
|
||||
// DeleteSessionAccountID 删除粘性会话与账号的绑定关系。
|
||||
// 当检测到绑定的账号不可用(如状态错误、禁用、不可调度等)时调用,
|
||||
// 以便下次请求能够重新选择可用账号。
|
||||
//
|
||||
// DeleteSessionAccountID removes the sticky session binding for the given session.
|
||||
// Called when the bound account becomes unavailable (e.g., error status, disabled,
|
||||
// or unschedulable), allowing subsequent requests to select a new available account.
|
||||
func (c *gatewayCache) DeleteSessionAccountID(ctx context.Context, groupID int64, sessionHash string) error {
|
||||
key := buildSessionKey(groupID, sessionHash)
|
||||
return c.rdb.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
// buildAffinityKey 构建正向亲和 key(member → accounts)
|
||||
// 格式: affinity:{groupID}:{userID}/{clientID}
|
||||
func buildAffinityKey(groupID int64, userID int64, clientID string) string {
|
||||
return fmt.Sprintf("%s%d:%s", affinityKeyPrefix, groupID, buildAffinityMember(userID, clientID))
|
||||
}
|
||||
|
||||
// buildAffinityReverseKey 构建反向亲和 key(account → members)
|
||||
// 格式: affinity_rev:{groupID}:{accountID}
|
||||
func buildAffinityReverseKey(groupID int64, accountID int64) string {
|
||||
return fmt.Sprintf("%s%d:%d", affinityRevKeyPrefix, groupID, accountID)
|
||||
}
|
||||
|
||||
// buildAffinityMember 构建亲和成员标识
|
||||
// 格式: {userID}/{clientID}
|
||||
func buildAffinityMember(userID int64, clientID string) string {
|
||||
return fmt.Sprintf("%d/%s", userID, clientID)
|
||||
}
|
||||
|
||||
// parseAffinityMember 解析亲和成员标识为 userID 和 clientID
|
||||
func parseAffinityMember(member string) (userID int64, clientID string) {
|
||||
idx := strings.IndexByte(member, '/')
|
||||
if idx < 0 {
|
||||
// 兼容旧格式(纯 clientID,无 userID 前缀)
|
||||
return 0, member
|
||||
}
|
||||
userID, _ = strconv.ParseInt(member[:idx], 10, 64)
|
||||
clientID = member[idx+1:]
|
||||
return userID, clientID
|
||||
}
|
||||
|
||||
// GetAffinityAccounts 获取亲和账号列表(按最近使用降序),同时清理过期成员
|
||||
func (c *gatewayCache) GetAffinityAccounts(ctx context.Context, groupID int64, userID int64, clientID string, ttl time.Duration) ([]int64, error) {
|
||||
key := buildAffinityKey(groupID, userID, clientID)
|
||||
now := time.Now().Unix()
|
||||
expireThreshold := now - int64(ttl.Seconds())
|
||||
|
||||
result, err := getAffinityScript.Run(ctx, c.rdb, []string{key}, expireThreshold).StringSlice()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
accountIDs := make([]int64, 0, len(result))
|
||||
for _, s := range result {
|
||||
id, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
accountIDs = append(accountIDs, id)
|
||||
}
|
||||
return accountIDs, nil
|
||||
}
|
||||
|
||||
// UpdateAffinity 添加/更新亲和关系(更新 score 为当前时间戳,刷新 key TTL)
|
||||
func (c *gatewayCache) UpdateAffinity(ctx context.Context, groupID int64, userID int64, clientID string, accountID int64, ttl time.Duration) error {
|
||||
fwdKey := buildAffinityKey(groupID, userID, clientID)
|
||||
revKey := buildAffinityReverseKey(groupID, accountID)
|
||||
now := time.Now().Unix()
|
||||
ttlSeconds := int64(ttl.Seconds())
|
||||
expireThreshold := now - ttlSeconds
|
||||
|
||||
member := buildAffinityMember(userID, clientID)
|
||||
return updateAffinityScript.Run(ctx, c.rdb, []string{fwdKey, revKey},
|
||||
now, ttlSeconds, accountID, expireThreshold, member,
|
||||
).Err()
|
||||
}
|
||||
|
||||
// GetAccountAffinityCountBatch 批量获取账号的亲和成员数量(惰性清理过期成员)
|
||||
func (c *gatewayCache) GetAccountAffinityCountBatch(ctx context.Context, groupID int64, accountIDs []int64, ttl time.Duration) (map[int64]int64, error) {
|
||||
if len(accountIDs) == 0 {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
|
||||
now := time.Now().Unix()
|
||||
expireThreshold := now - int64(ttl.Seconds())
|
||||
|
||||
ensureScriptLoaded(ctx, c.rdb, getAffinityCountScript)
|
||||
|
||||
pipe := c.rdb.Pipeline()
|
||||
cmds := make([]*redis.Cmd, len(accountIDs))
|
||||
for i, accID := range accountIDs {
|
||||
key := buildAffinityReverseKey(groupID, accID)
|
||||
cmds[i] = getAffinityCountScript.Run(ctx, pipe, []string{key}, expireThreshold)
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
if err != nil && err != redis.Nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[int64]int64, len(accountIDs))
|
||||
for i, accID := range accountIDs {
|
||||
count, _ := cmds[i].Int64()
|
||||
result[accID] = count
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetAccountAffinityClientsBatch 批量获取每个账号跨所有分组的亲和成员列表(去重)。
|
||||
// accountGroups: map[accountID][]groupID,对每个 (groupID, accountID) 组合查询反向索引。
|
||||
// 返回值成员格式为 {userID}/{clientID}。
|
||||
func (c *gatewayCache) GetAccountAffinityClientsBatch(ctx context.Context, accountGroups map[int64][]int64, ttl time.Duration) (map[int64][]string, error) {
|
||||
if len(accountGroups) == 0 {
|
||||
return map[int64][]string{}, nil
|
||||
}
|
||||
|
||||
now := time.Now().Unix()
|
||||
expireThreshold := now - int64(ttl.Seconds())
|
||||
|
||||
// 构建所有 (accountID, groupID) 组合的查询
|
||||
type queryItem struct {
|
||||
accountID int64
|
||||
groupID int64
|
||||
}
|
||||
var queries []queryItem
|
||||
for accID, groupIDs := range accountGroups {
|
||||
for _, gID := range groupIDs {
|
||||
queries = append(queries, queryItem{accountID: accID, groupID: gID})
|
||||
}
|
||||
}
|
||||
|
||||
ensureScriptLoaded(ctx, c.rdb, getAffinityClientsScript)
|
||||
|
||||
pipe := c.rdb.Pipeline()
|
||||
cmds := make([]*redis.Cmd, len(queries))
|
||||
for i, q := range queries {
|
||||
key := buildAffinityReverseKey(q.groupID, q.accountID)
|
||||
cmds[i] = getAffinityClientsScript.Run(ctx, pipe, []string{key}, expireThreshold)
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
if err != nil && err != redis.Nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 合并结果:同一个 accountID 跨多个 group 的成员去重
|
||||
result := make(map[int64][]string, len(accountGroups))
|
||||
seen := make(map[int64]map[string]struct{}, len(accountGroups))
|
||||
for i, q := range queries {
|
||||
members, _ := cmds[i].StringSlice()
|
||||
if len(members) == 0 {
|
||||
continue
|
||||
}
|
||||
if seen[q.accountID] == nil {
|
||||
seen[q.accountID] = make(map[string]struct{})
|
||||
}
|
||||
for _, member := range members {
|
||||
if _, exists := seen[q.accountID][member]; !exists {
|
||||
seen[q.accountID][member] = struct{}{}
|
||||
result[q.accountID] = append(result[q.accountID], member)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetAccountAffinityClientsWithScores 获取单个账号跨所有分组的亲和客户端列表(含最后活跃时间戳,去重取最近)。
|
||||
func (c *gatewayCache) GetAccountAffinityClientsWithScores(
|
||||
ctx context.Context,
|
||||
accountID int64,
|
||||
groupIDs []int64,
|
||||
ttl time.Duration,
|
||||
) ([]service.AffinityClient, error) {
|
||||
if len(groupIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
now := time.Now().Unix()
|
||||
expireThreshold := now - int64(ttl.Seconds())
|
||||
|
||||
ensureScriptLoaded(ctx, c.rdb, getAffinityClientsWithScoresScript)
|
||||
|
||||
pipe := c.rdb.Pipeline()
|
||||
cmds := make([]*redis.Cmd, len(groupIDs))
|
||||
for i, gID := range groupIDs {
|
||||
key := buildAffinityReverseKey(gID, accountID)
|
||||
cmds[i] = getAffinityClientsWithScoresScript.Run(ctx, pipe, []string{key}, expireThreshold)
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
if err != nil && err != redis.Nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 合并跨组结果,同一 member 取最近的 lastActive
|
||||
type memberInfo struct {
|
||||
userID int64
|
||||
clientID string
|
||||
ts int64
|
||||
}
|
||||
seen := make(map[string]*memberInfo) // member string → info
|
||||
for _, cmd := range cmds {
|
||||
vals, _ := cmd.StringSlice()
|
||||
// vals 格式: [member1, score1, member2, score2, ...]
|
||||
for j := 0; j+1 < len(vals); j += 2 {
|
||||
member := vals[j]
|
||||
ts, _ := strconv.ParseInt(vals[j+1], 10, 64)
|
||||
if existing, ok := seen[member]; !ok || ts > existing.ts {
|
||||
uid, cid := parseAffinityMember(member)
|
||||
seen[member] = &memberInfo{userID: uid, clientID: cid, ts: ts}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result := make([]service.AffinityClient, 0, len(seen))
|
||||
for _, info := range seen {
|
||||
result = append(result, service.AffinityClient{
|
||||
UserID: info.userID,
|
||||
ClientID: info.clientID,
|
||||
LastActive: time.Unix(info.ts, 0),
|
||||
})
|
||||
}
|
||||
|
||||
// 按最后活跃时间降序排序
|
||||
service.SortAffinityClients(result)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ClearAccountAffinity 清除指定账号在所有分组的亲和记录(正向+反向索引)。
|
||||
// 对每个 groupID 执行 Lua 脚本:读取反向索引获取所有成员,
|
||||
// 从每个成员的正向索引中移除该账号,然后删除反向索引。
|
||||
func (c *gatewayCache) ClearAccountAffinity(ctx context.Context, accountID int64, groupIDs []int64) error {
|
||||
if len(groupIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
ensureScriptLoaded(ctx, c.rdb, clearAccountAffinityScript)
|
||||
|
||||
pipe := c.rdb.Pipeline()
|
||||
for _, gID := range groupIDs {
|
||||
revKey := buildAffinityReverseKey(gID, accountID)
|
||||
clearAccountAffinityScript.Run(ctx, pipe, []string{revKey}, gID, accountID)
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
if err != nil && err != redis.Nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAffinityMultiCount 获取账号的多维度亲和计数。
|
||||
// 返回: uniqueUsers(独立用户数), uniqueClients(独立客户端数), perUserClients(目标用户的客户端数)
|
||||
func (c *gatewayCache) GetAffinityMultiCount(
|
||||
ctx context.Context,
|
||||
groupID int64,
|
||||
accountID int64,
|
||||
targetUserID int64,
|
||||
ttl time.Duration,
|
||||
) (users, clients, perUser int64, err error) {
|
||||
key := buildAffinityReverseKey(groupID, accountID)
|
||||
now := time.Now().Unix()
|
||||
expireThreshold := now - int64(ttl.Seconds())
|
||||
|
||||
targetUserStr := ""
|
||||
if targetUserID > 0 {
|
||||
targetUserStr = strconv.FormatInt(targetUserID, 10)
|
||||
}
|
||||
|
||||
result, err := getAffinityMultiCountScript.Run(ctx, c.rdb, []string{key}, expireThreshold, targetUserStr).Int64Slice()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
if len(result) < 4 {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
|
||||
// result: {totalMembers, uniqueUsers, uniqueClients, perUserClients}
|
||||
return result[1], result[2], result[3], nil
|
||||
}
|
||||
@@ -61,7 +61,8 @@ func (r *groupRepository) Create(ctx context.Context, groupIn *service.Group) er
|
||||
SetMcpXMLInject(groupIn.MCPXMLInject).
|
||||
SetSoraStorageQuotaBytes(groupIn.SoraStorageQuotaBytes).
|
||||
SetAllowMessagesDispatch(groupIn.AllowMessagesDispatch).
|
||||
SetDefaultMappedModel(groupIn.DefaultMappedModel)
|
||||
SetDefaultMappedModel(groupIn.DefaultMappedModel).
|
||||
SetSimulateClaudeMaxEnabled(groupIn.SimulateClaudeMaxEnabled)
|
||||
|
||||
// 设置模型路由配置
|
||||
if groupIn.ModelRouting != nil {
|
||||
@@ -130,7 +131,8 @@ func (r *groupRepository) Update(ctx context.Context, groupIn *service.Group) er
|
||||
SetMcpXMLInject(groupIn.MCPXMLInject).
|
||||
SetSoraStorageQuotaBytes(groupIn.SoraStorageQuotaBytes).
|
||||
SetAllowMessagesDispatch(groupIn.AllowMessagesDispatch).
|
||||
SetDefaultMappedModel(groupIn.DefaultMappedModel)
|
||||
SetDefaultMappedModel(groupIn.DefaultMappedModel).
|
||||
SetSimulateClaudeMaxEnabled(groupIn.SimulateClaudeMaxEnabled)
|
||||
|
||||
// 显式处理可空字段:nil 需要 clear,非 nil 需要 set。
|
||||
if groupIn.DailyLimitUSD != nil {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
internal500CounterPrefix = "internal500_count:account:"
|
||||
internal500CounterTTLSeconds = 86400 // 24 小时兜底
|
||||
)
|
||||
|
||||
// internal500CounterIncrScript 使用 Lua 脚本原子性地增加计数并返回当前值
|
||||
// 如果 key 不存在,则创建并设置过期时间
|
||||
var internal500CounterIncrScript = redis.NewScript(`
|
||||
local key = KEYS[1]
|
||||
local ttl = tonumber(ARGV[1])
|
||||
|
||||
local count = redis.call('INCR', key)
|
||||
if count == 1 then
|
||||
redis.call('EXPIRE', key, ttl)
|
||||
end
|
||||
|
||||
return count
|
||||
`)
|
||||
|
||||
type internal500CounterCache struct {
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
// NewInternal500CounterCache 创建 INTERNAL 500 连续失败计数器缓存实例
|
||||
func NewInternal500CounterCache(rdb *redis.Client) service.Internal500CounterCache {
|
||||
return &internal500CounterCache{rdb: rdb}
|
||||
}
|
||||
|
||||
// IncrementInternal500Count 原子递增计数并返回当前值
|
||||
func (c *internal500CounterCache) IncrementInternal500Count(ctx context.Context, accountID int64) (int64, error) {
|
||||
key := fmt.Sprintf("%s%d", internal500CounterPrefix, accountID)
|
||||
|
||||
result, err := internal500CounterIncrScript.Run(ctx, c.rdb, []string{key}, internal500CounterTTLSeconds).Int64()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("increment internal500 count: %w", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ResetInternal500Count 清零计数器(成功响应时调用)
|
||||
func (c *internal500CounterCache) ResetInternal500Count(ctx context.Context, accountID int64) error {
|
||||
key := fmt.Sprintf("%s%d", internal500CounterPrefix, accountID)
|
||||
return c.rdb.Del(ctx, key).Err()
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
-- 清除单个账号在指定分组的所有亲和记录(正向+反向)
|
||||
-- KEYS[1] = affinity_rev:{groupID}:{accountID} (反向索引)
|
||||
-- ARGV[1] = groupID (用于构建正向 key)
|
||||
-- ARGV[2] = accountID (正向索引中要移除的成员)
|
||||
-- 返回: 清理的成员数量
|
||||
local rev_key = KEYS[1]
|
||||
local group_id = ARGV[1]
|
||||
local account_id = ARGV[2]
|
||||
|
||||
-- 获取反向索引中所有成员 ({userID}/{clientID})
|
||||
local members = redis.call('ZRANGE', rev_key, 0, -1)
|
||||
if #members == 0 then
|
||||
return 0
|
||||
end
|
||||
|
||||
-- 从每个成员的正向索引中移除该账号
|
||||
for _, member in ipairs(members) do
|
||||
local fwd_key = 'affinity:' .. group_id .. ':' .. member
|
||||
redis.call('ZREM', fwd_key, account_id)
|
||||
-- 如果正向索引为空,删除 key
|
||||
if redis.call('ZCARD', fwd_key) == 0 then
|
||||
redis.call('DEL', fwd_key)
|
||||
end
|
||||
end
|
||||
|
||||
-- 删除反向索引
|
||||
redis.call('DEL', rev_key)
|
||||
|
||||
return #members
|
||||
@@ -0,0 +1,5 @@
|
||||
-- 清理过期成员后返回亲和账号列表(按最近使用降序)
|
||||
-- KEYS[1] = affinity:{groupID}:{userID}/{clientID}
|
||||
-- ARGV[1] = 过期阈值时间戳 (now - ttl)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
|
||||
return redis.call('ZREVRANGE', KEYS[1], 0, -1)
|
||||
@@ -0,0 +1,6 @@
|
||||
-- 清理过期成员后返回反向索引的成员列表(按最近使用降序)
|
||||
-- 成员格式: {userID}/{clientID}
|
||||
-- KEYS[1] = affinity_rev:{groupID}:{accountID}
|
||||
-- ARGV[1] = 过期阈值时间戳 (now - ttl)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
|
||||
return redis.call('ZREVRANGE', KEYS[1], 0, -1)
|
||||
@@ -0,0 +1,7 @@
|
||||
-- 清理过期成员后返回反向索引的成员列表及其 score(最后活跃时间戳)
|
||||
-- 成员格式: {userID}/{clientID}
|
||||
-- KEYS[1] = affinity_rev:{groupID}:{accountID}
|
||||
-- ARGV[1] = 过期阈值时间戳 (now - ttl)
|
||||
-- 返回: {member1, score1, member2, score2, ...}(按最近使用降序)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
|
||||
return redis.call('ZREVRANGEBYSCORE', KEYS[1], '+inf', '-inf', 'WITHSCORES')
|
||||
@@ -0,0 +1,5 @@
|
||||
-- 清理过期成员后返回反向索引的成员数量
|
||||
-- KEYS[1] = affinity_rev:{groupID}:{accountID}
|
||||
-- ARGV[1] = 过期阈值时间戳 (now - ttl)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
|
||||
return redis.call('ZCARD', KEYS[1])
|
||||
@@ -0,0 +1,40 @@
|
||||
-- 从反向索引解析多维度计数(用户/客户端/每用户客户端)
|
||||
-- KEYS[1] = affinity_rev:{groupID}:{accountID}
|
||||
-- ARGV[1] = 过期阈值时间戳 (now - ttl)
|
||||
-- ARGV[2] = 目标 userID(传 "" 则不计算 perUserClients)
|
||||
-- 返回: {totalMembers, uniqueUsers, uniqueClients, perUserClients}
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
|
||||
|
||||
local members = redis.call('ZRANGE', KEYS[1], 0, -1)
|
||||
local total = #members
|
||||
if total == 0 then
|
||||
return {0, 0, 0, 0}
|
||||
end
|
||||
|
||||
local target_user = ARGV[2]
|
||||
local users = {}
|
||||
local clients = {}
|
||||
local user_count = 0
|
||||
local client_count = 0
|
||||
local per_user_count = 0
|
||||
|
||||
for _, member in ipairs(members) do
|
||||
local sep = string.find(member, '/', 1, true)
|
||||
if sep then
|
||||
local uid = string.sub(member, 1, sep - 1)
|
||||
local cid = string.sub(member, sep + 1)
|
||||
if not users[uid] then
|
||||
users[uid] = true
|
||||
user_count = user_count + 1
|
||||
end
|
||||
if not clients[cid] then
|
||||
clients[cid] = true
|
||||
client_count = client_count + 1
|
||||
end
|
||||
if target_user ~= '' and uid == target_user then
|
||||
per_user_count = per_user_count + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return {total, user_count, client_count, per_user_count}
|
||||
@@ -0,0 +1,15 @@
|
||||
-- 原子双写正向+反向索引
|
||||
-- KEYS[1] = affinity:{groupID}:{userID}/{clientID} (正向: member → accounts)
|
||||
-- KEYS[2] = affinity_rev:{groupID}:{accountID} (反向: account → members)
|
||||
-- ARGV[1] = 当前时间戳 (score)
|
||||
-- ARGV[2] = TTL 秒数
|
||||
-- ARGV[3] = accountID (正向索引的成员)
|
||||
-- ARGV[4] = 过期阈值时间戳 (now - ttl)
|
||||
-- ARGV[5] = {userID}/{clientID} (反向索引的成员)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[4])
|
||||
redis.call('ZADD', KEYS[1], ARGV[1], ARGV[3])
|
||||
redis.call('EXPIRE', KEYS[1], ARGV[2])
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', ARGV[4])
|
||||
redis.call('ZADD', KEYS[2], ARGV[1], ARGV[5])
|
||||
redis.call('EXPIRE', KEYS[2], ARGV[2])
|
||||
return 1
|
||||
@@ -417,3 +417,22 @@ func (r *soraGenerationRepository) CountByUserAndStatus(ctx context.Context, use
|
||||
err := r.sql.QueryRowContext(ctx, query, args...).Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
// CountByStorageType 按存储类型和状态统计生成记录数。
|
||||
func (r *soraGenerationRepository) CountByStorageType(ctx context.Context, storageType string, statuses []string) (int64, error) {
|
||||
if len(statuses) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
placeholders := make([]string, len(statuses))
|
||||
args := []any{storageType}
|
||||
for i, s := range statuses {
|
||||
placeholders[i] = fmt.Sprintf("$%d", i+2)
|
||||
args = append(args, s)
|
||||
}
|
||||
|
||||
var count int64
|
||||
query := fmt.Sprintf("SELECT COUNT(*) FROM sora_generations WHERE storage_type = $1 AND status IN (%s)", strings.Join(placeholders, ","))
|
||||
err := r.sql.QueryRowContext(ctx, query, args...).Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
)
|
||||
|
||||
type SoraTaskRepository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewSoraTaskRepository(sqlDB *sql.DB) service.SoraTaskRepository {
|
||||
return &SoraTaskRepository{db: sqlDB}
|
||||
}
|
||||
|
||||
func (r *SoraTaskRepository) Create(ctx context.Context, task *service.SoraTask) error {
|
||||
var charStr, reqStr *string
|
||||
if task.CharacterInfo != nil {
|
||||
b, _ := json.Marshal(task.CharacterInfo)
|
||||
s := string(b)
|
||||
charStr = &s
|
||||
}
|
||||
if len(task.RequestBody) > 0 && utf8.Valid(task.RequestBody) {
|
||||
s := string(task.RequestBody)
|
||||
reqStr = &s
|
||||
}
|
||||
|
||||
_, err := r.db.ExecContext(ctx, `
|
||||
INSERT INTO sora_tasks (
|
||||
id, account_id, api_key_id, upstream_task_id, object_type,
|
||||
model, prompt, status, progress, video_url, stored_key, storage_type,
|
||||
share_id, character_info, error_message, error_type,
|
||||
request_body, seconds, size, created_at, completed_at
|
||||
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21)`,
|
||||
task.ID, task.AccountID, task.APIKeyID, task.UpstreamTaskID, task.ObjectType,
|
||||
task.Model, task.Prompt, task.Status, task.Progress, task.VideoURL,
|
||||
task.StoredKey, task.StorageType,
|
||||
task.ShareID, charStr, task.ErrorMessage, task.ErrorType,
|
||||
reqStr, task.Seconds, task.Size, task.CreatedAt, task.CompletedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *SoraTaskRepository) GetByID(ctx context.Context, id string) (*service.SoraTask, error) {
|
||||
row := r.db.QueryRowContext(ctx, `SELECT `+soraTaskColumns+` FROM sora_tasks WHERE id = $1`, id)
|
||||
return scanTask(row)
|
||||
}
|
||||
|
||||
func (r *SoraTaskRepository) GetByIDAndAPIKey(ctx context.Context, id string, apiKeyID int64) (*service.SoraTask, error) {
|
||||
row := r.db.QueryRowContext(ctx,
|
||||
`SELECT `+soraTaskColumns+` FROM sora_tasks WHERE id = $1 AND api_key_id = $2`,
|
||||
id, apiKeyID,
|
||||
)
|
||||
return scanTask(row)
|
||||
}
|
||||
|
||||
func (r *SoraTaskRepository) Update(ctx context.Context, task *service.SoraTask) error {
|
||||
var charStr *string
|
||||
if task.CharacterInfo != nil {
|
||||
b, _ := json.Marshal(task.CharacterInfo)
|
||||
s := string(b)
|
||||
charStr = &s
|
||||
}
|
||||
|
||||
_, err := r.db.ExecContext(ctx, `
|
||||
UPDATE sora_tasks SET
|
||||
upstream_task_id = $2, status = $3, progress = $4,
|
||||
video_url = $5, stored_key = $6, storage_type = $7,
|
||||
share_id = $8, character_info = $9,
|
||||
error_message = $10, error_type = $11,
|
||||
completed_at = $12, seconds = $13, size = $14, object_type = $15
|
||||
WHERE id = $1`,
|
||||
task.ID, task.UpstreamTaskID, task.Status, task.Progress,
|
||||
task.VideoURL, task.StoredKey, task.StorageType,
|
||||
task.ShareID, charStr,
|
||||
task.ErrorMessage, task.ErrorType,
|
||||
task.CompletedAt, task.Seconds, task.Size, task.ObjectType,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *SoraTaskRepository) ListPending(ctx context.Context) ([]*service.SoraTask, error) {
|
||||
rows, err := r.db.QueryContext(ctx,
|
||||
`SELECT `+soraTaskColumns+` FROM sora_tasks WHERE status IN ('queued', 'in_progress') ORDER BY created_at ASC LIMIT 200`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var tasks []*service.SoraTask
|
||||
for rows.Next() {
|
||||
t, err := scanTaskFromRow(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tasks = append(tasks, t)
|
||||
}
|
||||
return tasks, rows.Err()
|
||||
}
|
||||
|
||||
const soraTaskColumns = `id, account_id, api_key_id, upstream_task_id, object_type,
|
||||
model, prompt, status, progress, video_url, stored_key, storage_type,
|
||||
share_id, character_info, error_message, error_type,
|
||||
request_body, seconds, size, created_at, completed_at`
|
||||
|
||||
func scanTask(s scannable) (*service.SoraTask, error) {
|
||||
var t service.SoraTask
|
||||
var charJSON, reqBody []byte
|
||||
err := s.Scan(
|
||||
&t.ID, &t.AccountID, &t.APIKeyID, &t.UpstreamTaskID, &t.ObjectType,
|
||||
&t.Model, &t.Prompt, &t.Status, &t.Progress, &t.VideoURL,
|
||||
&t.StoredKey, &t.StorageType,
|
||||
&t.ShareID, &charJSON, &t.ErrorMessage, &t.ErrorType,
|
||||
&reqBody, &t.Seconds, &t.Size, &t.CreatedAt, &t.CompletedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(charJSON) > 0 {
|
||||
var ch service.SoraCharacter
|
||||
if json.Unmarshal(charJSON, &ch) == nil && ch.Username != "" {
|
||||
t.CharacterInfo = &ch
|
||||
}
|
||||
}
|
||||
t.RequestBody = reqBody
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func scanTaskFromRow(rows *sql.Rows) (*service.SoraTask, error) {
|
||||
return scanTask(rows)
|
||||
}
|
||||
@@ -3001,7 +3001,7 @@ func (r *usageLogRepository) GetGroupStatsWithFilters(ctx context.Context, start
|
||||
query := `
|
||||
SELECT
|
||||
COALESCE(ul.group_id, 0) as group_id,
|
||||
COALESCE(g.name, '') as group_name,
|
||||
COALESCE(g.name, '(无分组)') as group_name,
|
||||
COUNT(*) as requests,
|
||||
COALESCE(SUM(ul.input_tokens + ul.output_tokens + ul.cache_creation_tokens + ul.cache_read_tokens), 0) as total_tokens,
|
||||
COALESCE(SUM(ul.total_cost), 0) as cost,
|
||||
|
||||
@@ -100,7 +100,7 @@ func (r *userGroupRateRepository) GetByGroupID(ctx context.Context, groupID int6
|
||||
query := `
|
||||
SELECT ugr.user_id, u.username, u.email, COALESCE(u.notes, ''), u.status, ugr.rate_multiplier
|
||||
FROM user_group_rate_multipliers ugr
|
||||
JOIN users u ON u.id = ugr.user_id
|
||||
JOIN users u ON u.id = ugr.user_id AND u.deleted_at IS NULL
|
||||
WHERE ugr.group_id = $1
|
||||
ORDER BY ugr.user_id
|
||||
`
|
||||
|
||||
@@ -81,6 +81,7 @@ var ProviderSet = wire.NewSet(
|
||||
NewAPIKeyCache,
|
||||
NewTempUnschedCache,
|
||||
NewTimeoutCounterCache,
|
||||
NewInternal500CounterCache,
|
||||
ProvideConcurrencyCache,
|
||||
ProvideSessionLimitCache,
|
||||
NewRPMCache,
|
||||
|
||||
Reference in new issue
Block a user