修改集群模式使用封装的方法
This commit is contained in:
+20
-1
@@ -3,6 +3,7 @@ package priority
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/yuninks/timerx/logger"
|
||||
)
|
||||
|
||||
@@ -10,15 +11,21 @@ type Options struct {
|
||||
getInterval time.Duration // 查询周期
|
||||
updateInterval time.Duration // 更新间隔
|
||||
expireTime time.Duration // 有效时间
|
||||
logger logger.Logger
|
||||
logger logger.Logger // 日志
|
||||
source string // 来源服务
|
||||
instanceId string // 实例ID
|
||||
}
|
||||
|
||||
func defaultOptions() Options {
|
||||
|
||||
u, _ := uuid.NewV7()
|
||||
|
||||
return Options{
|
||||
getInterval: time.Second * 2,
|
||||
updateInterval: time.Second * 4,
|
||||
expireTime: time.Second * 8,
|
||||
logger: logger.NewLogger(),
|
||||
instanceId: u.String(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,3 +56,15 @@ func WithUpdateInterval(d time.Duration) Option {
|
||||
o.getInterval = d / 3
|
||||
}
|
||||
}
|
||||
|
||||
func WithInstanceId(instanceId string) Option {
|
||||
return func(o *Options) {
|
||||
o.instanceId = instanceId
|
||||
}
|
||||
}
|
||||
|
||||
func WithSource(s string) Option {
|
||||
return func(o *Options) {
|
||||
o.source = s
|
||||
}
|
||||
}
|
||||
|
||||
+13
-10
@@ -15,21 +15,23 @@ import (
|
||||
// 多版本场景判断当前是否最新版本
|
||||
|
||||
type Priority struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
priority int64 // 优先级
|
||||
redis redis.UniversalClient
|
||||
redisKey string
|
||||
logger logger.Logger
|
||||
expireTime time.Duration
|
||||
ctx context.Context // 上下文
|
||||
cancel context.CancelFunc // 取消函数
|
||||
priority int64 // 优先级
|
||||
redis redis.UniversalClient // redis
|
||||
redisKey string // redis key
|
||||
logger logger.Logger // 日志
|
||||
expireTime time.Duration // 过期时间
|
||||
|
||||
setInterval time.Duration // 尝试set的间隔
|
||||
getInterval time.Duration // 尝试get的间隔
|
||||
|
||||
wg sync.WaitGroup
|
||||
|
||||
isLatest bool
|
||||
latestMux sync.RWMutex
|
||||
isLatest bool // 是否是最新版本
|
||||
latestMux sync.RWMutex // 最新版本锁
|
||||
|
||||
instanceId string // 实例ID
|
||||
}
|
||||
|
||||
func InitPriority(ctx context.Context, re redis.UniversalClient, keyPrefix string, priority int64, opts ...Option) (*Priority, error) {
|
||||
@@ -48,10 +50,11 @@ func InitPriority(ctx context.Context, re redis.UniversalClient, keyPrefix strin
|
||||
priority: priority,
|
||||
redis: re,
|
||||
logger: conf.logger,
|
||||
redisKey: "timer:priority_" + keyPrefix,
|
||||
redisKey: "timer:priority_" + conf.source + keyPrefix,
|
||||
expireTime: conf.expireTime,
|
||||
setInterval: conf.updateInterval,
|
||||
getInterval: conf.getInterval,
|
||||
instanceId: conf.instanceId,
|
||||
}
|
||||
|
||||
pro.startDaemon()
|
||||
|
||||
Reference in New Issue
Block a user