go-micro.dev/v5@v5.12.0/registry/options.go (about)

     1  package registry
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"time"
     7  
     8  	"go-micro.dev/v5/logger"
     9  )
    10  
    11  type Options struct {
    12  	Logger logger.Logger
    13  	// Other options for implementations of the interface
    14  	// can be stored in a context
    15  	Context   context.Context
    16  	TLSConfig *tls.Config
    17  	Addrs     []string
    18  	Timeout   time.Duration
    19  	Secure    bool
    20  }
    21  
    22  type RegisterOptions struct {
    23  	// Other options for implementations of the interface
    24  	// can be stored in a context
    25  	Context context.Context
    26  	TTL     time.Duration
    27  }
    28  
    29  type WatchOptions struct {
    30  	// Other options for implementations of the interface
    31  	// can be stored in a context
    32  	Context context.Context
    33  	// Specify a service to watch
    34  	// If blank, the watch is for all services
    35  	Service string
    36  }
    37  
    38  type DeregisterOptions struct {
    39  	Context context.Context
    40  }
    41  
    42  type GetOptions struct {
    43  	Context context.Context
    44  }
    45  
    46  type ListOptions struct {
    47  	Context context.Context
    48  }
    49  
    50  func NewOptions(opts ...Option) *Options {
    51  	options := Options{
    52  		Context: context.Background(),
    53  		Logger:  logger.DefaultLogger,
    54  	}
    55  
    56  	for _, o := range opts {
    57  		o(&options)
    58  	}
    59  
    60  	return &options
    61  }
    62  
    63  // Addrs is the registry addresses to use.
    64  func Addrs(addrs ...string) Option {
    65  	return func(o *Options) {
    66  		o.Addrs = addrs
    67  	}
    68  }
    69  
    70  func Timeout(t time.Duration) Option {
    71  	return func(o *Options) {
    72  		o.Timeout = t
    73  	}
    74  }
    75  
    76  // Secure communication with the registry.
    77  func Secure(b bool) Option {
    78  	return func(o *Options) {
    79  		o.Secure = b
    80  	}
    81  }
    82  
    83  // Specify TLS Config.
    84  func TLSConfig(t *tls.Config) Option {
    85  	return func(o *Options) {
    86  		o.TLSConfig = t
    87  	}
    88  }
    89  
    90  func RegisterTTL(t time.Duration) RegisterOption {
    91  	return func(o *RegisterOptions) {
    92  		o.TTL = t
    93  	}
    94  }
    95  
    96  func RegisterContext(ctx context.Context) RegisterOption {
    97  	return func(o *RegisterOptions) {
    98  		o.Context = ctx
    99  	}
   100  }
   101  
   102  // Watch a service.
   103  func WatchService(name string) WatchOption {
   104  	return func(o *WatchOptions) {
   105  		o.Service = name
   106  	}
   107  }
   108  
   109  func WatchContext(ctx context.Context) WatchOption {
   110  	return func(o *WatchOptions) {
   111  		o.Context = ctx
   112  	}
   113  }
   114  
   115  func DeregisterContext(ctx context.Context) DeregisterOption {
   116  	return func(o *DeregisterOptions) {
   117  		o.Context = ctx
   118  	}
   119  }
   120  
   121  func GetContext(ctx context.Context) GetOption {
   122  	return func(o *GetOptions) {
   123  		o.Context = ctx
   124  	}
   125  }
   126  
   127  func ListContext(ctx context.Context) ListOption {
   128  	return func(o *ListOptions) {
   129  		o.Context = ctx
   130  	}
   131  }
   132  
   133  type servicesKey struct{}
   134  
   135  func getServiceRecords(ctx context.Context) map[string]map[string]*record {
   136  	memServices, ok := ctx.Value(servicesKey{}).(map[string][]*Service)
   137  	if !ok {
   138  		return nil
   139  	}
   140  
   141  	services := make(map[string]map[string]*record)
   142  
   143  	for name, svc := range memServices {
   144  		if _, ok := services[name]; !ok {
   145  			services[name] = make(map[string]*record)
   146  		}
   147  		// go through every version of the service
   148  		for _, s := range svc {
   149  			services[s.Name][s.Version] = serviceToRecord(s, 0)
   150  		}
   151  	}
   152  
   153  	return services
   154  }
   155  
   156  // Services is an option that preloads service data.
   157  func Services(s map[string][]*Service) Option {
   158  	return func(o *Options) {
   159  		if o.Context == nil {
   160  			o.Context = context.Background()
   161  		}
   162  		o.Context = context.WithValue(o.Context, servicesKey{}, s)
   163  	}
   164  }
   165  
   166  // Logger sets the underline logger.
   167  func Logger(l logger.Logger) Option {
   168  	return func(o *Options) {
   169  		o.Logger = l
   170  	}
   171  }