diff options
Diffstat (limited to 'vendor/google.golang.org/grpc/naming/dns_resolver.go')
-rw-r--r-- | vendor/google.golang.org/grpc/naming/dns_resolver.go | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/vendor/google.golang.org/grpc/naming/dns_resolver.go b/vendor/google.golang.org/grpc/naming/dns_resolver.go index efd37e3..fd8cd3b 100644 --- a/vendor/google.golang.org/grpc/naming/dns_resolver.go +++ b/vendor/google.golang.org/grpc/naming/dns_resolver.go | |||
@@ -19,13 +19,13 @@ | |||
19 | package naming | 19 | package naming |
20 | 20 | ||
21 | import ( | 21 | import ( |
22 | "context" | ||
22 | "errors" | 23 | "errors" |
23 | "fmt" | 24 | "fmt" |
24 | "net" | 25 | "net" |
25 | "strconv" | 26 | "strconv" |
26 | "time" | 27 | "time" |
27 | 28 | ||
28 | "golang.org/x/net/context" | ||
29 | "google.golang.org/grpc/grpclog" | 29 | "google.golang.org/grpc/grpclog" |
30 | ) | 30 | ) |
31 | 31 | ||
@@ -37,6 +37,9 @@ const ( | |||
37 | var ( | 37 | var ( |
38 | errMissingAddr = errors.New("missing address") | 38 | errMissingAddr = errors.New("missing address") |
39 | errWatcherClose = errors.New("watcher has been closed") | 39 | errWatcherClose = errors.New("watcher has been closed") |
40 | |||
41 | lookupHost = net.DefaultResolver.LookupHost | ||
42 | lookupSRV = net.DefaultResolver.LookupSRV | ||
40 | ) | 43 | ) |
41 | 44 | ||
42 | // NewDNSResolverWithFreq creates a DNS Resolver that can resolve DNS names, and | 45 | // NewDNSResolverWithFreq creates a DNS Resolver that can resolve DNS names, and |
@@ -141,8 +144,8 @@ type dnsWatcher struct { | |||
141 | r *dnsResolver | 144 | r *dnsResolver |
142 | host string | 145 | host string |
143 | port string | 146 | port string |
144 | // The latest resolved address list | 147 | // The latest resolved address set |
145 | curAddrs []*Update | 148 | curAddrs map[string]*Update |
146 | ctx context.Context | 149 | ctx context.Context |
147 | cancel context.CancelFunc | 150 | cancel context.CancelFunc |
148 | t *time.Timer | 151 | t *time.Timer |
@@ -153,10 +156,10 @@ type ipWatcher struct { | |||
153 | updateChan chan *Update | 156 | updateChan chan *Update |
154 | } | 157 | } |
155 | 158 | ||
156 | // Next returns the adrress resolution Update for the target. For IP address, | 159 | // Next returns the address resolution Update for the target. For IP address, |
157 | // the resolution is itself, thus polling name server is unncessary. Therefore, | 160 | // the resolution is itself, thus polling name server is unnecessary. Therefore, |
158 | // Next() will return an Update the first time it is called, and will be blocked | 161 | // Next() will return an Update the first time it is called, and will be blocked |
159 | // for all following calls as no Update exisits until watcher is closed. | 162 | // for all following calls as no Update exists until watcher is closed. |
160 | func (i *ipWatcher) Next() ([]*Update, error) { | 163 | func (i *ipWatcher) Next() ([]*Update, error) { |
161 | u, ok := <-i.updateChan | 164 | u, ok := <-i.updateChan |
162 | if !ok { | 165 | if !ok { |
@@ -192,28 +195,24 @@ type AddrMetadataGRPCLB struct { | |||
192 | 195 | ||
193 | // compileUpdate compares the old resolved addresses and newly resolved addresses, | 196 | // compileUpdate compares the old resolved addresses and newly resolved addresses, |
194 | // and generates an update list | 197 | // and generates an update list |
195 | func (w *dnsWatcher) compileUpdate(newAddrs []*Update) []*Update { | 198 | func (w *dnsWatcher) compileUpdate(newAddrs map[string]*Update) []*Update { |
196 | update := make(map[Update]bool) | 199 | var res []*Update |
197 | for _, u := range newAddrs { | 200 | for a, u := range w.curAddrs { |
198 | update[*u] = true | 201 | if _, ok := newAddrs[a]; !ok { |
199 | } | 202 | u.Op = Delete |
200 | for _, u := range w.curAddrs { | 203 | res = append(res, u) |
201 | if _, ok := update[*u]; ok { | ||
202 | delete(update, *u) | ||
203 | continue | ||
204 | } | 204 | } |
205 | update[Update{Addr: u.Addr, Op: Delete, Metadata: u.Metadata}] = true | ||
206 | } | 205 | } |
207 | res := make([]*Update, 0, len(update)) | 206 | for a, u := range newAddrs { |
208 | for k := range update { | 207 | if _, ok := w.curAddrs[a]; !ok { |
209 | tmp := k | 208 | res = append(res, u) |
210 | res = append(res, &tmp) | 209 | } |
211 | } | 210 | } |
212 | return res | 211 | return res |
213 | } | 212 | } |
214 | 213 | ||
215 | func (w *dnsWatcher) lookupSRV() []*Update { | 214 | func (w *dnsWatcher) lookupSRV() map[string]*Update { |
216 | var newAddrs []*Update | 215 | newAddrs := make(map[string]*Update) |
217 | _, srvs, err := lookupSRV(w.ctx, "grpclb", "tcp", w.host) | 216 | _, srvs, err := lookupSRV(w.ctx, "grpclb", "tcp", w.host) |
218 | if err != nil { | 217 | if err != nil { |
219 | grpclog.Infof("grpc: failed dns SRV record lookup due to %v.\n", err) | 218 | grpclog.Infof("grpc: failed dns SRV record lookup due to %v.\n", err) |
@@ -231,15 +230,16 @@ func (w *dnsWatcher) lookupSRV() []*Update { | |||
231 | grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err) | 230 | grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err) |
232 | continue | 231 | continue |
233 | } | 232 | } |
234 | newAddrs = append(newAddrs, &Update{Addr: a + ":" + strconv.Itoa(int(s.Port)), | 233 | addr := a + ":" + strconv.Itoa(int(s.Port)) |
235 | Metadata: AddrMetadataGRPCLB{AddrType: GRPCLB, ServerName: s.Target}}) | 234 | newAddrs[addr] = &Update{Addr: addr, |
235 | Metadata: AddrMetadataGRPCLB{AddrType: GRPCLB, ServerName: s.Target}} | ||
236 | } | 236 | } |
237 | } | 237 | } |
238 | return newAddrs | 238 | return newAddrs |
239 | } | 239 | } |
240 | 240 | ||
241 | func (w *dnsWatcher) lookupHost() []*Update { | 241 | func (w *dnsWatcher) lookupHost() map[string]*Update { |
242 | var newAddrs []*Update | 242 | newAddrs := make(map[string]*Update) |
243 | addrs, err := lookupHost(w.ctx, w.host) | 243 | addrs, err := lookupHost(w.ctx, w.host) |
244 | if err != nil { | 244 | if err != nil { |
245 | grpclog.Warningf("grpc: failed dns A record lookup due to %v.\n", err) | 245 | grpclog.Warningf("grpc: failed dns A record lookup due to %v.\n", err) |
@@ -251,7 +251,8 @@ func (w *dnsWatcher) lookupHost() []*Update { | |||
251 | grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err) | 251 | grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err) |
252 | continue | 252 | continue |
253 | } | 253 | } |
254 | newAddrs = append(newAddrs, &Update{Addr: a + ":" + w.port}) | 254 | addr := a + ":" + w.port |
255 | newAddrs[addr] = &Update{Addr: addr} | ||
255 | } | 256 | } |
256 | return newAddrs | 257 | return newAddrs |
257 | } | 258 | } |