fix(sharecode): 支持分享码TTL配置并限制每MAC分享码数

- 配置增加 ShareCodeMaxPerMac 和 ShareCodeTTLMin 参数
- ShareCodeCache 支持自定义分享码有效期 TTL
- 生成分享码时使用配置的 TTL 替代固定30分钟
- 共享码处理添加限制,同一MAC有效分享码数量限制
- 路由层、处理器层和任务调度中增加TTL及限额参数传递
- 同步脚本默认同步路径调整,去除默认同步Docker文件,增加docs目录同步
- 修正上传脚本拼接远程路径时尾部斜杠问题
This commit is contained in:
eafonyang
2026-06-12 18:29:08 +08:00
parent 28fb798926
commit 598bbd4998
6 changed files with 82 additions and 41 deletions
+9 -8
View File
@@ -51,7 +51,6 @@ const (
shareImportFlushLockPref = "share:import:flush:lock:"
shareMacIndexPrefix = "share:mac:"
shareCodeLength = 5
shareCodeTTL = 30 * time.Minute
shareMacIndexTTL = 1 * time.Hour
shareImportPendingTTL = 12 * time.Hour
shareCodeCharset = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
@@ -101,19 +100,21 @@ type ShareCodeData struct {
}
type ShareCodeCache struct {
rdb *redis.Client
rdb *redis.Client
codeTTL time.Duration
}
func NewShareCodeCache(rdb *redis.Client) *ShareCodeCache {
return &ShareCodeCache{rdb: rdb}
func NewShareCodeCache(rdb *redis.Client, codeTTL time.Duration) *ShareCodeCache {
return &ShareCodeCache{rdb: rdb, codeTTL: codeTTL}
}
func ShareCodeTTL() time.Duration {
return shareCodeTTL
// ShareCodeTTL returns the configured share code TTL.
func (c *ShareCodeCache) ShareCodeTTL() time.Duration {
return c.codeTTL
}
func (c *ShareCodeCache) Create(ctx context.Context, macAddr, ipAddr string, eqData []byte) (*ShareCodeData, error) {
expireAt := time.Now().Add(shareCodeTTL)
expireAt := time.Now().Add(c.codeTTL)
eqJSON := string(eqData)
for i := 0; i < shareCodeMaxRetries; i++ {
@@ -125,7 +126,7 @@ func (c *ShareCodeCache) Create(ctx context.Context, macAddr, ipAddr string, eqD
key := shareCodeKey(code)
macIdxKey := shareMacIndexKey(macAddr)
ok, err := shareCreateScript.Run(ctx, c.rdb, []string{key, sharePendingSet, macIdxKey},
macAddr, ipAddr, eqJSON, expireAt.Format(time.RFC3339), int(shareCodeTTL.Seconds()), code, expireAt.Unix(), int(shareMacIndexTTL.Seconds()),
macAddr, ipAddr, eqJSON, expireAt.Format(time.RFC3339), int(c.codeTTL.Seconds()), code, expireAt.Unix(), int(shareMacIndexTTL.Seconds()),
).Int()
if err != nil {
return nil, fmt.Errorf("create share code in redis: %w", err)