2021-01-29 13:17:32 +03:00
|
|
|
package register
|
2015-12-19 21:28:08 +03:00
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2016-01-17 02:39:47 +03:00
|
|
|
"crypto/tls"
|
2015-12-19 21:28:08 +03:00
|
|
|
"time"
|
2020-08-29 17:44:49 +03:00
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2021-01-22 23:32:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
|
|
|
"github.com/unistack-org/micro/v3/tracer"
|
2015-12-19 21:28:08 +03:00
|
|
|
)
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// Options holds options for register
|
2015-12-19 21:28:08 +03:00
|
|
|
type Options struct {
|
2021-01-29 13:17:32 +03:00
|
|
|
Name string
|
2016-03-16 01:20:21 +03:00
|
|
|
Addrs []string
|
2016-01-17 02:39:47 +03:00
|
|
|
Timeout time.Duration
|
|
|
|
TLSConfig *tls.Config
|
2020-12-09 12:10:25 +03:00
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Logger that will be used
|
2020-12-09 12:10:25 +03:00
|
|
|
Logger logger.Logger
|
2021-01-22 23:32:33 +03:00
|
|
|
// Meter that will be used
|
|
|
|
Meter meter.Meter
|
|
|
|
// Tracer
|
|
|
|
Tracer tracer.Tracer
|
2016-01-06 19:25:12 +03:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-19 21:28:08 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// NewOptions returns options that filled by opts
|
2020-09-05 02:11:29 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2020-09-03 15:11:05 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
2021-01-22 23:32:33 +03:00
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
2020-09-03 15:11:05 +03:00
|
|
|
Context: context.Background(),
|
|
|
|
}
|
2020-09-05 02:11:29 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
2020-09-03 15:11:05 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RegisterOptions holds options for register method
|
2016-01-27 02:32:27 +03:00
|
|
|
type RegisterOptions struct {
|
|
|
|
TTL time.Duration
|
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2020-06-17 15:23:41 +03:00
|
|
|
// Domain to register the service in
|
|
|
|
Domain string
|
2020-10-16 09:38:57 +03:00
|
|
|
// Attempts specify attempts for register
|
|
|
|
Attempts int
|
2016-01-27 02:32:27 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// NewRegisterOptions returns register options struct filled by opts
|
2020-11-05 21:17:40 +03:00
|
|
|
func NewRegisterOptions(opts ...RegisterOption) RegisterOptions {
|
|
|
|
options := RegisterOptions{
|
|
|
|
Domain: DefaultDomain,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// WatchOptions holds watch options
|
2018-02-19 20:12:37 +03:00
|
|
|
type WatchOptions struct {
|
|
|
|
// Specify a service to watch
|
|
|
|
// If blank, the watch is for all services
|
|
|
|
Service string
|
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2020-06-17 15:23:41 +03:00
|
|
|
// Domain to watch
|
|
|
|
Domain string
|
2018-02-19 20:12:37 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// NewWatchOptions returns watch options filled by opts
|
2020-11-05 21:17:40 +03:00
|
|
|
func NewWatchOptions(opts ...WatchOption) WatchOptions {
|
|
|
|
options := WatchOptions{
|
|
|
|
Domain: DefaultDomain,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// DeregisterOptions holds options for deregister method
|
2020-04-14 14:32:59 +03:00
|
|
|
type DeregisterOptions struct {
|
|
|
|
Context context.Context
|
2020-06-17 15:23:41 +03:00
|
|
|
// Domain the service was registered in
|
|
|
|
Domain string
|
2020-10-16 09:38:57 +03:00
|
|
|
// Atempts specify max attempts for deregister
|
|
|
|
Attempts int
|
2020-04-14 14:32:59 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// NewDeregisterOptions returns options for deregister filled by opts
|
2020-11-05 21:17:40 +03:00
|
|
|
func NewDeregisterOptions(opts ...DeregisterOption) DeregisterOptions {
|
|
|
|
options := DeregisterOptions{
|
|
|
|
Domain: DefaultDomain,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// LookupOptions holds lookup options
|
2021-01-29 13:17:32 +03:00
|
|
|
type LookupOptions struct {
|
2020-04-14 14:32:59 +03:00
|
|
|
Context context.Context
|
2020-06-17 15:23:41 +03:00
|
|
|
// Domain to scope the request to
|
|
|
|
Domain string
|
2020-04-14 14:32:59 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// NewLookupOptions returns lookup options filled by opts
|
2021-01-29 13:17:32 +03:00
|
|
|
func NewLookupOptions(opts ...LookupOption) LookupOptions {
|
|
|
|
options := LookupOptions{
|
2020-11-05 21:17:40 +03:00
|
|
|
Domain: DefaultDomain,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// ListOptions holds the list options for list method
|
2020-04-14 14:32:59 +03:00
|
|
|
type ListOptions struct {
|
|
|
|
Context context.Context
|
2020-06-17 15:23:41 +03:00
|
|
|
// Domain to scope the request to
|
|
|
|
Domain string
|
2020-04-14 14:32:59 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// NewListOptions returns list options filled by opts
|
2020-11-05 21:17:40 +03:00
|
|
|
func NewListOptions(opts ...ListOption) ListOptions {
|
|
|
|
options := ListOptions{
|
|
|
|
Domain: DefaultDomain,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Addrs is the register addresses to use
|
2016-03-16 01:20:21 +03:00
|
|
|
func Addrs(addrs ...string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Addrs = addrs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Timeout sets the timeout
|
2015-12-19 21:28:08 +03:00
|
|
|
func Timeout(t time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Timeout = t
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 23:25:18 +03:00
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Logger sets the logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
2016-01-16 23:25:18 +03:00
|
|
|
return func(o *Options) {
|
2021-01-22 23:32:33 +03:00
|
|
|
o.Logger = l
|
2016-01-16 23:25:18 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-17 02:39:47 +03:00
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Meter sets the meter
|
|
|
|
func Meter(m meter.Meter) Option {
|
2020-08-29 17:44:49 +03:00
|
|
|
return func(o *Options) {
|
2021-01-22 23:32:33 +03:00
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracer sets the tracer
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
// Context sets the context
|
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// TLSConfig Specify TLS Config
|
2016-01-17 02:39:47 +03:00
|
|
|
func TLSConfig(t *tls.Config) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TLSConfig = t
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 02:32:27 +03:00
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RegisterAttempts specifies register atempts count
|
2020-10-16 09:38:57 +03:00
|
|
|
func RegisterAttempts(t int) RegisterOption {
|
|
|
|
return func(o *RegisterOptions) {
|
|
|
|
o.Attempts = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RegisterTTL specifies register ttl
|
2016-01-27 15:23:18 +03:00
|
|
|
func RegisterTTL(t time.Duration) RegisterOption {
|
2016-01-27 02:32:27 +03:00
|
|
|
return func(o *RegisterOptions) {
|
|
|
|
o.TTL = t
|
|
|
|
}
|
|
|
|
}
|
2018-02-19 20:12:37 +03:00
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RegisterContext sets the register context
|
2020-04-14 14:32:59 +03:00
|
|
|
func RegisterContext(ctx context.Context) RegisterOption {
|
|
|
|
return func(o *RegisterOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RegisterDomain secifies register domain
|
2020-06-17 15:23:41 +03:00
|
|
|
func RegisterDomain(d string) RegisterOption {
|
|
|
|
return func(o *RegisterOptions) {
|
|
|
|
o.Domain = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// WatchService name
|
2018-02-19 20:12:37 +03:00
|
|
|
func WatchService(name string) WatchOption {
|
|
|
|
return func(o *WatchOptions) {
|
|
|
|
o.Service = name
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 14:32:59 +03:00
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// WatchContext sets the context for watch method
|
2020-04-14 14:32:59 +03:00
|
|
|
func WatchContext(ctx context.Context) WatchOption {
|
|
|
|
return func(o *WatchOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// WatchDomain sets the domain for watch
|
2020-06-17 15:23:41 +03:00
|
|
|
func WatchDomain(d string) WatchOption {
|
|
|
|
return func(o *WatchOptions) {
|
|
|
|
o.Domain = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// DeregisterAttempts specifies deregister atempts count
|
|
|
|
func DeregisterAttempts(t int) DeregisterOption {
|
2020-10-16 09:38:57 +03:00
|
|
|
return func(o *DeregisterOptions) {
|
|
|
|
o.Attempts = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// DeregisterContext sets the context for deregister method
|
2020-04-14 14:32:59 +03:00
|
|
|
func DeregisterContext(ctx context.Context) DeregisterOption {
|
|
|
|
return func(o *DeregisterOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// DeregisterDomain specifies deregister domain
|
2020-06-17 15:23:41 +03:00
|
|
|
func DeregisterDomain(d string) DeregisterOption {
|
|
|
|
return func(o *DeregisterOptions) {
|
|
|
|
o.Domain = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// LookupContext sets the context for lookup method
|
2021-01-29 15:06:47 +03:00
|
|
|
func LookupContext(ctx context.Context) LookupOption {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *LookupOptions) {
|
2020-04-14 14:32:59 +03:00
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// LookupDomain sets the domain for lookup
|
2021-01-29 15:06:47 +03:00
|
|
|
func LookupDomain(d string) LookupOption {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *LookupOptions) {
|
2020-06-17 15:23:41 +03:00
|
|
|
o.Domain = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// ListContext specifies context for list method
|
2020-04-14 14:32:59 +03:00
|
|
|
func ListContext(ctx context.Context) ListOption {
|
|
|
|
return func(o *ListOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 15:23:41 +03:00
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// ListDomain sets the domain for list method
|
2020-06-17 15:23:41 +03:00
|
|
|
func ListDomain(d string) ListOption {
|
|
|
|
return func(o *ListOptions) {
|
|
|
|
o.Domain = d
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 14:07:35 +03:00
|
|
|
|
|
|
|
// Name sets the name
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|