fix:"模型准入改为公平排队并收紧渠道凭据与目录暴露"

- acquire-pair.lua 增加模型到达顺序队列:空闲槽位先给更早等待者,超时/放弃/过期即出队,仅因自身 Key 满的等待者不占模型队位
- 修改渠道 Base URL 必须重新填写凭据,阻止已存密钥被发送到新地址;审计记录新旧地址
- 普通用户模型目录只返回渠道 id/name/status,不再暴露 Base URL 和凭据掩码
- 管理端用量 CSV 导出中和公式前缀
- 新增真实 Redis 公平准入、渠道凭据重填、目录字段和 CSV 回归

验证:mvn -B -o clean verify 403 项通过(独立 PostgreSQL/Redis)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
xppandClaude Opus 5.5 committed 2026-10-22 12:06:41 +08:00
1 parent 2e53b1d9c8
commit 1a608f8ff8
9 files changed
+178 -16

No files matched your search

@@ -1,19 +1,40 @@
-- KEYS: model slots, key slots, model waiting (score = wait expiry), model arrival order (score = first arrival).
-- ARGV: model limit, key limit, slot ttl, token, remaining wait ms, waiting ttl.
-- Returns 1 admitted, 0 model busy or behind earlier waiters, 2 own API Key busy.
local time = redis.call('TIME')
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', now)
redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', now)
-- Expired waiters (crashed or timed-out callers) must also leave the arrival order.
local expired = redis.call('ZRANGEBYSCORE', KEYS[3], '-inf', now)
for _, member in ipairs(expired) do redis.call('ZREM', KEYS[4], member) end
redis.call('ZREMRANGEBYSCORE', KEYS[3], '-inf', now)
local busy = nil
if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then busy = 0
elseif redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then busy = 2 end
if busy ~= nil then
if tonumber(ARGV[5]) > 0 then
redis.call('ZADD', KEYS[3], now + tonumber(ARGV[5]), ARGV[4])
local wait = tonumber(ARGV[5])
local function remember_waiting()
if wait > 0 then
redis.call('ZADD', KEYS[3], now + wait, ARGV[4])
redis.call('PEXPIRE', KEYS[3], ARGV[6])
end
return busy
end
-- A waiter blocked only by its own API Key limit does not hold a place ahead of other keys.
if redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then
redis.call('ZREM', KEYS[4], ARGV[4])
remember_waiting()
return 2
end
local ahead = redis.call('ZRANK', KEYS[4], ARGV[4])
if not ahead then ahead = redis.call('ZCARD', KEYS[4]) end
-- FIFO: a free model slot belongs to earlier model waiters before a newer request.
if redis.call('ZCARD', KEYS[1]) + ahead >= tonumber(ARGV[1]) then
if wait > 0 then
redis.call('ZADD', KEYS[4], 'NX', now, ARGV[4])
redis.call('PEXPIRE', KEYS[4], ARGV[6])
end
remember_waiting()
return 0
end
redis.call('ZREM', KEYS[3], ARGV[4])
redis.call('ZREM', KEYS[4], ARGV[4])
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[3]), ARGV[4])
redis.call('ZADD', KEYS[2], now + tonumber(ARGV[3]), ARGV[4])
redis.call('PEXPIRE', KEYS[1], ARGV[3])