1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/registry/options.go

172 lines
3.2 KiB
Go
Raw Normal View History

2015-12-19 20:28:08 +02:00
package registry
import (
2018-03-03 13:53:52 +02:00
"context"
2016-01-17 01:39:47 +02:00
"crypto/tls"
2015-12-19 20:28:08 +02:00
"time"
"go-micro.dev/v4/logger"
2015-12-19 20:28:08 +02:00
)
type Options struct {
2023-04-26 02:16:34 +02:00
Logger logger.Logger
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
TLSConfig *tls.Config
2016-03-16 00:20:21 +02:00
Addrs []string
2016-01-17 01:39:47 +02:00
Timeout time.Duration
Secure bool
2015-12-19 20:28:08 +02:00
}
2016-01-27 01:32:27 +02:00
type RegisterOptions struct {
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
2023-04-26 02:16:34 +02:00
TTL time.Duration
2016-01-27 01:32:27 +02:00
}
2018-02-19 19:12:37 +02:00
type WatchOptions struct {
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
2023-04-26 02:16:34 +02:00
// Specify a service to watch
// If blank, the watch is for all services
Service string
2018-02-19 19:12:37 +02:00
}
2020-04-14 13:32:59 +02:00
type DeregisterOptions struct {
Context context.Context
}
type GetOptions struct {
Context context.Context
}
type ListOptions struct {
Context context.Context
}
func NewOptions(opts ...Option) *Options {
options := Options{
Context: context.Background(),
Logger: logger.DefaultLogger,
}
for _, o := range opts {
o(&options)
}
return &options
}
2022-09-30 16:27:07 +02:00
// Addrs is the registry addresses to use.
2016-03-16 00:20:21 +02:00
func Addrs(addrs ...string) Option {
return func(o *Options) {
o.Addrs = addrs
}
}
2015-12-19 20:28:08 +02:00
func Timeout(t time.Duration) Option {
return func(o *Options) {
o.Timeout = t
}
}
2016-01-16 22:25:18 +02:00
2022-09-30 16:27:07 +02:00
// Secure communication with the registry.
2016-01-16 22:25:18 +02:00
func Secure(b bool) Option {
return func(o *Options) {
o.Secure = b
}
}
2016-01-17 01:39:47 +02:00
2022-09-30 16:27:07 +02:00
// Specify TLS Config.
2016-01-17 01:39:47 +02:00
func TLSConfig(t *tls.Config) Option {
return func(o *Options) {
o.TLSConfig = t
}
}
2016-01-27 01:32:27 +02:00
2016-01-27 14:23:18 +02:00
func RegisterTTL(t time.Duration) RegisterOption {
2016-01-27 01:32:27 +02:00
return func(o *RegisterOptions) {
o.TTL = t
}
}
2018-02-19 19:12:37 +02:00
2020-04-14 13:32:59 +02:00
func RegisterContext(ctx context.Context) RegisterOption {
return func(o *RegisterOptions) {
o.Context = ctx
}
}
2022-09-30 16:27:07 +02:00
// Watch a service.
2018-02-19 19:12:37 +02:00
func WatchService(name string) WatchOption {
return func(o *WatchOptions) {
o.Service = name
}
}
2020-04-14 13:32:59 +02:00
func WatchContext(ctx context.Context) WatchOption {
return func(o *WatchOptions) {
o.Context = ctx
}
}
func DeregisterContext(ctx context.Context) DeregisterOption {
return func(o *DeregisterOptions) {
o.Context = ctx
}
}
func GetContext(ctx context.Context) GetOption {
return func(o *GetOptions) {
o.Context = ctx
}
}
func ListContext(ctx context.Context) ListOption {
return func(o *ListOptions) {
o.Context = ctx
}
}
2020-12-29 17:49:26 +02:00
type servicesKey struct{}
func getServiceRecords(ctx context.Context) map[string]map[string]*record {
memServices, ok := ctx.Value(servicesKey{}).(map[string][]*Service)
if !ok {
return nil
}
services := make(map[string]map[string]*record)
for name, svc := range memServices {
if _, ok := services[name]; !ok {
services[name] = make(map[string]*record)
}
// go through every version of the service
for _, s := range svc {
services[s.Name][s.Version] = serviceToRecord(s, 0)
}
}
return services
}
2022-09-30 16:27:07 +02:00
// Services is an option that preloads service data.
2020-12-29 17:49:26 +02:00
func Services(s map[string][]*Service) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, servicesKey{}, s)
}
}
2022-09-30 16:27:07 +02:00
// Logger sets the underline logger.
func Logger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}