blob: 03567d7be74bf50cc3c19de92f79a9be75cb6f50 [file] [log] [blame]
Prince Pereirac1c21d62021-04-22 08:38:15 +00001/*
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 resolver defines APIs for name resolution in gRPC.
20// All APIs in this package are experimental.
21package resolver
22
23import (
24 "context"
25 "net"
26
27 "google.golang.org/grpc/attributes"
28 "google.golang.org/grpc/credentials"
29 "google.golang.org/grpc/serviceconfig"
30)
31
32var (
33 // m is a map from scheme to resolver builder.
34 m = make(map[string]Builder)
35 // defaultScheme is the default scheme to use.
36 defaultScheme = "passthrough"
37)
38
39// TODO(bar) install dns resolver in init(){}.
40
41// Register registers the resolver builder to the resolver map. b.Scheme will be
42// used as the scheme 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 Resolvers are
46// registered with the same name, the one registered last will take effect.
47func Register(b Builder) {
48 m[b.Scheme()] = b
49}
50
51// Get returns the resolver builder registered with the given scheme.
52//
53// If no builder is register with the scheme, nil will be returned.
54func Get(scheme string) Builder {
55 if b, ok := m[scheme]; ok {
56 return b
57 }
58 return nil
59}
60
61// SetDefaultScheme sets the default scheme that will be used. The default
62// default scheme is "passthrough".
63//
64// NOTE: this function must only be called during initialization time (i.e. in
65// an init() function), and is not thread-safe. The scheme set last overrides
66// previously set values.
67func SetDefaultScheme(scheme string) {
68 defaultScheme = scheme
69}
70
71// GetDefaultScheme gets the default scheme that will be used.
72func GetDefaultScheme() string {
73 return defaultScheme
74}
75
76// AddressType indicates the address type returned by name resolution.
77//
78// Deprecated: use Attributes in Address instead.
79type AddressType uint8
80
81const (
82 // Backend indicates the address is for a backend server.
83 //
84 // Deprecated: use Attributes in Address instead.
85 Backend AddressType = iota
86 // GRPCLB indicates the address is for a grpclb load balancer.
87 //
88 // Deprecated: use Attributes in Address instead.
89 GRPCLB
90)
91
92// Address represents a server the client connects to.
93// This is the EXPERIMENTAL API and may be changed or extended in the future.
94type Address struct {
95 // Addr is the server address on which a connection will be established.
96 Addr string
97
98 // ServerName is the name of this address.
99 // If non-empty, the ServerName is used as the transport certification authority for
100 // the address, instead of the hostname from the Dial target string. In most cases,
101 // this should not be set.
102 //
103 // If Type is GRPCLB, ServerName should be the name of the remote load
104 // balancer, not the name of the backend.
105 //
106 // WARNING: ServerName must only be populated with trusted values. It
107 // is insecure to populate it with data from untrusted inputs since untrusted
108 // values could be used to bypass the authority checks performed by TLS.
109 ServerName string
110
111 // Attributes contains arbitrary data about this address intended for
112 // consumption by the load balancing policy.
113 Attributes *attributes.Attributes
114
115 // Type is the type of this address.
116 //
117 // Deprecated: use Attributes instead.
118 Type AddressType
119
120 // Metadata is the information associated with Addr, which may be used
121 // to make load balancing decision.
122 //
123 // Deprecated: use Attributes instead.
124 Metadata interface{}
125}
126
127// BuildOption is a type alias of BuildOptions for legacy reasons.
128//
129// Deprecated: use BuildOptions instead.
130type BuildOption = BuildOptions
131
132// BuildOptions includes additional information for the builder to create
133// the resolver.
134type BuildOptions struct {
135 // DisableServiceConfig indicates whether a resolver implementation should
136 // fetch service config data.
137 DisableServiceConfig bool
138 // DialCreds is the transport credentials used by the ClientConn for
139 // communicating with the target gRPC service (set via
140 // WithTransportCredentials). In cases where a name resolution service
141 // requires the same credentials, the resolver may use this field. In most
142 // cases though, it is not appropriate, and this field may be ignored.
143 DialCreds credentials.TransportCredentials
144 // CredsBundle is the credentials bundle used by the ClientConn for
145 // communicating with the target gRPC service (set via
146 // WithCredentialsBundle). In cases where a name resolution service
147 // requires the same credentials, the resolver may use this field. In most
148 // cases though, it is not appropriate, and this field may be ignored.
149 CredsBundle credentials.Bundle
150 // Dialer is the custom dialer used by the ClientConn for dialling the
151 // target gRPC service (set via WithDialer). In cases where a name
152 // resolution service requires the same dialer, the resolver may use this
153 // field. In most cases though, it is not appropriate, and this field may
154 // be ignored.
155 Dialer func(context.Context, string) (net.Conn, error)
156}
157
158// State contains the current Resolver state relevant to the ClientConn.
159type State struct {
160 // Addresses is the latest set of resolved addresses for the target.
161 Addresses []Address
162
163 // ServiceConfig contains the result from parsing the latest service
164 // config. If it is nil, it indicates no service config is present or the
165 // resolver does not provide service configs.
166 ServiceConfig *serviceconfig.ParseResult
167
168 // Attributes contains arbitrary data about the resolver intended for
169 // consumption by the load balancing policy.
170 Attributes *attributes.Attributes
171}
172
173// ClientConn contains the callbacks for resolver to notify any updates
174// to the gRPC ClientConn.
175//
176// This interface is to be implemented by gRPC. Users should not need a
177// brand new implementation of this interface. For the situations like
178// testing, the new implementation should embed this interface. This allows
179// gRPC to add new methods to this interface.
180type ClientConn interface {
181 // UpdateState updates the state of the ClientConn appropriately.
182 UpdateState(State)
183 // ReportError notifies the ClientConn that the Resolver encountered an
184 // error. The ClientConn will notify the load balancer and begin calling
185 // ResolveNow on the Resolver with exponential backoff.
186 ReportError(error)
187 // NewAddress is called by resolver to notify ClientConn a new list
188 // of resolved addresses.
189 // The address list should be the complete list of resolved addresses.
190 //
191 // Deprecated: Use UpdateState instead.
192 NewAddress(addresses []Address)
193 // NewServiceConfig is called by resolver to notify ClientConn a new
194 // service config. The service config should be provided as a json string.
195 //
196 // Deprecated: Use UpdateState instead.
197 NewServiceConfig(serviceConfig string)
198 // ParseServiceConfig parses the provided service config and returns an
199 // object that provides the parsed config.
200 ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult
201}
202
203// Target represents a target for gRPC, as specified in:
204// https://github.com/grpc/grpc/blob/master/doc/naming.md.
205// It is parsed from the target string that gets passed into Dial or DialContext by the user. And
206// grpc passes it to the resolver and the balancer.
207//
208// If the target follows the naming spec, and the parsed scheme is registered with grpc, we will
209// parse the target string according to the spec. e.g. "dns://some_authority/foo.bar" will be parsed
210// into &Target{Scheme: "dns", Authority: "some_authority", Endpoint: "foo.bar"}
211//
212// If the target does not contain a scheme, we will apply the default scheme, and set the Target to
213// be the full target string. e.g. "foo.bar" will be parsed into
214// &Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "foo.bar"}.
215//
216// If the parsed scheme is not registered (i.e. no corresponding resolver available to resolve the
217// endpoint), we set the Scheme to be the default scheme, and set the Endpoint to be the full target
218// string. e.g. target string "unknown_scheme://authority/endpoint" will be parsed into
219// &Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "unknown_scheme://authority/endpoint"}.
220type Target struct {
221 Scheme string
222 Authority string
223 Endpoint string
224}
225
226// Builder creates a resolver that will be used to watch name resolution updates.
227type Builder interface {
228 // Build creates a new resolver for the given target.
229 //
230 // gRPC dial calls Build synchronously, and fails if the returned error is
231 // not nil.
232 Build(target Target, cc ClientConn, opts BuildOptions) (Resolver, error)
233 // Scheme returns the scheme supported by this resolver.
234 // Scheme is defined at https://github.com/grpc/grpc/blob/master/doc/naming.md.
235 Scheme() string
236}
237
238// ResolveNowOption is a type alias of ResolveNowOptions for legacy reasons.
239//
240// Deprecated: use ResolveNowOptions instead.
241type ResolveNowOption = ResolveNowOptions
242
243// ResolveNowOptions includes additional information for ResolveNow.
244type ResolveNowOptions struct{}
245
246// Resolver watches for the updates on the specified target.
247// Updates include address updates and service config updates.
248type Resolver interface {
249 // ResolveNow will be called by gRPC to try to resolve the target name
250 // again. It's just a hint, resolver can ignore this if it's not necessary.
251 //
252 // It could be called multiple times concurrently.
253 ResolveNow(ResolveNowOptions)
254 // Close closes the resolver.
255 Close()
256}
257
258// UnregisterForTesting removes the resolver builder with the given scheme from the
259// resolver map.
260// This function is for testing only.
261func UnregisterForTesting(scheme string) {
262 delete(m, scheme)
263}