dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/balancer/clusterresolver/resource_resolver_dns.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 * 20 * Copyright 2021 gRPC authors. 21 * 22 */ 23 24 package clusterresolver 25 26 import ( 27 "fmt" 28 ) 29 30 import ( 31 "google.golang.org/grpc/resolver" 32 33 "google.golang.org/grpc/serviceconfig" 34 ) 35 36 var ( 37 newDNS = func(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) { 38 // The dns resolver is registered by the grpc package. So, this call to 39 // resolver.Get() is never expected to return nil. 40 return resolver.Get("dns").Build(target, cc, opts) 41 } 42 ) 43 44 // dnsDiscoveryMechanism watches updates for the given DNS hostname. 45 // 46 // It implements resolver.ClientConn interface to work with the DNS resolver. 47 type dnsDiscoveryMechanism struct { 48 target string 49 topLevelResolver *resourceResolver 50 r resolver.Resolver 51 52 addrs []string 53 updateReceived bool 54 } 55 56 func newDNSResolver(target string, topLevelResolver *resourceResolver) *dnsDiscoveryMechanism { 57 ret := &dnsDiscoveryMechanism{ 58 target: target, 59 topLevelResolver: topLevelResolver, 60 } 61 r, err := newDNS(resolver.Target{Scheme: "dns", Endpoint: target}, ret, resolver.BuildOptions{}) 62 if err != nil { 63 select { 64 case <-topLevelResolver.updateChannel: 65 default: 66 } 67 topLevelResolver.updateChannel <- &resourceUpdate{err: err} 68 } 69 ret.r = r 70 return ret 71 } 72 73 func (dr *dnsDiscoveryMechanism) lastUpdate() (interface{}, bool) { 74 if !dr.updateReceived { 75 return nil, false 76 } 77 return dr.addrs, true 78 } 79 80 func (dr *dnsDiscoveryMechanism) resolveNow() { 81 dr.r.ResolveNow(resolver.ResolveNowOptions{}) 82 } 83 84 func (dr *dnsDiscoveryMechanism) stop() { 85 dr.r.Close() 86 } 87 88 // dnsDiscoveryMechanism needs to implement resolver.ClientConn interface to receive 89 // updates from the real DNS resolver. 90 91 func (dr *dnsDiscoveryMechanism) UpdateState(state resolver.State) error { 92 dr.topLevelResolver.mu.Lock() 93 defer dr.topLevelResolver.mu.Unlock() 94 addrs := make([]string, len(state.Addresses)) 95 for i, a := range state.Addresses { 96 addrs[i] = a.Addr 97 } 98 dr.addrs = addrs 99 dr.updateReceived = true 100 dr.topLevelResolver.generate() 101 return nil 102 } 103 104 func (dr *dnsDiscoveryMechanism) ReportError(err error) { 105 select { 106 case <-dr.topLevelResolver.updateChannel: 107 default: 108 } 109 dr.topLevelResolver.updateChannel <- &resourceUpdate{err: err} 110 } 111 112 func (dr *dnsDiscoveryMechanism) NewAddress(addresses []resolver.Address) { 113 dr.UpdateState(resolver.State{Addresses: addresses}) 114 } 115 116 func (dr *dnsDiscoveryMechanism) NewServiceConfig(string) { 117 // This method is deprecated, and service config isn't supported. 118 } 119 120 func (dr *dnsDiscoveryMechanism) ParseServiceConfig(string) *serviceconfig.ParseResult { 121 return &serviceconfig.ParseResult{Err: fmt.Errorf("service config not supported")} 122 }