Files
iotlcm/internal/queue/zset/producer_lease.go
T
MiFaZhan 2bec257289 chore: 初始开源发布
iotlcm-worker-svr:异步执行中枢(RabbitMQ 消费者 + 全量同步编排)。

- Go module:iotlcm-worker-svr → git.5iot.com/iotlcm-public/iotlcm-worker-svr(214 文件)
- 凭据与内部标识脱敏:MySQL/Redis 凭据、私网与生产 IP、内网域名、
  库名 xiujie_admin → iotlcm、哨兵名占位
- 159 处真实手机号 → 合法 11 位编造号(保留号段与后 4 位,维持互异,
  不破坏「校验 MSISDN 格式」等测试断言)
- 移除内部资产:CI 触发器、Jenkins 触发测试文档、预演门禁与镜像
  digest 记录
- Dockerfile 改用公共基础镜像(golang:1.25-alpine → alpine:3.23),
  移除写死的 git 凭据
- README 重写(架构/全量同步模型/配置/已知失败),补 Apache-2.0
  LICENSE,LF 行尾 + .gitattributes

已知:3 个测试包在源仓同一提交上同样失败(client/upstream、
service/allocate、service/inbound),属既有缺陷,本仓未引入亦未修复。
2026-10-22 16:37:56 +08:00

65 lines
2.5 KiB
Go

package zset
import (
"context"
"strings"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/errors/gerror"
)
var ErrProducerLeaseLost = gerror.New("producer lease lost")
// WithIMEIProducerLease returns a private producer view; the shared store and
// other producer/consumer protocols are unchanged. The IMEI caller must reject
// Redis Cluster before using this view: the global lock and run keys cross slots.
func (s *Store) WithIMEIProducerLease(key, owner, runID string) (*Store, error) {
if key == "" || owner == "" || runID == "" {
return nil, gerror.New("producer lease key, owner and runId are required")
}
view := *s
view.redis = &imeiProducerRedis{RedisClient: s.redis, key: key, owner: owner, runID: runID}
return &view, nil
}
type imeiProducerRedis struct {
RedisClient
key, owner, runID string
}
func (r *imeiProducerRedis) Eval(ctx context.Context, script string, numKeys int64, keys []string, args []any) (*gvar.Var, error) {
leaseKeys := append(append([]string(nil), keys...), r.key)
leaseArgs := append(append([]any(nil), args...), r.owner, r.runID)
result, err := r.RedisClient.Eval(ctx, imeiProducerLeaseGuard+script, numKeys+1, leaseKeys, leaseArgs)
if err != nil && strings.Contains(err.Error(), "IMEI_PRODUCER_LEASE_LOST") {
return nil, gerror.Wrap(ErrProducerLeaseLost, "reject stale IMEI producer write")
}
return result, err
}
func (r *imeiProducerRedis) Do(ctx context.Context, command string, args ...any) (*gvar.Var, error) {
switch strings.ToUpper(command) {
case "HGETALL", "SMEMBERS": // The producer's metadata/capacity reads are not mutations.
return r.RedisClient.Do(ctx, command, args...)
case "SADD", "HSET": // CreateRun also writes the index and metadata extensions.
commandArgs := append([]any{command}, args...)
return r.Eval(ctx, "return redis.call(unpack(ARGV))", 0, nil, commandArgs)
default:
return nil, gerror.Newf("unsupported command in IMEI producer lease view: %s", command)
}
}
// Remove guard-only arguments before executing the original queue script, so
// task-pair loops and key indexing retain their original semantics.
const imeiProducerLeaseGuard = `
local leaseRunID = table.remove(ARGV)
local leaseOwner = table.remove(ARGV)
local leaseKey = table.remove(KEYS)
local current = redis.call('GET', leaseKey)
if not current then return redis.error_reply('IMEI_PRODUCER_LEASE_LOST') end
local ok, lease = pcall(cjson.decode, current)
if not ok or type(lease) ~= 'table' or lease['owner'] ~= leaseOwner or lease['runId'] ~= leaseRunID then
return redis.error_reply('IMEI_PRODUCER_LEASE_LOST')
end
`