github.com/searKing/golang/go@v1.2.117/net/resolver/passthrough/passthrough.go (about)

     1  // Copyright 2021 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package passthrough
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/searKing/golang/go/net/resolver"
    11  )
    12  
    13  const scheme = "passthrough"
    14  
    15  func init() {
    16  	resolver.Register(&passthroughBuilder{})
    17  }
    18  
    19  type passthroughBuilder struct{}
    20  
    21  func (*passthroughBuilder) Build(ctx context.Context, target resolver.Target, opts ...resolver.BuildOption) (resolver.Resolver, error) {
    22  	var opt resolver.Build
    23  	opt.ApplyOptions(opts...)
    24  	r := &passthroughResolver{
    25  		target: target,
    26  		cc:     opt.ClientConn,
    27  	}
    28  	r.start()
    29  	return r, nil
    30  }
    31  
    32  func (*passthroughBuilder) Scheme() string {
    33  	return scheme
    34  }
    35  
    36  type passthroughResolver struct {
    37  	target resolver.Target
    38  	cc     resolver.ClientConn
    39  }
    40  
    41  func (r *passthroughResolver) ResolveOneAddr(ctx context.Context, opts ...resolver.ResolveOneAddrOption) (resolver.Address, error) {
    42  	return resolver.Address{Addr: r.target.Endpoint}, nil
    43  }
    44  
    45  func (r *passthroughResolver) ResolveAddr(ctx context.Context, opts ...resolver.ResolveAddrOption) ([]resolver.Address, error) {
    46  	return []resolver.Address{{Addr: r.target.Endpoint}}, nil
    47  }
    48  
    49  func (r *passthroughResolver) ResolveNow(ctx context.Context, opts ...resolver.ResolveNowOption) {}
    50  
    51  func (r *passthroughResolver) start() {
    52  	if r.cc != nil {
    53  		_ = r.cc.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: r.target.Endpoint}}})
    54  	}
    55  }
    56  
    57  func (*passthroughResolver) Close() {}