mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 15:06:36 +00:00
feat: add affinity scheduling controls
This commit is contained in:
1 parent
338e3df8d9
commit
44ec2626cf
44 files changed
+3711
-496
No files matched your search
@@ -1,21 +1,21 @@
|
||||
-- 清除单个账号在指定分组的所有亲和记录(正向+反向)
|
||||
-- KEYS[1] = client_affinity_rev:{groupID}:{accountID} (反向索引)
|
||||
-- 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]
|
||||
|
||||
-- 获取反向索引中所有客户端 ID
|
||||
local clients = redis.call('ZRANGE', rev_key, 0, -1)
|
||||
if #clients == 0 then
|
||||
-- 获取反向索引中所有成员 ({userID}/{clientID})
|
||||
local members = redis.call('ZRANGE', rev_key, 0, -1)
|
||||
if #members == 0 then
|
||||
return 0
|
||||
end
|
||||
|
||||
-- 从每个客户端的正向索引中移除该账号
|
||||
for _, client_id in ipairs(clients) do
|
||||
local fwd_key = 'client_affinity:' .. group_id .. ':' .. client_id
|
||||
-- 从每个成员的正向索引中移除该账号
|
||||
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
|
||||
@@ -26,4 +26,4 @@ end
|
||||
-- 删除反向索引
|
||||
redis.call('DEL', rev_key)
|
||||
|
||||
return #clients
|
||||
return #members
|
||||
@@ -1,5 +1,5 @@
|
||||
-- 清理过期成员后返回亲和账号列表(按最近使用降序)
|
||||
-- KEYS[1] = client_affinity:{groupID}:{clientID}
|
||||
-- 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)
|
||||
@@ -1,5 +1,6 @@
|
||||
-- 清理过期成员后返回反向索引的 clientID 列表(按最近使用降序)
|
||||
-- KEYS[1] = client_affinity_rev:{groupID}:{accountID}
|
||||
-- 清理过期成员后返回反向索引的成员列表(按最近使用降序)
|
||||
-- 成员格式: {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)
|
||||
@@ -1,6 +1,7 @@
|
||||
-- 清理过期成员后返回反向索引的 clientID 列表及其 score(最后活跃时间戳)
|
||||
-- KEYS[1] = client_affinity_rev:{groupID}:{accountID}
|
||||
-- 清理过期成员后返回反向索引的成员列表及其 score(最后活跃时间戳)
|
||||
-- 成员格式: {userID}/{clientID}
|
||||
-- KEYS[1] = affinity_rev:{groupID}:{accountID}
|
||||
-- ARGV[1] = 过期阈值时间戳 (now - ttl)
|
||||
-- 返回: {clientID1, score1, clientID2, score2, ...}(按最近使用降序)
|
||||
-- 返回: {member1, score1, member2, score2, ...}(按最近使用降序)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
|
||||
return redis.call('ZREVRANGEBYSCORE', KEYS[1], '+inf', '-inf', 'WITHSCORES')
|
||||
@@ -1,5 +1,5 @@
|
||||
-- 清理过期成员后返回反向索引的成员数量
|
||||
-- KEYS[1] = client_affinity_rev:{groupID}:{accountID}
|
||||
-- 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}
|
||||
@@ -1,11 +1,11 @@
|
||||
-- 原子双写正向+反向索引
|
||||
-- KEYS[1] = client_affinity:{groupID}:{clientID} (正向: client → accounts)
|
||||
-- KEYS[2] = client_affinity_rev:{groupID}:{accountID} (反向: account → clients)
|
||||
-- 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] = clientID (反向索引的成员)
|
||||
-- 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])
|
||||
|
||||
Reference in new issue
Block a user