mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 23:07:18 +00:00
revert: remove client affinity count display infrastructure
Remove frontend badge/tooltip and backend counting/query infrastructure for client affinity. Core client affinity scheduling logic is preserved. Removed: - GatewayCache injection in AccountHandler - GetAccountAffinityCount/Batch/Clients methods and interface - AffinityClientInfo struct and ClientAffinityTTL function - /affinity-clients API endpoint and route - Reverse index (account_affinity:*) Lua scripts - Frontend blue badge, tooltip, and lazy-load logic - affinity_client_count field from types and response Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
77ae2bcccb
commit
e8e5acb9e7
19 files changed
+15
-342
No files matched your search
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
@@ -12,9 +11,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
stickySessionPrefix = "sticky_session:"
|
||||
clientAffinityPrefix = "client_affinity:"
|
||||
accountAffinityPrefix = "account_affinity:"
|
||||
stickySessionPrefix = "sticky_session:"
|
||||
clientAffinityPrefix = "client_affinity:"
|
||||
)
|
||||
|
||||
type gatewayCache struct {
|
||||
@@ -72,21 +70,16 @@ redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
|
||||
return redis.call('ZREVRANGE', KEYS[1], 0, -1)
|
||||
`)
|
||||
|
||||
// updateAffinityScript: 清理过期成员、添加/更新亲和关系、刷新 TTL,
|
||||
// 同时维护反向索引 account_affinity:{accountID}
|
||||
// updateAffinityScript: 清理过期成员、添加/更新亲和关系、刷新 TTL
|
||||
// KEYS[1] = client_affinity:{groupID}:{clientID}
|
||||
// KEYS[2] = account_affinity:{accountID}
|
||||
// ARGV[1] = 当前时间戳
|
||||
// ARGV[2] = TTL 秒数
|
||||
// ARGV[3] = accountID
|
||||
// ARGV[4] = 过期阈值时间戳 (now - ttl)
|
||||
// ARGV[5] = 反向索引成员 "{groupID}:{clientID}"
|
||||
var updateAffinityScript = redis.NewScript(`
|
||||
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('ZADD', KEYS[2], ARGV[1], ARGV[5])
|
||||
redis.call('EXPIRE', KEYS[2], 86400)
|
||||
return 1
|
||||
`)
|
||||
|
||||
@@ -116,140 +109,16 @@ func (c *gatewayCache) GetClientAffinityAccounts(ctx context.Context, groupID in
|
||||
|
||||
func (c *gatewayCache) UpdateClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64, ttl time.Duration) error {
|
||||
key := buildAffinityKey(groupID, clientID)
|
||||
reverseKey := buildAccountAffinityKey(accountID)
|
||||
reverseMember := fmt.Sprintf("%d:%s", groupID, clientID)
|
||||
now := time.Now().Unix()
|
||||
ttlSeconds := int64(ttl.Seconds())
|
||||
expireThreshold := now - ttlSeconds
|
||||
|
||||
return updateAffinityScript.Run(ctx, c.rdb, []string{key, reverseKey},
|
||||
now, ttlSeconds, accountID, expireThreshold, reverseMember,
|
||||
return updateAffinityScript.Run(ctx, c.rdb, []string{key},
|
||||
now, ttlSeconds, accountID, expireThreshold,
|
||||
).Err()
|
||||
}
|
||||
|
||||
func (c *gatewayCache) RemoveClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64) error {
|
||||
key := buildAffinityKey(groupID, clientID)
|
||||
reverseKey := buildAccountAffinityKey(accountID)
|
||||
reverseMember := fmt.Sprintf("%d:%s", groupID, clientID)
|
||||
pipe := c.rdb.Pipeline()
|
||||
pipe.ZRem(ctx, key, accountID)
|
||||
pipe.ZRem(ctx, reverseKey, reverseMember)
|
||||
_, err := pipe.Exec(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
// buildAccountAffinityKey 构建账号亲和反向索引 key
|
||||
// 格式: account_affinity:{accountID}
|
||||
func buildAccountAffinityKey(accountID int64) string {
|
||||
return fmt.Sprintf("%s%d", accountAffinityPrefix, accountID)
|
||||
}
|
||||
|
||||
// getAccountAffinityScript: 清理过期成员后返回 ZCARD,空 key 自动删除
|
||||
// KEYS[1] = account_affinity:{accountID}
|
||||
// ARGV[1] = 过期阈值时间戳 (now - ttl)
|
||||
var getAccountAffinityScript = redis.NewScript(`
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
|
||||
local count = redis.call('ZCARD', KEYS[1])
|
||||
if count == 0 then
|
||||
redis.call('DEL', KEYS[1])
|
||||
end
|
||||
return count
|
||||
`)
|
||||
|
||||
func (c *gatewayCache) GetAccountAffinityCount(ctx context.Context, accountID int64, ttl time.Duration) (int, error) {
|
||||
key := buildAccountAffinityKey(accountID)
|
||||
expireThreshold := time.Now().Unix() - int64(ttl.Seconds())
|
||||
|
||||
count, err := getAccountAffinityScript.Run(ctx, c.rdb, []string{key}, expireThreshold).Int()
|
||||
if err != nil && err != redis.Nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (c *gatewayCache) GetAccountAffinityCountBatch(ctx context.Context, accountIDs []int64, ttl time.Duration) (map[int64]int, error) {
|
||||
if len(accountIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
expireThreshold := time.Now().Unix() - int64(ttl.Seconds())
|
||||
pipe := c.rdb.Pipeline()
|
||||
cmds := make([]*redis.Cmd, len(accountIDs))
|
||||
|
||||
for i, id := range accountIDs {
|
||||
key := buildAccountAffinityKey(id)
|
||||
cmds[i] = getAccountAffinityScript.Run(ctx, pipe, []string{key}, expireThreshold)
|
||||
}
|
||||
|
||||
_, err := pipe.Exec(ctx)
|
||||
if err != nil && err != redis.Nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[int64]int, len(accountIDs))
|
||||
for i, id := range accountIDs {
|
||||
count, err := cmds[i].Int()
|
||||
if err != nil && err != redis.Nil {
|
||||
continue
|
||||
}
|
||||
result[id] = count
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// getAccountAffinityClientsScript: 清理过期成员后返回成员列表及 score
|
||||
// KEYS[1] = account_affinity:{accountID}
|
||||
// ARGV[1] = 过期阈值时间戳 (now - ttl)
|
||||
var getAccountAffinityClientsScript = redis.NewScript(`
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
|
||||
return redis.call('ZRANGE', KEYS[1], 0, -1, 'WITHSCORES')
|
||||
`)
|
||||
|
||||
func (c *gatewayCache) GetAccountAffinityClients(ctx context.Context, accountID int64, ttl time.Duration) ([]service.AffinityClientInfo, error) {
|
||||
key := buildAccountAffinityKey(accountID)
|
||||
now := time.Now().Unix()
|
||||
expireThreshold := now - int64(ttl.Seconds())
|
||||
|
||||
raw, err := getAccountAffinityClientsScript.Run(ctx, c.rdb, []string{key}, expireThreshold).StringSlice()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// ZRANGE WITHSCORES returns [member1, score1, member2, score2, ...]
|
||||
clients := make([]service.AffinityClientInfo, 0, len(raw)/2)
|
||||
for i := 0; i+1 < len(raw); i += 2 {
|
||||
member := raw[i]
|
||||
scoreStr := raw[i+1]
|
||||
|
||||
score, parseErr := strconv.ParseInt(scoreStr, 10, 64)
|
||||
if parseErr != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// member format: "{groupID}:{clientID}"
|
||||
sepIdx := strings.IndexByte(member, ':')
|
||||
if sepIdx < 0 {
|
||||
continue
|
||||
}
|
||||
groupID, parseErr := strconv.ParseInt(member[:sepIdx], 10, 64)
|
||||
if parseErr != nil {
|
||||
continue
|
||||
}
|
||||
clientID := member[sepIdx+1:]
|
||||
|
||||
remaining := score + int64(ttl.Seconds()) - now
|
||||
if remaining < 0 {
|
||||
remaining = 0
|
||||
}
|
||||
|
||||
clients = append(clients, service.AffinityClientInfo{
|
||||
GroupID: groupID,
|
||||
ClientID: clientID,
|
||||
TTL: remaining,
|
||||
})
|
||||
}
|
||||
return clients, nil
|
||||
return c.rdb.ZRem(ctx, key, accountID).Err()
|
||||
}
|
||||
Reference in new issue
Block a user