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