micro/sync/options.go

101 lines
1.7 KiB
Go
Raw Normal View History

2019-05-31 02:43:23 +03:00
package sync
import (
2020-04-11 12:37:54 +03:00
"time"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/meter"
"github.com/unistack-org/micro/v3/tracer"
2019-05-31 02:43:23 +03:00
)
// Options holds the sync options
type Options struct {
Nodes []string
Prefix string
Logger logger.Logger
Tracer tracer.Tracer
Meter meter.Meter
}
// Option func signature
type Option func(o *Options)
// NewOptions returns options that filled by opts
func NewOptions(opts ...Option) Options {
options := Options{
Logger: logger.DefaultLogger,
Meter: meter.DefaultMeter,
Tracer: tracer.DefaultTracer,
}
for _, o := range opts {
o(&options)
}
return options
}
// LeaderOptions holds the leader options
type LeaderOptions struct{}
// LeaderOption func signature
type LeaderOption func(o *LeaderOptions)
// LockOptions holds the lock options
type LockOptions struct {
TTL time.Duration
Wait time.Duration
}
// LockOption func signature
type LockOption func(o *LockOptions)
// Logger sets the logger
func Logger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}
// 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 12:37:54 +03:00
// Nodes sets the addresses to use
func Nodes(a ...string) Option {
2019-05-31 02:43:23 +03:00
return func(o *Options) {
2020-04-11 12:37:54 +03:00
o.Nodes = a
2019-05-31 02:43:23 +03:00
}
}
2020-04-11 12:37:54 +03:00
// Prefix sets a prefix to any lock ids used
func Prefix(p string) Option {
2019-05-31 02:43:23 +03:00
return func(o *Options) {
2020-04-11 12:37:54 +03:00
o.Prefix = p
2019-05-31 02:43:23 +03:00
}
}
2020-04-11 12:37:54 +03:00
// LockTTL sets the lock ttl
func LockTTL(t time.Duration) LockOption {
return func(o *LockOptions) {
o.TTL = t
2019-05-31 02:43:23 +03:00
}
}
2020-04-11 12:37:54 +03:00
// LockWait sets the wait time
func LockWait(t time.Duration) LockOption {
return func(o *LockOptions) {
o.Wait = t
2019-05-31 02:43:23 +03:00
}
}