github.com/kaydxh/golang@v0.0.131/go/net/resolver/resolver.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package resolver
    23  
    24  import (
    25  	"fmt"
    26  
    27  	"context"
    28  )
    29  
    30  // ResolveNowOptions includes additional information for ResolveNow.
    31  type ResolveNowOptions struct{}
    32  
    33  // Resolver watches for the updates on the specified target.
    34  // Updates include address updates and service config updates.
    35  type Resolver interface {
    36  	// It could be called multiple times concurrently.
    37  	ResolveOne(opts ...ResolveOneOption) (Address, error)
    38  
    39  	ResolveAll(opts ...ResolveAllOption) ([]Address, error)
    40  
    41  	// ResolveNow will be called by gRPC to try to resolve the target name
    42  	// again. It's just a hint, resolver can ignore this if it's not necessary.
    43  	//
    44  	// It could be called multiple times concurrently.
    45  	ResolveNow(opts ...ResolveNowOption)
    46  	// Close closes the resolver.
    47  	Close()
    48  }
    49  
    50  type Resolver_PickMode int32
    51  
    52  const (
    53  	Resolver_pick_mode_random Resolver_PickMode = 0
    54  	Resolver_pick_mode_first  Resolver_PickMode = 1
    55  )
    56  
    57  type Resolver_IPType int32
    58  
    59  const (
    60  	Resolver_ip_type_all Resolver_IPType = 0
    61  	Resolver_ip_type_v4  Resolver_IPType = 1
    62  	Resolver_ip_type_v6  Resolver_IPType = 2
    63  )
    64  
    65  type ResolveOneOptions struct {
    66  	PickMode Resolver_PickMode
    67  	IPType   Resolver_IPType
    68  }
    69  
    70  type ResolveAllOptions struct {
    71  	IPType Resolver_IPType
    72  }
    73  
    74  func GetResolver(ctx context.Context, target string, opts ...ResolverBuildOption) (Resolver, error) {
    75  	var opt ResolverBuildOptions
    76  	opt.ApplyOptions(opts...)
    77  	targetInfo, err := ParseTarget(target)
    78  	if err != nil {
    79  		// support no scheme, just ip:port format
    80  		if targetInfo.Scheme == "" {
    81  			return GetDefault().Build(Target{
    82  				Endpoint: target,
    83  			})
    84  		}
    85  
    86  		return nil, fmt.Errorf("target[%v] is invalid", targetInfo)
    87  	}
    88  
    89  	r := getResolver(target)
    90  	if r == nil {
    91  		builder := Get(targetInfo.Scheme)
    92  		if builder == nil {
    93  			return nil, fmt.Errorf("scheme[%v] was not registered", targetInfo.Scheme)
    94  		}
    95  
    96  		r, err = builder.Build(targetInfo)
    97  		if err != nil {
    98  			return nil, fmt.Errorf("failed to build target[%v], err: %v", targetInfo, err)
    99  		}
   100  
   101  		setResolver(target, r)
   102  	}
   103  	return r, nil
   104  }