Debian Codegraph @

vendor/google.golang.org/grpc/balancer/balancer.go

raw
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 "errors"
25 "net"
26
27 "golang.org/x/net/context"
28 "google.golang.org/grpc/connectivity"
29 "google.golang.org/grpc/credentials"
30 "google.golang.org/grpc/resolver"
31 )
32
33 var (
34 // m is a map from name to balancer builder.
35 m = make(map[string]Builder)
36 // defaultBuilder is the default balancer to use.
37 defaultBuilder Builder // TODO(bar) install pickfirst as default.
38 )
39
40 // Register registers the balancer builder to the balancer map.
41 // b.Name will be used as the name registered with this builder.
42 func Register(b Builder) {
43 m[b.Name()] = b
44 }
45
46 // Get returns the resolver builder registered with the given name.
47 // If no builder is register with the name, the default pickfirst will
48 // be used.
49 func Get(name string) Builder {
50 if b, ok := m[name]; ok {
51 return b
52 }
53 return defaultBuilder
54 }
55
56 // SubConn represents a gRPC sub connection.
57 // Each sub connection contains a list of addresses. gRPC will
58 // try to connect to them (in sequence), and stop trying the
59 // remainder once one connection is successful.
60 //
61 // The reconnect backoff will be applied on the list, not a single address.
62 // For example, try_on_all_addresses -> backoff -> try_on_all_addresses.
63 //
64 // All SubConns start in IDLE, and will not try to connect. To trigger
65 // the connecting, Balancers must call Connect.
66 // When the connection encounters an error, it will reconnect immediately.
67 // When the connection becomes IDLE, it will not reconnect unless Connect is
68 // called.
69 type SubConn interface {
70 // UpdateAddresses updates the addresses used in this SubConn.
71 // gRPC checks if currently-connected address is still in the new list.
72 // If it's in the list, the connection will be kept.
73 // If it's not in the list, the connection will gracefully closed, and
74 // a new connection will be created.
75 //
76 // This will trigger a state transition for the SubConn.
77 UpdateAddresses([]resolver.Address)
78 // Connect starts the connecting for this SubConn.
79 Connect()
80 }
81
82 // NewSubConnOptions contains options to create new SubConn.
83 type NewSubConnOptions struct{}
84
85 // ClientConn represents a gRPC ClientConn.
86 type ClientConn interface {
87 // NewSubConn is called by balancer to create a new SubConn.
88 // It doesn't block and wait for the connections to be established.
89 // Behaviors of the SubConn can be controlled by options.
90 NewSubConn([]resolver.Address, NewSubConnOptions) (SubConn, error)
91 // RemoveSubConn removes the SubConn from ClientConn.
92 // The SubConn will be shutdown.
93 RemoveSubConn(SubConn)
94
95 // UpdateBalancerState is called by balancer to nofity gRPC that some internal
96 // state in balancer has changed.
97 //
98 // gRPC will update the connectivity state of the ClientConn, and will call pick
99 // on the new picker to pick new SubConn.
100 UpdateBalancerState(s connectivity.State, p Picker)
101
102 // Target returns the dial target for this ClientConn.
103 Target() string
104 }
105
106 // BuildOptions contains additional information for Build.
107 type BuildOptions struct {
108 // DialCreds is the transport credential the Balancer implementation can
109 // use to dial to a remote load balancer server. The Balancer implementations
110 // can ignore this if it does not need to talk to another party securely.
111 DialCreds credentials.TransportCredentials
112 // Dialer is the custom dialer the Balancer implementation can use to dial
113 // to a remote load balancer server. The Balancer implementations
114 // can ignore this if it doesn't need to talk to remote balancer.
115 Dialer func(context.Context, string) (net.Conn, error)
116 }
117
118 // Builder creates a balancer.
119 type Builder interface {
120 // Build creates a new balancer with the ClientConn.
121 Build(cc ClientConn, opts BuildOptions) Balancer
122 // Name returns the name of balancers built by this builder.
123 // It will be used to pick balancers (for example in service config).
124 Name() string
125 }
126
127 // PickOptions contains addition information for the Pick operation.
128 type PickOptions struct{}
129
130 // DoneInfo contains additional information for done.
131 type DoneInfo struct {
132 // Err is the rpc error the RPC finished with. It could be nil.
133 Err error
134 }
135
136 var (
137 // ErrNoSubConnAvailable indicates no SubConn is available for pick().
138 // gRPC will block the RPC until a new picker is available via UpdateBalancerState().
139 ErrNoSubConnAvailable = errors.New("no SubConn is available")
140 // ErrTransientFailure indicates all SubConns are in TransientFailure.
141 // WaitForReady RPCs will block, non-WaitForReady RPCs will fail.
142 ErrTransientFailure = errors.New("all SubConns are in TransientFailure")
143 )
144
145 // Picker is used by gRPC to pick a SubConn to send an RPC.
146 // Balancer is expected to generate a new picker from its snapshot everytime its
147 // internal state has changed.
148 //
149 // The pickers used by gRPC can be updated by ClientConn.UpdateBalancerState().
150 type Picker interface {
151 // Pick returns the SubConn to be used to send the RPC.
152 // The returned SubConn must be one returned by NewSubConn().
153 //
154 // This functions is expected to return:
155 // - a SubConn that is known to be READY;
156 // - ErrNoSubConnAvailable if no SubConn is available, but progress is being
157 // made (for example, some SubConn is in CONNECTING mode);
158 // - other errors if no active connecting is happening (for example, all SubConn
159 // are in TRANSIENT_FAILURE mode).
160 //
161 // If a SubConn is returned:
162 // - If it is READY, gRPC will send the RPC on it;
163 // - If it is not ready, or becomes not ready after it's returned, gRPC will block
164 // this call until a new picker is updated and will call pick on the new picker.
165 //
166 // If the returned error is not nil:
167 // - If the error is ErrNoSubConnAvailable, gRPC will block until UpdateBalancerState()
168 // - If the error is ErrTransientFailure:
169 // - If the RPC is wait-for-ready, gRPC will block until UpdateBalancerState()
170 // is called to pick again;
171 // - Otherwise, RPC will fail with unavailable error.
172 // - Else (error is other non-nil error):
173 // - The RPC will fail with unavailable error.
174 //
175 // The returned done() function will be called once the rpc has finished, with the
176 // final status of that RPC.
177 // done may be nil if balancer doesn't care about the RPC status.
178 Pick(ctx context.Context, opts PickOptions) (conn SubConn, done func(DoneInfo), err error)
179 }
180
181 // Balancer takes input from gRPC, manages SubConns, and collects and aggregates
182 // the connectivity states.
183 //
184 // It also generates and updates the Picker used by gRPC to pick SubConns for RPCs.
185 //
186 // HandleSubConnectionStateChange, HandleResolvedAddrs and Close are guaranteed
187 // to be called synchronously from the same goroutine.
188 // There's no guarantee on picker.Pick, it may be called anytime.
189 type Balancer interface {
190 // HandleSubConnStateChange is called by gRPC when the connectivity state
191 // of sc has changed.
192 // Balancer is expected to aggregate all the state of SubConn and report
193 // that back to gRPC.
194 // Balancer should also generate and update Pickers when its internal state has
195 // been changed by the new state.
196 HandleSubConnStateChange(sc SubConn, state connectivity.State)
197 // HandleResolvedAddrs is called by gRPC to send updated resolved addresses to
198 // balancers.
199 // Balancer can create new SubConn or remove SubConn with the addresses.
200 // An empty address slice and a non-nil error will be passed if the resolver returns
201 // non-nil error to gRPC.
202 HandleResolvedAddrs([]resolver.Address, error)
203 // Close closes the balancer. The balancer is not required to call
204 // ClientConn.RemoveSubConn for its existing SubConns.
205 Close()
206 }