去掉单例模式,改为每次初始化均生成一个新的实例

This commit is contained in:
Yun
2025-08-23 18:55:59 +08:00
parent 2a7092ab0a
commit bb5b01071a
4 changed files with 110 additions and 106 deletions
+15 -16
View File
@@ -6,7 +6,6 @@ import (
"fmt"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/go-redis/redis/v8"
@@ -47,8 +46,8 @@ type Callback interface {
Worker(ctx context.Context, taskType string, taskId string, attachData interface{}) *OnceWorkerResp
}
var wo *Once = nil
var once sync.Once
// var wo *Once = nil
// var once sync.Once
type extendData struct {
Delay time.Duration
@@ -62,19 +61,19 @@ func InitOnce(ctx context.Context, re redis.UniversalClient, keyPrefix string, c
}
op := newOptions(opts...)
once.Do(func() {
wo = &Once{
ctx: ctx,
logger: op.logger,
zsetKey: "timer:once_zsetkey" + keyPrefix,
listKey: "timer:once_listkey" + keyPrefix,
redis: re,
worker: call,
keyPrefix: keyPrefix,
}
go wo.getTask()
go wo.watch()
})
// once.Do(func() {
wo := &Once{
ctx: ctx,
logger: op.logger,
zsetKey: "timer:once_zsetkey" + keyPrefix,
listKey: "timer:once_listkey" + keyPrefix,
redis: re,
worker: call,
keyPrefix: keyPrefix,
}
go wo.getTask()
go wo.watch()
// })
return wo
}