添加超时时间的限制

This commit is contained in:
Yun
2024-06-22 15:34:49 +08:00
parent 5ca5b31efb
commit 79fda8c78c
2 changed files with 19 additions and 5 deletions
+6 -1
View File
@@ -32,6 +32,7 @@ type Cluster struct {
ctx context.Context
redis redis.UniversalClient
cache *cachex.Cache
timeout time.Duration
logger Logger
keyPrefix string // key前缀
location *time.Location // 根据时区计算的时间
@@ -55,6 +56,7 @@ func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix strin
ctx: ctx,
redis: red,
cache: cachex.NewCache(),
timeout: op.timeout,
logger: op.logger,
keyPrefix: keyPrefix,
location: op.location,
@@ -64,6 +66,9 @@ func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix strin
setKey: "timer:cluster_setKey" + keyPrefix, // 重入集合
}
// 设置锁的超时时间
lockx.InitOption(lockx.SetTimeout(op.timeout))
// 监听任务
go clu.watch()
@@ -418,7 +423,7 @@ type ReJobData struct {
// 执行任务
func (c *Cluster) doTask(ctx context.Context, taskId string) {
ctx, cancel := context.WithCancel(ctx)
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
val, ok := clusterWorkerList.Load(taskId)