blob: f6f6934d9bc3800200718722edd7aaa43dc9ed5c [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
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
23var (
24 // m is a map from scheme to resolver builder.
25 m = make(map[string]Builder)
26 // defaultScheme is the default scheme to use.
27 defaultScheme = "passthrough"
28)
29
30// TODO(bar) install dns resolver in init(){}.
31
32// Register registers the resolver builder to the resolver map. b.Scheme will be
33// used as the scheme registered with this builder.
34//
35// NOTE: this function must only be called during initialization time (i.e. in
36// an init() function), and is not thread-safe. If multiple Resolvers are
37// registered with the same name, the one registered last will take effect.
38func Register(b Builder) {
39 m[b.Scheme()] = b
40}
41
42// Get returns the resolver builder registered with the given scheme.
43//
44// If no builder is register with the scheme, nil will be returned.
45func Get(scheme string) Builder {
46 if b, ok := m[scheme]; ok {
47 return b
48 }
49 return nil
50}
51
52// SetDefaultScheme sets the default scheme that will be used. The default
53// default scheme is "passthrough".
54//
55// NOTE: this function must only be called during initialization time (i.e. in
56// an init() function), and is not thread-safe. The scheme set last overrides
57// previously set values.
58func SetDefaultScheme(scheme string) {
59 defaultScheme = scheme
60}
61
62// GetDefaultScheme gets the default scheme that will be used.
63func GetDefaultScheme() string {
64 return defaultScheme
65}
66
67// AddressType indicates the address type returned by name resolution.
68type AddressType uint8
69
70const (
71 // Backend indicates the address is for a backend server.
72 Backend AddressType = iota
73 // GRPCLB indicates the address is for a grpclb load balancer.
74 GRPCLB
75)
76
77// Address represents a server the client connects to.
78// This is the EXPERIMENTAL API and may be changed or extended in the future.
79type Address struct {
80 // Addr is the server address on which a connection will be established.
81 Addr string
82 // Type is the type of this address.
83 Type AddressType
84 // ServerName is the name of this address.
85 //
86 // e.g. if Type is GRPCLB, ServerName should be the name of the remote load
87 // balancer, not the name of the backend.
88 ServerName string
89 // Metadata is the information associated with Addr, which may be used
90 // to make load balancing decision.
91 Metadata interface{}
92}
93
94// BuildOption includes additional information for the builder to create
95// the resolver.
96type BuildOption struct {
97 // DisableServiceConfig indicates whether resolver should fetch service config data.
98 DisableServiceConfig bool
99}
100
101// State contains the current Resolver state relevant to the ClientConn.
102type State struct {
103 Addresses []Address // Resolved addresses for the target
104 ServiceConfig string // JSON representation of the service config
105
106 // TODO: add Err error
107 // TODO: add ParsedServiceConfig interface{}
108}
109
110// ClientConn contains the callbacks for resolver to notify any updates
111// to the gRPC ClientConn.
112//
113// This interface is to be implemented by gRPC. Users should not need a
114// brand new implementation of this interface. For the situations like
115// testing, the new implementation should embed this interface. This allows
116// gRPC to add new methods to this interface.
117type ClientConn interface {
118 // UpdateState updates the state of the ClientConn appropriately.
119 UpdateState(State)
120 // NewAddress is called by resolver to notify ClientConn a new list
121 // of resolved addresses.
122 // The address list should be the complete list of resolved addresses.
123 //
124 // Deprecated: Use UpdateState instead.
125 NewAddress(addresses []Address)
126 // NewServiceConfig is called by resolver to notify ClientConn a new
127 // service config. The service config should be provided as a json string.
128 //
129 // Deprecated: Use UpdateState instead.
130 NewServiceConfig(serviceConfig string)
131}
132
133// Target represents a target for gRPC, as specified in:
134// https://github.com/grpc/grpc/blob/master/doc/naming.md.
135// It is parsed from the target string that gets passed into Dial or DialContext by the user. And
136// grpc passes it to the resolver and the balancer.
137//
138// If the target follows the naming spec, and the parsed scheme is registered with grpc, we will
139// parse the target string according to the spec. e.g. "dns://some_authority/foo.bar" will be parsed
140// into &Target{Scheme: "dns", Authority: "some_authority", Endpoint: "foo.bar"}
141//
142// If the target does not contain a scheme, we will apply the default scheme, and set the Target to
143// be the full target string. e.g. "foo.bar" will be parsed into
144// &Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "foo.bar"}.
145//
146// If the parsed scheme is not registered (i.e. no corresponding resolver available to resolve the
147// endpoint), we set the Scheme to be the default scheme, and set the Endpoint to be the full target
148// string. e.g. target string "unknown_scheme://authority/endpoint" will be parsed into
149// &Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "unknown_scheme://authority/endpoint"}.
150type Target struct {
151 Scheme string
152 Authority string
153 Endpoint string
154}
155
156// Builder creates a resolver that will be used to watch name resolution updates.
157type Builder interface {
158 // Build creates a new resolver for the given target.
159 //
160 // gRPC dial calls Build synchronously, and fails if the returned error is
161 // not nil.
162 Build(target Target, cc ClientConn, opts BuildOption) (Resolver, error)
163 // Scheme returns the scheme supported by this resolver.
164 // Scheme is defined at https://github.com/grpc/grpc/blob/master/doc/naming.md.
165 Scheme() string
166}
167
168// ResolveNowOption includes additional information for ResolveNow.
169type ResolveNowOption struct{}
170
171// Resolver watches for the updates on the specified target.
172// Updates include address updates and service config updates.
173type Resolver interface {
174 // ResolveNow will be called by gRPC to try to resolve the target name
175 // again. It's just a hint, resolver can ignore this if it's not necessary.
176 //
177 // It could be called multiple times concurrently.
178 ResolveNow(ResolveNowOption)
179 // Close closes the resolver.
180 Close()
181}
182
183// UnregisterForTesting removes the resolver builder with the given scheme from the
184// resolver map.
185// This function is for testing only.
186func UnregisterForTesting(scheme string) {
187 delete(m, scheme)
188}