2025-12-19 23:39:28 +08:00
|
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
2025-12-25 17:15:01 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
2025-12-19 23:39:28 +08:00
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-31 08:50:12 +08:00
|
|
|
|
// 并发控制缓存常量定义
|
|
|
|
|
|
//
|
|
|
|
|
|
// 性能优化说明:
|
|
|
|
|
|
// 原实现使用 SCAN 命令遍历独立的槽位键(concurrency:account:{id}:{requestID}),
|
|
|
|
|
|
// 在高并发场景下 SCAN 需要多次往返,且遍历大量键时性能下降明显。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 新实现改用 Redis 有序集合(Sorted Set):
|
|
|
|
|
|
// 1. 每个账号/用户只有一个键,成员为 requestID,分数为时间戳
|
|
|
|
|
|
// 2. 使用 ZCARD 原子获取并发数,时间复杂度 O(1)
|
|
|
|
|
|
// 3. 使用 ZREMRANGEBYSCORE 清理过期槽位,避免手动管理 TTL
|
|
|
|
|
|
// 4. 单次 Redis 调用完成计数,减少网络往返
|
2025-12-19 23:39:28 +08:00
|
|
|
|
const (
|
2025-12-31 08:50:12 +08:00
|
|
|
|
// 并发槽位键前缀(有序集合)
|
|
|
|
|
|
// 格式: concurrency:account:{accountID}
|
2025-12-24 21:00:29 +08:00
|
|
|
|
accountSlotKeyPrefix = "concurrency:account:"
|
2025-12-31 08:50:12 +08:00
|
|
|
|
// 格式: concurrency:user:{userID}
|
2025-12-24 21:00:29 +08:00
|
|
|
|
userSlotKeyPrefix = "concurrency:user:"
|
2025-12-31 08:50:12 +08:00
|
|
|
|
// 等待队列计数器格式: concurrency:wait:{userID}
|
2025-12-24 21:00:29 +08:00
|
|
|
|
waitQueueKeyPrefix = "concurrency:wait:"
|
|
|
|
|
|
|
2025-12-31 08:50:12 +08:00
|
|
|
|
// 默认槽位过期时间(分钟),可通过配置覆盖
|
|
|
|
|
|
defaultSlotTTLMinutes = 15
|
2025-12-19 23:39:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
2025-12-31 08:50:12 +08:00
|
|
|
|
// acquireScript 使用有序集合计数并在未达上限时添加槽位
|
|
|
|
|
|
// 使用 Redis TIME 命令获取服务器时间,避免多实例时钟不同步问题
|
|
|
|
|
|
// KEYS[1] = 有序集合键 (concurrency:account:{id} / concurrency:user:{id})
|
2025-12-24 21:00:29 +08:00
|
|
|
|
// ARGV[1] = maxConcurrency
|
2025-12-31 08:50:12 +08:00
|
|
|
|
// ARGV[2] = TTL(秒)
|
|
|
|
|
|
// ARGV[3] = requestID
|
2025-12-19 23:39:28 +08:00
|
|
|
|
acquireScript = redis.NewScript(`
|
2025-12-31 08:50:12 +08:00
|
|
|
|
local key = KEYS[1]
|
2025-12-24 21:00:29 +08:00
|
|
|
|
local maxConcurrency = tonumber(ARGV[1])
|
|
|
|
|
|
local ttl = tonumber(ARGV[2])
|
2025-12-31 08:50:12 +08:00
|
|
|
|
local requestID = ARGV[3]
|
|
|
|
|
|
|
|
|
|
|
|
-- 使用 Redis 服务器时间,确保多实例时钟一致
|
|
|
|
|
|
local timeResult = redis.call('TIME')
|
|
|
|
|
|
local now = tonumber(timeResult[1])
|
|
|
|
|
|
local expireBefore = now - ttl
|
2025-12-24 21:00:29 +08:00
|
|
|
|
|
2025-12-31 08:50:12 +08:00
|
|
|
|
-- 清理过期槽位
|
|
|
|
|
|
redis.call('ZREMRANGEBYSCORE', key, '-inf', expireBefore)
|
|
|
|
|
|
|
|
|
|
|
|
-- 检查是否已存在(支持重试场景刷新时间戳)
|
|
|
|
|
|
local exists = redis.call('ZSCORE', key, requestID)
|
|
|
|
|
|
if exists ~= false then
|
|
|
|
|
|
redis.call('ZADD', key, now, requestID)
|
|
|
|
|
|
redis.call('EXPIRE', key, ttl)
|
|
|
|
|
|
return 1
|
|
|
|
|
|
end
|
2025-12-24 21:00:29 +08:00
|
|
|
|
|
2025-12-31 08:50:12 +08:00
|
|
|
|
-- 检查是否达到并发上限
|
|
|
|
|
|
local count = redis.call('ZCARD', key)
|
2025-12-24 21:00:29 +08:00
|
|
|
|
if count < maxConcurrency then
|
2025-12-31 08:50:12 +08:00
|
|
|
|
redis.call('ZADD', key, now, requestID)
|
|
|
|
|
|
redis.call('EXPIRE', key, ttl)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return 1
|
|
|
|
|
|
end
|
2025-12-24 21:00:29 +08:00
|
|
|
|
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return 0
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
2025-12-31 08:50:12 +08:00
|
|
|
|
// getCountScript 统计有序集合中的槽位数量并清理过期条目
|
|
|
|
|
|
// 使用 Redis TIME 命令获取服务器时间
|
|
|
|
|
|
// KEYS[1] = 有序集合键
|
|
|
|
|
|
// ARGV[1] = TTL(秒)
|
2025-12-24 21:00:29 +08:00
|
|
|
|
getCountScript = redis.NewScript(`
|
2025-12-31 08:50:12 +08:00
|
|
|
|
local key = KEYS[1]
|
|
|
|
|
|
local ttl = tonumber(ARGV[1])
|
|
|
|
|
|
|
|
|
|
|
|
-- 使用 Redis 服务器时间
|
|
|
|
|
|
local timeResult = redis.call('TIME')
|
|
|
|
|
|
local now = tonumber(timeResult[1])
|
|
|
|
|
|
local expireBefore = now - ttl
|
|
|
|
|
|
|
|
|
|
|
|
redis.call('ZREMRANGEBYSCORE', key, '-inf', expireBefore)
|
|
|
|
|
|
return redis.call('ZCARD', key)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
`)
|
|
|
|
|
|
|
2025-12-24 21:00:29 +08:00
|
|
|
|
// incrementWaitScript - only sets TTL on first creation to avoid refreshing
|
|
|
|
|
|
// KEYS[1] = wait queue key
|
|
|
|
|
|
// ARGV[1] = maxWait
|
|
|
|
|
|
// ARGV[2] = TTL in seconds
|
2025-12-19 23:39:28 +08:00
|
|
|
|
incrementWaitScript = redis.NewScript(`
|
2025-12-24 21:00:29 +08:00
|
|
|
|
local current = redis.call('GET', KEYS[1])
|
2025-12-19 23:39:28 +08:00
|
|
|
|
if current == false then
|
|
|
|
|
|
current = 0
|
|
|
|
|
|
else
|
|
|
|
|
|
current = tonumber(current)
|
|
|
|
|
|
end
|
2025-12-24 21:00:29 +08:00
|
|
|
|
|
|
|
|
|
|
if current >= tonumber(ARGV[1]) then
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return 0
|
|
|
|
|
|
end
|
2025-12-24 21:00:29 +08:00
|
|
|
|
|
|
|
|
|
|
local newVal = redis.call('INCR', KEYS[1])
|
|
|
|
|
|
|
|
|
|
|
|
-- Only set TTL on first creation to avoid refreshing zombie data
|
|
|
|
|
|
if newVal == 1 then
|
|
|
|
|
|
redis.call('EXPIRE', KEYS[1], ARGV[2])
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return 1
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
2025-12-24 21:00:29 +08:00
|
|
|
|
// decrementWaitScript - same as before
|
2025-12-19 23:39:28 +08:00
|
|
|
|
decrementWaitScript = redis.NewScript(`
|
|
|
|
|
|
local current = redis.call('GET', KEYS[1])
|
|
|
|
|
|
if current ~= false and tonumber(current) > 0 then
|
|
|
|
|
|
redis.call('DECR', KEYS[1])
|
|
|
|
|
|
end
|
|
|
|
|
|
return 1
|
|
|
|
|
|
`)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type concurrencyCache struct {
|
2025-12-31 14:51:58 +08:00
|
|
|
|
rdb *redis.Client
|
2025-12-31 08:50:12 +08:00
|
|
|
|
slotTTLSeconds int // 槽位过期时间(秒)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-31 08:50:12 +08:00
|
|
|
|
// NewConcurrencyCache 创建并发控制缓存
|
|
|
|
|
|
// slotTTLMinutes: 槽位过期时间(分钟),0 或负数使用默认值 15 分钟
|
|
|
|
|
|
func NewConcurrencyCache(rdb *redis.Client, slotTTLMinutes int) service.ConcurrencyCache {
|
|
|
|
|
|
if slotTTLMinutes <= 0 {
|
|
|
|
|
|
slotTTLMinutes = defaultSlotTTLMinutes
|
|
|
|
|
|
}
|
|
|
|
|
|
return &concurrencyCache{
|
2025-12-31 14:51:58 +08:00
|
|
|
|
rdb: rdb,
|
2025-12-31 08:50:12 +08:00
|
|
|
|
slotTTLSeconds: slotTTLMinutes * 60,
|
|
|
|
|
|
}
|
2025-12-19 23:39:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-24 21:00:29 +08:00
|
|
|
|
// Helper functions for key generation
|
2025-12-31 08:50:12 +08:00
|
|
|
|
func accountSlotKey(accountID int64) string {
|
|
|
|
|
|
return fmt.Sprintf("%s%d", accountSlotKeyPrefix, accountID)
|
2025-12-24 21:00:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-31 08:50:12 +08:00
|
|
|
|
func userSlotKey(userID int64) string {
|
|
|
|
|
|
return fmt.Sprintf("%s%d", userSlotKeyPrefix, userID)
|
2025-12-24 21:00:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func waitQueueKey(userID int64) string {
|
|
|
|
|
|
return fmt.Sprintf("%s%d", waitQueueKeyPrefix, userID)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Account slot operations
|
|
|
|
|
|
|
|
|
|
|
|
func (c *concurrencyCache) AcquireAccountSlot(ctx context.Context, accountID int64, maxConcurrency int, requestID string) (bool, error) {
|
2025-12-31 08:50:12 +08:00
|
|
|
|
key := accountSlotKey(accountID)
|
|
|
|
|
|
// 时间戳在 Lua 脚本内使用 Redis TIME 命令获取,确保多实例时钟一致
|
|
|
|
|
|
result, err := acquireScript.Run(ctx, c.rdb, []string{key}, maxConcurrency, c.slotTTLSeconds, requestID).Int()
|
2025-12-19 23:39:28 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return result == 1, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-24 21:00:29 +08:00
|
|
|
|
func (c *concurrencyCache) ReleaseAccountSlot(ctx context.Context, accountID int64, requestID string) error {
|
2025-12-31 08:50:12 +08:00
|
|
|
|
key := accountSlotKey(accountID)
|
|
|
|
|
|
return c.rdb.ZRem(ctx, key, requestID).Err()
|
2025-12-19 23:39:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *concurrencyCache) GetAccountConcurrency(ctx context.Context, accountID int64) (int, error) {
|
2025-12-31 08:50:12 +08:00
|
|
|
|
key := accountSlotKey(accountID)
|
|
|
|
|
|
// 时间戳在 Lua 脚本内使用 Redis TIME 命令获取
|
|
|
|
|
|
result, err := getCountScript.Run(ctx, c.rdb, []string{key}, c.slotTTLSeconds).Int()
|
2025-12-24 21:00:29 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return result, nil
|
2025-12-19 23:39:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-24 21:00:29 +08:00
|
|
|
|
// User slot operations
|
|
|
|
|
|
|
|
|
|
|
|
func (c *concurrencyCache) AcquireUserSlot(ctx context.Context, userID int64, maxConcurrency int, requestID string) (bool, error) {
|
2025-12-31 08:50:12 +08:00
|
|
|
|
key := userSlotKey(userID)
|
|
|
|
|
|
// 时间戳在 Lua 脚本内使用 Redis TIME 命令获取,确保多实例时钟一致
|
|
|
|
|
|
result, err := acquireScript.Run(ctx, c.rdb, []string{key}, maxConcurrency, c.slotTTLSeconds, requestID).Int()
|
2025-12-19 23:39:28 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return result == 1, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-24 21:00:29 +08:00
|
|
|
|
func (c *concurrencyCache) ReleaseUserSlot(ctx context.Context, userID int64, requestID string) error {
|
2025-12-31 08:50:12 +08:00
|
|
|
|
key := userSlotKey(userID)
|
|
|
|
|
|
return c.rdb.ZRem(ctx, key, requestID).Err()
|
2025-12-19 23:39:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *concurrencyCache) GetUserConcurrency(ctx context.Context, userID int64) (int, error) {
|
2025-12-31 08:50:12 +08:00
|
|
|
|
key := userSlotKey(userID)
|
|
|
|
|
|
// 时间戳在 Lua 脚本内使用 Redis TIME 命令获取
|
|
|
|
|
|
result, err := getCountScript.Run(ctx, c.rdb, []string{key}, c.slotTTLSeconds).Int()
|
2025-12-24 21:00:29 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return result, nil
|
2025-12-19 23:39:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-24 21:00:29 +08:00
|
|
|
|
// Wait queue operations
|
|
|
|
|
|
|
2025-12-19 23:39:28 +08:00
|
|
|
|
func (c *concurrencyCache) IncrementWaitCount(ctx context.Context, userID int64, maxWait int) (bool, error) {
|
2025-12-24 21:00:29 +08:00
|
|
|
|
key := waitQueueKey(userID)
|
2025-12-31 08:50:12 +08:00
|
|
|
|
result, err := incrementWaitScript.Run(ctx, c.rdb, []string{key}, maxWait, c.slotTTLSeconds).Int()
|
2025-12-19 23:39:28 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return result == 1, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *concurrencyCache) DecrementWaitCount(ctx context.Context, userID int64) error {
|
2025-12-24 21:00:29 +08:00
|
|
|
|
key := waitQueueKey(userID)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
_, err := decrementWaitScript.Run(ctx, c.rdb, []string{key}).Result()
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|