2019-05-31 00:43:23 +01:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2020-04-11 10:37:54 +01:00
|
|
|
"time"
|
2020-08-29 17:44:49 +03:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/meter"
|
|
|
|
"go.unistack.org/micro/v4/tracer"
|
2019-05-31 00:43:23 +01:00
|
|
|
)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Options holds the sync options
|
2020-08-29 17:44:49 +03:00
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Logger used for logging
|
2020-08-29 17:44:49 +03:00
|
|
|
Logger logger.Logger
|
2021-03-06 19:45:13 +03:00
|
|
|
// Tracer used for tracing
|
2021-01-22 23:32:33 +03:00
|
|
|
Tracer tracer.Tracer
|
2021-03-06 19:45:13 +03:00
|
|
|
// Meter used for merics
|
|
|
|
Meter meter.Meter
|
|
|
|
// Prefix holds prefix?
|
|
|
|
Prefix string
|
|
|
|
// Nodes holds the nodes
|
|
|
|
// TODO: change to Addrs ?
|
|
|
|
Nodes []string
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Option func signature
|
2020-08-29 17:44:49 +03:00
|
|
|
type Option func(o *Options)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// NewOptions returns options that filled by opts
|
2021-01-22 23:32:33 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// LeaderOptions holds the leader options
|
2020-08-29 17:44:49 +03:00
|
|
|
type LeaderOptions struct{}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// LeaderOption func signature
|
2020-08-29 17:44:49 +03:00
|
|
|
type LeaderOption func(o *LeaderOptions)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// LockOptions holds the lock options
|
2020-08-29 17:44:49 +03:00
|
|
|
type LockOptions struct {
|
|
|
|
TTL time.Duration
|
|
|
|
Wait time.Duration
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// LockOption func signature
|
2020-08-29 17:44:49 +03:00
|
|
|
type LockOption func(o *LockOptions)
|
|
|
|
|
|
|
|
// Logger sets the logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Meter sets the logger
|
|
|
|
func Meter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracer sets the tracer
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 10:37:54 +01:00
|
|
|
// Nodes sets the addresses to use
|
|
|
|
func Nodes(a ...string) Option {
|
2019-05-31 00:43:23 +01:00
|
|
|
return func(o *Options) {
|
2020-04-11 10:37:54 +01:00
|
|
|
o.Nodes = a
|
2019-05-31 00:43:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 10:37:54 +01:00
|
|
|
// Prefix sets a prefix to any lock ids used
|
|
|
|
func Prefix(p string) Option {
|
2019-05-31 00:43:23 +01:00
|
|
|
return func(o *Options) {
|
2020-04-11 10:37:54 +01:00
|
|
|
o.Prefix = p
|
2019-05-31 00:43:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 10:37:54 +01:00
|
|
|
// LockTTL sets the lock ttl
|
|
|
|
func LockTTL(t time.Duration) LockOption {
|
|
|
|
return func(o *LockOptions) {
|
|
|
|
o.TTL = t
|
2019-05-31 00:43:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 10:37:54 +01:00
|
|
|
// LockWait sets the wait time
|
|
|
|
func LockWait(t time.Duration) LockOption {
|
|
|
|
return func(o *LockOptions) {
|
|
|
|
o.Wait = t
|
2019-05-31 00:43:23 +01:00
|
|
|
}
|
|
|
|
}
|