github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xresolver/xresolver.go (about)

     1  package xresolver
     2  
     3  import (
     4  	"strings"
     5  
     6  	"google.golang.org/grpc/resolver"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    11  )
    12  
    13  type dnsBuilder struct {
    14  	resolver.Builder
    15  	scheme string
    16  	trace  *trace.Driver
    17  }
    18  
    19  type clientConn struct {
    20  	resolver.ClientConn
    21  	target resolver.Target
    22  	trace  *trace.Driver
    23  }
    24  
    25  func (c *clientConn) Endpoint() string {
    26  	endpoint := c.target.URL.Path
    27  	if endpoint == "" {
    28  		endpoint = c.target.URL.Opaque
    29  	}
    30  
    31  	return strings.TrimPrefix(endpoint, "/")
    32  }
    33  
    34  func (c *clientConn) UpdateState(state resolver.State) (err error) {
    35  	onDone := trace.DriverOnResolve(c.trace,
    36  		stack.FunctionID(""),
    37  		c.Endpoint(), func() (addrs []string) {
    38  			for i := range state.Addresses {
    39  				addrs = append(addrs, state.Addresses[i].Addr)
    40  			}
    41  
    42  			return
    43  		}(),
    44  	)
    45  	defer func() {
    46  		onDone(err)
    47  	}()
    48  
    49  	err = c.ClientConn.UpdateState(state)
    50  	if err != nil {
    51  		return xerrors.WithStackTrace(err)
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func (d *dnsBuilder) Build(
    58  	target resolver.Target, //nolint:gocritic
    59  	cc resolver.ClientConn,
    60  	opts resolver.BuildOptions,
    61  ) (resolver.Resolver, error) {
    62  	return d.Builder.Build(target, &clientConn{
    63  		ClientConn: cc,
    64  		target:     target,
    65  		trace:      d.trace,
    66  	}, opts)
    67  }
    68  
    69  func (d *dnsBuilder) Scheme() string {
    70  	return d.scheme
    71  }
    72  
    73  func New(scheme string, trace *trace.Driver) resolver.Builder {
    74  	return &dnsBuilder{
    75  		Builder: resolver.Get("dns"),
    76  		scheme:  scheme,
    77  		trace:   trace,
    78  	}
    79  }