X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=vendor%2Fgoogle.golang.org%2Fgrpc%2Fnaming%2Fdns_resolver.go;h=fd8cd3b5a4ff4bf1a997532ead95df1a2fa0f152;hb=107c1cdb09c575aa2f61d97f48d8587eb6bada4c;hp=efd37e304983aa4f0a1a4b269c201d3d06a6cbfa;hpb=844b5a68d8af4791755b8f0ad293cc99f5959183;p=github%2Ffretlink%2Fterraform-provider-statuscake.git 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 @@ package naming import ( + "context" "errors" "fmt" "net" "strconv" "time" - "golang.org/x/net/context" "google.golang.org/grpc/grpclog" ) @@ -37,6 +37,9 @@ const ( var ( errMissingAddr = errors.New("missing address") errWatcherClose = errors.New("watcher has been closed") + + lookupHost = net.DefaultResolver.LookupHost + lookupSRV = net.DefaultResolver.LookupSRV ) // NewDNSResolverWithFreq creates a DNS Resolver that can resolve DNS names, and @@ -141,8 +144,8 @@ type dnsWatcher struct { r *dnsResolver host string port string - // The latest resolved address list - curAddrs []*Update + // The latest resolved address set + curAddrs map[string]*Update ctx context.Context cancel context.CancelFunc t *time.Timer @@ -153,10 +156,10 @@ type ipWatcher struct { updateChan chan *Update } -// Next returns the adrress resolution Update for the target. For IP address, -// the resolution is itself, thus polling name server is unncessary. Therefore, +// Next returns the address resolution Update for the target. For IP address, +// the resolution is itself, thus polling name server is unnecessary. Therefore, // Next() will return an Update the first time it is called, and will be blocked -// for all following calls as no Update exisits until watcher is closed. +// for all following calls as no Update exists until watcher is closed. func (i *ipWatcher) Next() ([]*Update, error) { u, ok := <-i.updateChan if !ok { @@ -192,28 +195,24 @@ type AddrMetadataGRPCLB struct { // compileUpdate compares the old resolved addresses and newly resolved addresses, // and generates an update list -func (w *dnsWatcher) compileUpdate(newAddrs []*Update) []*Update { - update := make(map[Update]bool) - for _, u := range newAddrs { - update[*u] = true - } - for _, u := range w.curAddrs { - if _, ok := update[*u]; ok { - delete(update, *u) - continue +func (w *dnsWatcher) compileUpdate(newAddrs map[string]*Update) []*Update { + var res []*Update + for a, u := range w.curAddrs { + if _, ok := newAddrs[a]; !ok { + u.Op = Delete + res = append(res, u) } - update[Update{Addr: u.Addr, Op: Delete, Metadata: u.Metadata}] = true } - res := make([]*Update, 0, len(update)) - for k := range update { - tmp := k - res = append(res, &tmp) + for a, u := range newAddrs { + if _, ok := w.curAddrs[a]; !ok { + res = append(res, u) + } } return res } -func (w *dnsWatcher) lookupSRV() []*Update { - var newAddrs []*Update +func (w *dnsWatcher) lookupSRV() map[string]*Update { + newAddrs := make(map[string]*Update) _, srvs, err := lookupSRV(w.ctx, "grpclb", "tcp", w.host) if err != nil { grpclog.Infof("grpc: failed dns SRV record lookup due to %v.\n", err) @@ -231,15 +230,16 @@ func (w *dnsWatcher) lookupSRV() []*Update { grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err) continue } - newAddrs = append(newAddrs, &Update{Addr: a + ":" + strconv.Itoa(int(s.Port)), - Metadata: AddrMetadataGRPCLB{AddrType: GRPCLB, ServerName: s.Target}}) + addr := a + ":" + strconv.Itoa(int(s.Port)) + newAddrs[addr] = &Update{Addr: addr, + Metadata: AddrMetadataGRPCLB{AddrType: GRPCLB, ServerName: s.Target}} } } return newAddrs } -func (w *dnsWatcher) lookupHost() []*Update { - var newAddrs []*Update +func (w *dnsWatcher) lookupHost() map[string]*Update { + newAddrs := make(map[string]*Update) addrs, err := lookupHost(w.ctx, w.host) if err != nil { grpclog.Warningf("grpc: failed dns A record lookup due to %v.\n", err) @@ -251,7 +251,8 @@ func (w *dnsWatcher) lookupHost() []*Update { grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err) continue } - newAddrs = append(newAddrs, &Update{Addr: a + ":" + w.port}) + addr := a + ":" + w.port + newAddrs[addr] = &Update{Addr: addr} } return newAddrs }