trpc.group/trpc-go/trpc-go@v1.0.3/naming/selector/options.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package selector 15 16 import ( 17 "context" 18 19 "trpc.group/trpc-go/trpc-go/naming/circuitbreaker" 20 "trpc.group/trpc-go/trpc-go/naming/discovery" 21 "trpc.group/trpc-go/trpc-go/naming/loadbalance" 22 "trpc.group/trpc-go/trpc-go/naming/servicerouter" 23 ) 24 25 var ( 26 defaultDiscoveryOptionsSize = 2 27 defaultServiceRouterOptionsSize = 2 28 defaultLoadBalanceOptionsSize = 2 29 ) 30 31 // Options defines the call options. 32 type Options struct { 33 // Ctx is the corresponding context to request. 34 Ctx context.Context 35 // Key is the hash key of stateful routing. 36 Key string 37 // Replicas is the replicas of a single node for stateful routing. It's optional, and used to 38 // address hash ring. 39 Replicas int 40 // EnvKey is the environment key. 41 EnvKey string 42 // Namespace is the callee namespace. 43 Namespace string 44 // SourceNamespace is the caller namespace. 45 SourceNamespace string 46 // SourceServiceName is the caller service name. 47 SourceServiceName string 48 // SourceEnvName is the caller environment name. 49 SourceEnvName string 50 // SourceSetName if the caller set group. 51 SourceSetName string 52 // SourceMetadata is the caller metadata used to match routing. 53 SourceMetadata map[string]string 54 // DestinationEnvName is the callee environment name which is used to get node in the specific 55 // environment. 56 DestinationEnvName string 57 // DestinationSetName is callee set name. 58 DestinationSetName string 59 // DestinationMetadata is the callee metadata used to match routing. 60 DestinationMetadata map[string]string 61 // LoadBalanceType is the load balance type. 62 LoadBalanceType string 63 64 // EnvTransfer is the environment of upstream server. 65 EnvTransfer string 66 Discovery discovery.Discovery 67 DiscoveryOptions []discovery.Option 68 ServiceRouter servicerouter.ServiceRouter 69 ServiceRouterOptions []servicerouter.Option 70 LoadBalancer loadbalance.LoadBalancer 71 LoadBalanceOptions []loadbalance.Option 72 CircuitBreaker circuitbreaker.CircuitBreaker 73 DisableServiceRouter bool 74 } 75 76 // Option modifies the Options. 77 type Option func(*Options) 78 79 // WithContext returns an Option which sets the request context. 80 func WithContext(ctx context.Context) Option { 81 return func(o *Options) { 82 o.Ctx = ctx 83 o.DiscoveryOptions = append(o.DiscoveryOptions, discovery.WithContext(ctx)) 84 o.LoadBalanceOptions = append(o.LoadBalanceOptions, loadbalance.WithContext(ctx)) 85 o.ServiceRouterOptions = append(o.ServiceRouterOptions, servicerouter.WithContext(ctx)) 86 } 87 } 88 89 // WithNamespace returns an Option which sets the namespace. 90 func WithNamespace(namespace string) Option { 91 return func(opts *Options) { 92 opts.Namespace = namespace 93 opts.DiscoveryOptions = append(opts.DiscoveryOptions, discovery.WithNamespace(namespace)) 94 opts.LoadBalanceOptions = append(opts.LoadBalanceOptions, loadbalance.WithNamespace(namespace)) 95 opts.ServiceRouterOptions = append(opts.ServiceRouterOptions, servicerouter.WithNamespace(namespace)) 96 } 97 } 98 99 // WithSourceSetName returns an Option which sets the set name. 100 func WithSourceSetName(sourceSetName string) Option { 101 return func(opts *Options) { 102 opts.SourceSetName = sourceSetName 103 opts.ServiceRouterOptions = append(opts.ServiceRouterOptions, servicerouter.WithSourceSetName(sourceSetName)) 104 } 105 } 106 107 // WithKey returns an Option which sets the hash key of stateful routing. 108 func WithKey(k string) Option { 109 return func(o *Options) { 110 o.Key = k 111 o.LoadBalanceOptions = append(o.LoadBalanceOptions, loadbalance.WithKey(k)) 112 } 113 } 114 115 // WithReplicas returns an Option which sets node replicas of stateful routing. 116 func WithReplicas(r int) Option { 117 return func(o *Options) { 118 o.Replicas = r 119 o.LoadBalanceOptions = append(o.LoadBalanceOptions, loadbalance.WithReplicas(r)) 120 } 121 } 122 123 // WithDisableServiceRouter returns an Option which disables the service router. 124 func WithDisableServiceRouter() Option { 125 return func(o *Options) { 126 o.DisableServiceRouter = true 127 o.ServiceRouterOptions = append(o.ServiceRouterOptions, servicerouter.WithDisableServiceRouter()) 128 } 129 } 130 131 // WithDiscovery returns an Option which sets the discovery. 132 func WithDiscovery(d discovery.Discovery) Option { 133 return func(o *Options) { 134 o.Discovery = d 135 } 136 } 137 138 // WithServiceRouter returns an Option which sets the service router. 139 func WithServiceRouter(r servicerouter.ServiceRouter) Option { 140 return func(o *Options) { 141 o.ServiceRouter = r 142 } 143 } 144 145 // WithLoadBalancer returns an Option which sets load balancer. 146 func WithLoadBalancer(b loadbalance.LoadBalancer) Option { 147 return func(o *Options) { 148 o.LoadBalancer = b 149 } 150 } 151 152 // WithLoadBalanceType returns an Option which sets load balance type. 153 func WithLoadBalanceType(name string) Option { 154 return func(o *Options) { 155 o.LoadBalanceType = name 156 o.LoadBalanceOptions = append( 157 o.LoadBalanceOptions, 158 loadbalance.WithLoadBalanceType(name), 159 ) 160 } 161 } 162 163 // WithCircuitBreaker returns an Option which sets circuit breaker. 164 func WithCircuitBreaker(cb circuitbreaker.CircuitBreaker) Option { 165 return func(o *Options) { 166 o.CircuitBreaker = cb 167 } 168 } 169 170 // WithEnvKey returns an Option which sets the environment key. 171 func WithEnvKey(key string) Option { 172 return func(o *Options) { 173 o.EnvKey = key 174 o.ServiceRouterOptions = append(o.ServiceRouterOptions, servicerouter.WithEnvKey(key)) 175 } 176 } 177 178 // WithSourceNamespace returns an Option which sets caller namespace. 179 func WithSourceNamespace(namespace string) Option { 180 return func(o *Options) { 181 o.SourceNamespace = namespace 182 o.ServiceRouterOptions = append(o.ServiceRouterOptions, servicerouter.WithSourceNamespace(namespace)) 183 } 184 } 185 186 // WithSourceServiceName returns an Option which sets caller service name. 187 func WithSourceServiceName(serviceName string) Option { 188 return func(o *Options) { 189 o.SourceServiceName = serviceName 190 o.ServiceRouterOptions = append(o.ServiceRouterOptions, servicerouter.WithSourceServiceName(serviceName)) 191 } 192 } 193 194 // WithDestinationEnvName returns an Option which sets callee environment name. 195 func WithDestinationEnvName(envName string) Option { 196 return func(o *Options) { 197 o.DestinationEnvName = envName 198 o.ServiceRouterOptions = append(o.ServiceRouterOptions, servicerouter.WithDestinationEnvName(envName)) 199 } 200 } 201 202 // WithSourceEnvName returns an Option which sets caller environment name. 203 func WithSourceEnvName(envName string) Option { 204 return func(o *Options) { 205 o.SourceEnvName = envName 206 o.ServiceRouterOptions = append(o.ServiceRouterOptions, servicerouter.WithSourceEnvName(envName)) 207 } 208 } 209 210 // WithEnvTransfer returns an Option which sets the transparent environment information. 211 func WithEnvTransfer(envTransfer string) Option { 212 return func(o *Options) { 213 o.EnvTransfer = envTransfer 214 o.ServiceRouterOptions = append(o.ServiceRouterOptions, servicerouter.WithEnvTransfer(envTransfer)) 215 } 216 } 217 218 // WithDestinationSetName returns an Option which sets callee set name. 219 func WithDestinationSetName(destinationSetName string) Option { 220 return func(o *Options) { 221 o.DestinationSetName = destinationSetName 222 o.ServiceRouterOptions = append(o.ServiceRouterOptions, 223 servicerouter.WithDestinationSetName(destinationSetName)) 224 } 225 } 226 227 // WithSourceMetadata returns an Option which adds a caller metadata k-v. 228 // Do not use this function to set env/set, they have their own Option-s. 229 func WithSourceMetadata(key string, val string) Option { 230 return func(o *Options) { 231 if o.SourceMetadata == nil { 232 o.SourceMetadata = make(map[string]string) 233 } 234 o.SourceMetadata[key] = val 235 o.ServiceRouterOptions = append(o.ServiceRouterOptions, servicerouter.WithSourceMetadata(key, val)) 236 } 237 } 238 239 // WithDestinationMetadata returns an Option which adds a callee metadata k-v. 240 // Do not use this function to set env/set, they have their own Option-s. 241 func WithDestinationMetadata(key string, val string) Option { 242 return func(o *Options) { 243 if o.DestinationMetadata == nil { 244 o.DestinationMetadata = make(map[string]string) 245 } 246 o.DestinationMetadata[key] = val 247 o.ServiceRouterOptions = append(o.ServiceRouterOptions, servicerouter.WithDestinationMetadata(key, val)) 248 } 249 }