diff options
author | Nathan Dench <ndenc2@gmail.com> | 2019-05-24 15:16:44 +1000 |
---|---|---|
committer | Nathan Dench <ndenc2@gmail.com> | 2019-05-24 15:16:44 +1000 |
commit | 107c1cdb09c575aa2f61d97f48d8587eb6bada4c (patch) | |
tree | ca7d008643efc555c388baeaf1d986e0b6b3e28c /vendor/google.golang.org/grpc/balancer | |
parent | 844b5a68d8af4791755b8f0ad293cc99f5959183 (diff) | |
download | terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.gz terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.zst terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.zip |
Upgrade to 0.12
Diffstat (limited to 'vendor/google.golang.org/grpc/balancer')
4 files changed, 617 insertions, 0 deletions
diff --git a/vendor/google.golang.org/grpc/balancer/balancer.go b/vendor/google.golang.org/grpc/balancer/balancer.go new file mode 100644 index 0000000..317c2e7 --- /dev/null +++ b/vendor/google.golang.org/grpc/balancer/balancer.go | |||
@@ -0,0 +1,303 @@ | |||
1 | /* | ||
2 | * | ||
3 | * Copyright 2017 gRPC authors. | ||
4 | * | ||
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
6 | * you may not use this file except in compliance with the License. | ||
7 | * You may obtain a copy of the License at | ||
8 | * | ||
9 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
10 | * | ||
11 | * Unless required by applicable law or agreed to in writing, software | ||
12 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
14 | * See the License for the specific language governing permissions and | ||
15 | * limitations under the License. | ||
16 | * | ||
17 | */ | ||
18 | |||
19 | // Package balancer defines APIs for load balancing in gRPC. | ||
20 | // All APIs in this package are experimental. | ||
21 | package balancer | ||
22 | |||
23 | import ( | ||
24 | "context" | ||
25 | "errors" | ||
26 | "net" | ||
27 | "strings" | ||
28 | |||
29 | "google.golang.org/grpc/connectivity" | ||
30 | "google.golang.org/grpc/credentials" | ||
31 | "google.golang.org/grpc/internal" | ||
32 | "google.golang.org/grpc/metadata" | ||
33 | "google.golang.org/grpc/resolver" | ||
34 | ) | ||
35 | |||
36 | var ( | ||
37 | // m is a map from name to balancer builder. | ||
38 | m = make(map[string]Builder) | ||
39 | ) | ||
40 | |||
41 | // Register registers the balancer builder to the balancer map. b.Name | ||
42 | // (lowercased) will be used as the name registered with this builder. | ||
43 | // | ||
44 | // NOTE: this function must only be called during initialization time (i.e. in | ||
45 | // an init() function), and is not thread-safe. If multiple Balancers are | ||
46 | // registered with the same name, the one registered last will take effect. | ||
47 | func Register(b Builder) { | ||
48 | m[strings.ToLower(b.Name())] = b | ||
49 | } | ||
50 | |||
51 | // unregisterForTesting deletes the balancer with the given name from the | ||
52 | // balancer map. | ||
53 | // | ||
54 | // This function is not thread-safe. | ||
55 | func unregisterForTesting(name string) { | ||
56 | delete(m, name) | ||
57 | } | ||
58 | |||
59 | func init() { | ||
60 | internal.BalancerUnregister = unregisterForTesting | ||
61 | } | ||
62 | |||
63 | // Get returns the resolver builder registered with the given name. | ||
64 | // Note that the compare is done in a case-insenstive fashion. | ||
65 | // If no builder is register with the name, nil will be returned. | ||
66 | func Get(name string) Builder { | ||
67 | if b, ok := m[strings.ToLower(name)]; ok { | ||
68 | return b | ||
69 | } | ||
70 | return nil | ||
71 | } | ||
72 | |||
73 | // SubConn represents a gRPC sub connection. | ||
74 | // Each sub connection contains a list of addresses. gRPC will | ||
75 | // try to connect to them (in sequence), and stop trying the | ||
76 | // remainder once one connection is successful. | ||
77 | // | ||
78 | // The reconnect backoff will be applied on the list, not a single address. | ||
79 | // For example, try_on_all_addresses -> backoff -> try_on_all_addresses. | ||
80 | // | ||
81 | // All SubConns start in IDLE, and will not try to connect. To trigger | ||
82 | // the connecting, Balancers must call Connect. | ||
83 | // When the connection encounters an error, it will reconnect immediately. | ||
84 | // When the connection becomes IDLE, it will not reconnect unless Connect is | ||
85 | // called. | ||
86 | // | ||
87 | // This interface is to be implemented by gRPC. Users should not need a | ||
88 | // brand new implementation of this interface. For the situations like | ||
89 | // testing, the new implementation should embed this interface. This allows | ||
90 | // gRPC to add new methods to this interface. | ||
91 | type SubConn interface { | ||
92 | // UpdateAddresses updates the addresses used in this SubConn. | ||
93 | // gRPC checks if currently-connected address is still in the new list. | ||
94 | // If it's in the list, the connection will be kept. | ||
95 | // If it's not in the list, the connection will gracefully closed, and | ||
96 | // a new connection will be created. | ||
97 | // | ||
98 | // This will trigger a state transition for the SubConn. | ||
99 | UpdateAddresses([]resolver.Address) | ||
100 | // Connect starts the connecting for this SubConn. | ||
101 | Connect() | ||
102 | } | ||
103 | |||
104 | // NewSubConnOptions contains options to create new SubConn. | ||
105 | type NewSubConnOptions struct { | ||
106 | // CredsBundle is the credentials bundle that will be used in the created | ||
107 | // SubConn. If it's nil, the original creds from grpc DialOptions will be | ||
108 | // used. | ||
109 | CredsBundle credentials.Bundle | ||
110 | // HealthCheckEnabled indicates whether health check service should be | ||
111 | // enabled on this SubConn | ||
112 | HealthCheckEnabled bool | ||
113 | } | ||
114 | |||
115 | // ClientConn represents a gRPC ClientConn. | ||
116 | // | ||
117 | // This interface is to be implemented by gRPC. Users should not need a | ||
118 | // brand new implementation of this interface. For the situations like | ||
119 | // testing, the new implementation should embed this interface. This allows | ||
120 | // gRPC to add new methods to this interface. | ||
121 | type ClientConn interface { | ||
122 | // NewSubConn is called by balancer to create a new SubConn. | ||
123 | // It doesn't block and wait for the connections to be established. | ||
124 | // Behaviors of the SubConn can be controlled by options. | ||
125 | NewSubConn([]resolver.Address, NewSubConnOptions) (SubConn, error) | ||
126 | // RemoveSubConn removes the SubConn from ClientConn. | ||
127 | // The SubConn will be shutdown. | ||
128 | RemoveSubConn(SubConn) | ||
129 | |||
130 | // UpdateBalancerState is called by balancer to nofity gRPC that some internal | ||
131 | // state in balancer has changed. | ||
132 | // | ||
133 | // gRPC will update the connectivity state of the ClientConn, and will call pick | ||
134 | // on the new picker to pick new SubConn. | ||
135 | UpdateBalancerState(s connectivity.State, p Picker) | ||
136 | |||
137 | // ResolveNow is called by balancer to notify gRPC to do a name resolving. | ||
138 | ResolveNow(resolver.ResolveNowOption) | ||
139 | |||
140 | // Target returns the dial target for this ClientConn. | ||
141 | Target() string | ||
142 | } | ||
143 | |||
144 | // BuildOptions contains additional information for Build. | ||
145 | type BuildOptions struct { | ||
146 | // DialCreds is the transport credential the Balancer implementation can | ||
147 | // use to dial to a remote load balancer server. The Balancer implementations | ||
148 | // can ignore this if it does not need to talk to another party securely. | ||
149 | DialCreds credentials.TransportCredentials | ||
150 | // CredsBundle is the credentials bundle that the Balancer can use. | ||
151 | CredsBundle credentials.Bundle | ||
152 | // Dialer is the custom dialer the Balancer implementation can use to dial | ||
153 | // to a remote load balancer server. The Balancer implementations | ||
154 | // can ignore this if it doesn't need to talk to remote balancer. | ||
155 | Dialer func(context.Context, string) (net.Conn, error) | ||
156 | // ChannelzParentID is the entity parent's channelz unique identification number. | ||
157 | ChannelzParentID int64 | ||
158 | } | ||
159 | |||
160 | // Builder creates a balancer. | ||
161 | type Builder interface { | ||
162 | // Build creates a new balancer with the ClientConn. | ||
163 | Build(cc ClientConn, opts BuildOptions) Balancer | ||
164 | // Name returns the name of balancers built by this builder. | ||
165 | // It will be used to pick balancers (for example in service config). | ||
166 | Name() string | ||
167 | } | ||
168 | |||
169 | // PickOptions contains addition information for the Pick operation. | ||
170 | type PickOptions struct { | ||
171 | // FullMethodName is the method name that NewClientStream() is called | ||
172 | // with. The canonical format is /service/Method. | ||
173 | FullMethodName string | ||
174 | // Header contains the metadata from the RPC's client header. The metadata | ||
175 | // should not be modified; make a copy first if needed. | ||
176 | Header metadata.MD | ||
177 | } | ||
178 | |||
179 | // DoneInfo contains additional information for done. | ||
180 | type DoneInfo struct { | ||
181 | // Err is the rpc error the RPC finished with. It could be nil. | ||
182 | Err error | ||
183 | // Trailer contains the metadata from the RPC's trailer, if present. | ||
184 | Trailer metadata.MD | ||
185 | // BytesSent indicates if any bytes have been sent to the server. | ||
186 | BytesSent bool | ||
187 | // BytesReceived indicates if any byte has been received from the server. | ||
188 | BytesReceived bool | ||
189 | } | ||
190 | |||
191 | var ( | ||
192 | // ErrNoSubConnAvailable indicates no SubConn is available for pick(). | ||
193 | // gRPC will block the RPC until a new picker is available via UpdateBalancerState(). | ||
194 | ErrNoSubConnAvailable = errors.New("no SubConn is available") | ||
195 | // ErrTransientFailure indicates all SubConns are in TransientFailure. | ||
196 | // WaitForReady RPCs will block, non-WaitForReady RPCs will fail. | ||
197 | ErrTransientFailure = errors.New("all SubConns are in TransientFailure") | ||
198 | ) | ||
199 | |||
200 | // Picker is used by gRPC to pick a SubConn to send an RPC. | ||
201 | // Balancer is expected to generate a new picker from its snapshot every time its | ||
202 | // internal state has changed. | ||
203 | // | ||
204 | // The pickers used by gRPC can be updated by ClientConn.UpdateBalancerState(). | ||
205 | type Picker interface { | ||
206 | // Pick returns the SubConn to be used to send the RPC. | ||
207 | // The returned SubConn must be one returned by NewSubConn(). | ||
208 | // | ||
209 | // This functions is expected to return: | ||
210 | // - a SubConn that is known to be READY; | ||
211 | // - ErrNoSubConnAvailable if no SubConn is available, but progress is being | ||
212 | // made (for example, some SubConn is in CONNECTING mode); | ||
213 | // - other errors if no active connecting is happening (for example, all SubConn | ||
214 | // are in TRANSIENT_FAILURE mode). | ||
215 | // | ||
216 | // If a SubConn is returned: | ||
217 | // - If it is READY, gRPC will send the RPC on it; | ||
218 | // - If it is not ready, or becomes not ready after it's returned, gRPC will block | ||
219 | // until UpdateBalancerState() is called and will call pick on the new picker. | ||
220 | // | ||
221 | // If the returned error is not nil: | ||
222 | // - If the error is ErrNoSubConnAvailable, gRPC will block until UpdateBalancerState() | ||
223 | // - If the error is ErrTransientFailure: | ||
224 | // - If the RPC is wait-for-ready, gRPC will block until UpdateBalancerState() | ||
225 | // is called to pick again; | ||
226 | // - Otherwise, RPC will fail with unavailable error. | ||
227 | // - Else (error is other non-nil error): | ||
228 | // - The RPC will fail with unavailable error. | ||
229 | // | ||
230 | // The returned done() function will be called once the rpc has finished, with the | ||
231 | // final status of that RPC. | ||
232 | // done may be nil if balancer doesn't care about the RPC status. | ||
233 | Pick(ctx context.Context, opts PickOptions) (conn SubConn, done func(DoneInfo), err error) | ||
234 | } | ||
235 | |||
236 | // Balancer takes input from gRPC, manages SubConns, and collects and aggregates | ||
237 | // the connectivity states. | ||
238 | // | ||
239 | // It also generates and updates the Picker used by gRPC to pick SubConns for RPCs. | ||
240 | // | ||
241 | // HandleSubConnectionStateChange, HandleResolvedAddrs and Close are guaranteed | ||
242 | // to be called synchronously from the same goroutine. | ||
243 | // There's no guarantee on picker.Pick, it may be called anytime. | ||
244 | type Balancer interface { | ||
245 | // HandleSubConnStateChange is called by gRPC when the connectivity state | ||
246 | // of sc has changed. | ||
247 | // Balancer is expected to aggregate all the state of SubConn and report | ||
248 | // that back to gRPC. | ||
249 | // Balancer should also generate and update Pickers when its internal state has | ||
250 | // been changed by the new state. | ||
251 | HandleSubConnStateChange(sc SubConn, state connectivity.State) | ||
252 | // HandleResolvedAddrs is called by gRPC to send updated resolved addresses to | ||
253 | // balancers. | ||
254 | // Balancer can create new SubConn or remove SubConn with the addresses. | ||
255 | // An empty address slice and a non-nil error will be passed if the resolver returns | ||
256 | // non-nil error to gRPC. | ||
257 | HandleResolvedAddrs([]resolver.Address, error) | ||
258 | // Close closes the balancer. The balancer is not required to call | ||
259 | // ClientConn.RemoveSubConn for its existing SubConns. | ||
260 | Close() | ||
261 | } | ||
262 | |||
263 | // ConnectivityStateEvaluator takes the connectivity states of multiple SubConns | ||
264 | // and returns one aggregated connectivity state. | ||
265 | // | ||
266 | // It's not thread safe. | ||
267 | type ConnectivityStateEvaluator struct { | ||
268 | numReady uint64 // Number of addrConns in ready state. | ||
269 | numConnecting uint64 // Number of addrConns in connecting state. | ||
270 | numTransientFailure uint64 // Number of addrConns in transientFailure. | ||
271 | } | ||
272 | |||
273 | // RecordTransition records state change happening in subConn and based on that | ||
274 | // it evaluates what aggregated state should be. | ||
275 | // | ||
276 | // - If at least one SubConn in Ready, the aggregated state is Ready; | ||
277 | // - Else if at least one SubConn in Connecting, the aggregated state is Connecting; | ||
278 | // - Else the aggregated state is TransientFailure. | ||
279 | // | ||
280 | // Idle and Shutdown are not considered. | ||
281 | func (cse *ConnectivityStateEvaluator) RecordTransition(oldState, newState connectivity.State) connectivity.State { | ||
282 | // Update counters. | ||
283 | for idx, state := range []connectivity.State{oldState, newState} { | ||
284 | updateVal := 2*uint64(idx) - 1 // -1 for oldState and +1 for new. | ||
285 | switch state { | ||
286 | case connectivity.Ready: | ||
287 | cse.numReady += updateVal | ||
288 | case connectivity.Connecting: | ||
289 | cse.numConnecting += updateVal | ||
290 | case connectivity.TransientFailure: | ||
291 | cse.numTransientFailure += updateVal | ||
292 | } | ||
293 | } | ||
294 | |||
295 | // Evaluate. | ||
296 | if cse.numReady > 0 { | ||
297 | return connectivity.Ready | ||
298 | } | ||
299 | if cse.numConnecting > 0 { | ||
300 | return connectivity.Connecting | ||
301 | } | ||
302 | return connectivity.TransientFailure | ||
303 | } | ||
diff --git a/vendor/google.golang.org/grpc/balancer/base/balancer.go b/vendor/google.golang.org/grpc/balancer/base/balancer.go new file mode 100644 index 0000000..245785e --- /dev/null +++ b/vendor/google.golang.org/grpc/balancer/base/balancer.go | |||
@@ -0,0 +1,171 @@ | |||
1 | /* | ||
2 | * | ||
3 | * Copyright 2017 gRPC authors. | ||
4 | * | ||
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
6 | * you may not use this file except in compliance with the License. | ||
7 | * You may obtain a copy of the License at | ||
8 | * | ||
9 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
10 | * | ||
11 | * Unless required by applicable law or agreed to in writing, software | ||
12 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
14 | * See the License for the specific language governing permissions and | ||
15 | * limitations under the License. | ||
16 | * | ||
17 | */ | ||
18 | |||
19 | package base | ||
20 | |||
21 | import ( | ||
22 | "context" | ||
23 | |||
24 | "google.golang.org/grpc/balancer" | ||
25 | "google.golang.org/grpc/connectivity" | ||
26 | "google.golang.org/grpc/grpclog" | ||
27 | "google.golang.org/grpc/resolver" | ||
28 | ) | ||
29 | |||
30 | type baseBuilder struct { | ||
31 | name string | ||
32 | pickerBuilder PickerBuilder | ||
33 | config Config | ||
34 | } | ||
35 | |||
36 | func (bb *baseBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer { | ||
37 | return &baseBalancer{ | ||
38 | cc: cc, | ||
39 | pickerBuilder: bb.pickerBuilder, | ||
40 | |||
41 | subConns: make(map[resolver.Address]balancer.SubConn), | ||
42 | scStates: make(map[balancer.SubConn]connectivity.State), | ||
43 | csEvltr: &balancer.ConnectivityStateEvaluator{}, | ||
44 | // Initialize picker to a picker that always return | ||
45 | // ErrNoSubConnAvailable, because when state of a SubConn changes, we | ||
46 | // may call UpdateBalancerState with this picker. | ||
47 | picker: NewErrPicker(balancer.ErrNoSubConnAvailable), | ||
48 | config: bb.config, | ||
49 | } | ||
50 | } | ||
51 | |||
52 | func (bb *baseBuilder) Name() string { | ||
53 | return bb.name | ||
54 | } | ||
55 | |||
56 | type baseBalancer struct { | ||
57 | cc balancer.ClientConn | ||
58 | pickerBuilder PickerBuilder | ||
59 | |||
60 | csEvltr *balancer.ConnectivityStateEvaluator | ||
61 | state connectivity.State | ||
62 | |||
63 | subConns map[resolver.Address]balancer.SubConn | ||
64 | scStates map[balancer.SubConn]connectivity.State | ||
65 | picker balancer.Picker | ||
66 | config Config | ||
67 | } | ||
68 | |||
69 | func (b *baseBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) { | ||
70 | if err != nil { | ||
71 | grpclog.Infof("base.baseBalancer: HandleResolvedAddrs called with error %v", err) | ||
72 | return | ||
73 | } | ||
74 | grpclog.Infoln("base.baseBalancer: got new resolved addresses: ", addrs) | ||
75 | // addrsSet is the set converted from addrs, it's used for quick lookup of an address. | ||
76 | addrsSet := make(map[resolver.Address]struct{}) | ||
77 | for _, a := range addrs { | ||
78 | addrsSet[a] = struct{}{} | ||
79 | if _, ok := b.subConns[a]; !ok { | ||
80 | // a is a new address (not existing in b.subConns). | ||
81 | sc, err := b.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{HealthCheckEnabled: b.config.HealthCheck}) | ||
82 | if err != nil { | ||
83 | grpclog.Warningf("base.baseBalancer: failed to create new SubConn: %v", err) | ||
84 | continue | ||
85 | } | ||
86 | b.subConns[a] = sc | ||
87 | b.scStates[sc] = connectivity.Idle | ||
88 | sc.Connect() | ||
89 | } | ||
90 | } | ||
91 | for a, sc := range b.subConns { | ||
92 | // a was removed by resolver. | ||
93 | if _, ok := addrsSet[a]; !ok { | ||
94 | b.cc.RemoveSubConn(sc) | ||
95 | delete(b.subConns, a) | ||
96 | // Keep the state of this sc in b.scStates until sc's state becomes Shutdown. | ||
97 | // The entry will be deleted in HandleSubConnStateChange. | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | // regeneratePicker takes a snapshot of the balancer, and generates a picker | ||
103 | // from it. The picker is | ||
104 | // - errPicker with ErrTransientFailure if the balancer is in TransientFailure, | ||
105 | // - built by the pickerBuilder with all READY SubConns otherwise. | ||
106 | func (b *baseBalancer) regeneratePicker() { | ||
107 | if b.state == connectivity.TransientFailure { | ||
108 | b.picker = NewErrPicker(balancer.ErrTransientFailure) | ||
109 | return | ||
110 | } | ||
111 | readySCs := make(map[resolver.Address]balancer.SubConn) | ||
112 | |||
113 | // Filter out all ready SCs from full subConn map. | ||
114 | for addr, sc := range b.subConns { | ||
115 | if st, ok := b.scStates[sc]; ok && st == connectivity.Ready { | ||
116 | readySCs[addr] = sc | ||
117 | } | ||
118 | } | ||
119 | b.picker = b.pickerBuilder.Build(readySCs) | ||
120 | } | ||
121 | |||
122 | func (b *baseBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) { | ||
123 | grpclog.Infof("base.baseBalancer: handle SubConn state change: %p, %v", sc, s) | ||
124 | oldS, ok := b.scStates[sc] | ||
125 | if !ok { | ||
126 | grpclog.Infof("base.baseBalancer: got state changes for an unknown SubConn: %p, %v", sc, s) | ||
127 | return | ||
128 | } | ||
129 | b.scStates[sc] = s | ||
130 | switch s { | ||
131 | case connectivity.Idle: | ||
132 | sc.Connect() | ||
133 | case connectivity.Shutdown: | ||
134 | // When an address was removed by resolver, b called RemoveSubConn but | ||
135 | // kept the sc's state in scStates. Remove state for this sc here. | ||
136 | delete(b.scStates, sc) | ||
137 | } | ||
138 | |||
139 | oldAggrState := b.state | ||
140 | b.state = b.csEvltr.RecordTransition(oldS, s) | ||
141 | |||
142 | // Regenerate picker when one of the following happens: | ||
143 | // - this sc became ready from not-ready | ||
144 | // - this sc became not-ready from ready | ||
145 | // - the aggregated state of balancer became TransientFailure from non-TransientFailure | ||
146 | // - the aggregated state of balancer became non-TransientFailure from TransientFailure | ||
147 | if (s == connectivity.Ready) != (oldS == connectivity.Ready) || | ||
148 | (b.state == connectivity.TransientFailure) != (oldAggrState == connectivity.TransientFailure) { | ||
149 | b.regeneratePicker() | ||
150 | } | ||
151 | |||
152 | b.cc.UpdateBalancerState(b.state, b.picker) | ||
153 | } | ||
154 | |||
155 | // Close is a nop because base balancer doesn't have internal state to clean up, | ||
156 | // and it doesn't need to call RemoveSubConn for the SubConns. | ||
157 | func (b *baseBalancer) Close() { | ||
158 | } | ||
159 | |||
160 | // NewErrPicker returns a picker that always returns err on Pick(). | ||
161 | func NewErrPicker(err error) balancer.Picker { | ||
162 | return &errPicker{err: err} | ||
163 | } | ||
164 | |||
165 | type errPicker struct { | ||
166 | err error // Pick() always returns this err. | ||
167 | } | ||
168 | |||
169 | func (p *errPicker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) { | ||
170 | return nil, nil, p.err | ||
171 | } | ||
diff --git a/vendor/google.golang.org/grpc/balancer/base/base.go b/vendor/google.golang.org/grpc/balancer/base/base.go new file mode 100644 index 0000000..34b1f29 --- /dev/null +++ b/vendor/google.golang.org/grpc/balancer/base/base.go | |||
@@ -0,0 +1,64 @@ | |||
1 | /* | ||
2 | * | ||
3 | * Copyright 2017 gRPC authors. | ||
4 | * | ||
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
6 | * you may not use this file except in compliance with the License. | ||
7 | * You may obtain a copy of the License at | ||
8 | * | ||
9 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
10 | * | ||
11 | * Unless required by applicable law or agreed to in writing, software | ||
12 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
14 | * See the License for the specific language governing permissions and | ||
15 | * limitations under the License. | ||
16 | * | ||
17 | */ | ||
18 | |||
19 | // Package base defines a balancer base that can be used to build balancers with | ||
20 | // different picking algorithms. | ||
21 | // | ||
22 | // The base balancer creates a new SubConn for each resolved address. The | ||
23 | // provided picker will only be notified about READY SubConns. | ||
24 | // | ||
25 | // This package is the base of round_robin balancer, its purpose is to be used | ||
26 | // to build round_robin like balancers with complex picking algorithms. | ||
27 | // Balancers with more complicated logic should try to implement a balancer | ||
28 | // builder from scratch. | ||
29 | // | ||
30 | // All APIs in this package are experimental. | ||
31 | package base | ||
32 | |||
33 | import ( | ||
34 | "google.golang.org/grpc/balancer" | ||
35 | "google.golang.org/grpc/resolver" | ||
36 | ) | ||
37 | |||
38 | // PickerBuilder creates balancer.Picker. | ||
39 | type PickerBuilder interface { | ||
40 | // Build takes a slice of ready SubConns, and returns a picker that will be | ||
41 | // used by gRPC to pick a SubConn. | ||
42 | Build(readySCs map[resolver.Address]balancer.SubConn) balancer.Picker | ||
43 | } | ||
44 | |||
45 | // NewBalancerBuilder returns a balancer builder. The balancers | ||
46 | // built by this builder will use the picker builder to build pickers. | ||
47 | func NewBalancerBuilder(name string, pb PickerBuilder) balancer.Builder { | ||
48 | return NewBalancerBuilderWithConfig(name, pb, Config{}) | ||
49 | } | ||
50 | |||
51 | // Config contains the config info about the base balancer builder. | ||
52 | type Config struct { | ||
53 | // HealthCheck indicates whether health checking should be enabled for this specific balancer. | ||
54 | HealthCheck bool | ||
55 | } | ||
56 | |||
57 | // NewBalancerBuilderWithConfig returns a base balancer builder configured by the provided config. | ||
58 | func NewBalancerBuilderWithConfig(name string, pb PickerBuilder, config Config) balancer.Builder { | ||
59 | return &baseBuilder{ | ||
60 | name: name, | ||
61 | pickerBuilder: pb, | ||
62 | config: config, | ||
63 | } | ||
64 | } | ||
diff --git a/vendor/google.golang.org/grpc/balancer/roundrobin/roundrobin.go b/vendor/google.golang.org/grpc/balancer/roundrobin/roundrobin.go new file mode 100644 index 0000000..57aea9f --- /dev/null +++ b/vendor/google.golang.org/grpc/balancer/roundrobin/roundrobin.go | |||
@@ -0,0 +1,79 @@ | |||
1 | /* | ||
2 | * | ||
3 | * Copyright 2017 gRPC authors. | ||
4 | * | ||
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
6 | * you may not use this file except in compliance with the License. | ||
7 | * You may obtain a copy of the License at | ||
8 | * | ||
9 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
10 | * | ||
11 | * Unless required by applicable law or agreed to in writing, software | ||
12 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
14 | * See the License for the specific language governing permissions and | ||
15 | * limitations under the License. | ||
16 | * | ||
17 | */ | ||
18 | |||
19 | // Package roundrobin defines a roundrobin balancer. Roundrobin balancer is | ||
20 | // installed as one of the default balancers in gRPC, users don't need to | ||
21 | // explicitly install this balancer. | ||
22 | package roundrobin | ||
23 | |||
24 | import ( | ||
25 | "context" | ||
26 | "sync" | ||
27 | |||
28 | "google.golang.org/grpc/balancer" | ||
29 | "google.golang.org/grpc/balancer/base" | ||
30 | "google.golang.org/grpc/grpclog" | ||
31 | "google.golang.org/grpc/resolver" | ||
32 | ) | ||
33 | |||
34 | // Name is the name of round_robin balancer. | ||
35 | const Name = "round_robin" | ||
36 | |||
37 | // newBuilder creates a new roundrobin balancer builder. | ||
38 | func newBuilder() balancer.Builder { | ||
39 | return base.NewBalancerBuilderWithConfig(Name, &rrPickerBuilder{}, base.Config{HealthCheck: true}) | ||
40 | } | ||
41 | |||
42 | func init() { | ||
43 | balancer.Register(newBuilder()) | ||
44 | } | ||
45 | |||
46 | type rrPickerBuilder struct{} | ||
47 | |||
48 | func (*rrPickerBuilder) Build(readySCs map[resolver.Address]balancer.SubConn) balancer.Picker { | ||
49 | grpclog.Infof("roundrobinPicker: newPicker called with readySCs: %v", readySCs) | ||
50 | var scs []balancer.SubConn | ||
51 | for _, sc := range readySCs { | ||
52 | scs = append(scs, sc) | ||
53 | } | ||
54 | return &rrPicker{ | ||
55 | subConns: scs, | ||
56 | } | ||
57 | } | ||
58 | |||
59 | type rrPicker struct { | ||
60 | // subConns is the snapshot of the roundrobin balancer when this picker was | ||
61 | // created. The slice is immutable. Each Get() will do a round robin | ||
62 | // selection from it and return the selected SubConn. | ||
63 | subConns []balancer.SubConn | ||
64 | |||
65 | mu sync.Mutex | ||
66 | next int | ||
67 | } | ||
68 | |||
69 | func (p *rrPicker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) { | ||
70 | if len(p.subConns) <= 0 { | ||
71 | return nil, nil, balancer.ErrNoSubConnAvailable | ||
72 | } | ||
73 | |||
74 | p.mu.Lock() | ||
75 | sc := p.subConns[p.next] | ||
76 | p.next = (p.next + 1) % len(p.subConns) | ||
77 | p.mu.Unlock() | ||
78 | return sc, nil, nil | ||
79 | } | ||