gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/balancer/clusterresolver/resource_resolver_dns.go (about)

     1  /*
     2   *
     3   * Copyright 2021 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 clusterresolver
    20  
    21  import (
    22  	"fmt"
    23  	"net/url"
    24  
    25  	"gitee.com/ks-custle/core-gm/grpc/resolver"
    26  	"gitee.com/ks-custle/core-gm/grpc/serviceconfig"
    27  )
    28  
    29  var (
    30  	newDNS = func(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
    31  		// The dns resolver is registered by the grpc package. So, this call to
    32  		// resolver.Get() is never expected to return nil.
    33  		return resolver.Get("dns").Build(target, cc, opts)
    34  	}
    35  )
    36  
    37  // dnsDiscoveryMechanism watches updates for the given DNS hostname.
    38  //
    39  // It implements resolver.ClientConn interface to work with the DNS resolver.
    40  type dnsDiscoveryMechanism struct {
    41  	target           string
    42  	topLevelResolver *resourceResolver
    43  	r                resolver.Resolver
    44  
    45  	addrs          []string
    46  	updateReceived bool
    47  }
    48  
    49  func newDNSResolver(target string, topLevelResolver *resourceResolver) *dnsDiscoveryMechanism {
    50  	ret := &dnsDiscoveryMechanism{
    51  		target:           target,
    52  		topLevelResolver: topLevelResolver,
    53  	}
    54  	// Target.Scheme、Target.Endpoint are deprecated, use URL.Scheme、URL.Path instead.
    55  	//r, err := newDNS(resolver.Target{Scheme: "dns", Endpoint: target}, ret, resolver.BuildOptions{})
    56  	r, err := newDNS(resolver.Target{URL: url.URL{Scheme: "dns", Path: target}}, ret, resolver.BuildOptions{})
    57  	if err != nil {
    58  		select {
    59  		case <-topLevelResolver.updateChannel:
    60  		default:
    61  		}
    62  		topLevelResolver.updateChannel <- &resourceUpdate{err: err}
    63  	}
    64  	ret.r = r
    65  	return ret
    66  }
    67  
    68  func (dr *dnsDiscoveryMechanism) lastUpdate() (interface{}, bool) {
    69  	if !dr.updateReceived {
    70  		return nil, false
    71  	}
    72  	return dr.addrs, true
    73  }
    74  
    75  func (dr *dnsDiscoveryMechanism) resolveNow() {
    76  	dr.r.ResolveNow(resolver.ResolveNowOptions{})
    77  }
    78  
    79  func (dr *dnsDiscoveryMechanism) stop() {
    80  	dr.r.Close()
    81  }
    82  
    83  // dnsDiscoveryMechanism needs to implement resolver.ClientConn interface to receive
    84  // updates from the real DNS resolver.
    85  
    86  func (dr *dnsDiscoveryMechanism) UpdateState(state resolver.State) error {
    87  	dr.topLevelResolver.mu.Lock()
    88  	defer dr.topLevelResolver.mu.Unlock()
    89  	addrs := make([]string, len(state.Addresses))
    90  	for i, a := range state.Addresses {
    91  		addrs[i] = a.Addr
    92  	}
    93  	dr.addrs = addrs
    94  	dr.updateReceived = true
    95  	dr.topLevelResolver.generate()
    96  	return nil
    97  }
    98  
    99  func (dr *dnsDiscoveryMechanism) ReportError(err error) {
   100  	select {
   101  	case <-dr.topLevelResolver.updateChannel:
   102  	default:
   103  	}
   104  	dr.topLevelResolver.updateChannel <- &resourceUpdate{err: err}
   105  }
   106  
   107  func (dr *dnsDiscoveryMechanism) NewAddress(addresses []resolver.Address) {
   108  	dr.UpdateState(resolver.State{Addresses: addresses})
   109  }
   110  
   111  func (dr *dnsDiscoveryMechanism) NewServiceConfig(string) {
   112  	// This method is deprecated, and service config isn't supported.
   113  }
   114  
   115  func (dr *dnsDiscoveryMechanism) ParseServiceConfig(string) *serviceconfig.ParseResult {
   116  	return &serviceconfig.ParseResult{Err: fmt.Errorf("service config not supported")}
   117  }