once使用封装的leader和heartbeat
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package leader
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/yuninks/timerx/logger"
|
||||
"github.com/yuninks/timerx/priority"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
logger logger.Logger
|
||||
instanceId string
|
||||
priority *priority.Priority // 全局优先级
|
||||
}
|
||||
|
||||
func defaultOptions() Options {
|
||||
|
||||
u, _ := uuid.NewV7()
|
||||
|
||||
return Options{
|
||||
logger: logger.NewLogger(),
|
||||
instanceId: u.String(),
|
||||
}
|
||||
}
|
||||
|
||||
type Option func(*Options)
|
||||
|
||||
func newOptions(opts ...Option) Options {
|
||||
o := defaultOptions()
|
||||
for _, opt := range opts {
|
||||
opt(&o)
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func WithLogger(log logger.Logger) Option {
|
||||
return func(o *Options) {
|
||||
o.logger = log
|
||||
}
|
||||
}
|
||||
|
||||
func WithPriority(p *priority.Priority) Option {
|
||||
return func(o *Options) {
|
||||
o.priority = p
|
||||
}
|
||||
}
|
||||
|
||||
func WithInstanceId(instanceId string) Option {
|
||||
return func(o *Options) {
|
||||
o.instanceId = instanceId
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user