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