github.com/searKing/golang/go@v1.2.117/net/resolver/manual/manaul.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 manual 6 7 import ( 8 "context" 9 10 "github.com/searKing/golang/go/net/resolver" 11 ) 12 13 // NewBuilderWithScheme creates a new test resolver builder with the given scheme. 14 func NewBuilderWithScheme(scheme string) *Resolver { 15 r := &Resolver{ 16 ResolveNowCallback: func(ctx context.Context, opts ...resolver.ResolveNowOption) {}, 17 scheme: scheme, 18 } 19 r.ResolveOneAddrCallback = func(ctx context.Context, addrs []resolver.Address, opts ...resolver.ResolveOneAddrOption) (resolver.Address, error) { 20 return resolver.PickFirst(ctx, addrs) 21 } 22 r.ResolveAddrCallback = func(ctx context.Context, addrs []resolver.Address, opts ...resolver.ResolveAddrOption) ([]resolver.Address, error) { 23 return addrs, nil 24 } 25 26 return r 27 } 28 29 // Resolver is also a resolver builder. 30 // It's build() function always returns itself. 31 type Resolver struct { 32 ResolveOneAddrCallback func(ctx context.Context, addrs []resolver.Address, opts ...resolver.ResolveOneAddrOption) (resolver.Address, error) 33 ResolveAddrCallback func(ctx context.Context, addrs []resolver.Address, opts ...resolver.ResolveAddrOption) ([]resolver.Address, error) 34 // ResolveNowCallback is called when the ResolveNow method is called on the 35 // resolver. Must not be nil. Must not be changed after the resolver may 36 // be built. 37 ResolveNowCallback func(ctx context.Context, opts ...resolver.ResolveNowOption) 38 scheme string 39 40 // Addresses is the latest set of resolved addresses for the target. 41 Addresses []resolver.Address 42 43 // Fields actually belong to the resolver. 44 CC resolver.ClientConn 45 bootstrapState *resolver.State 46 } 47 48 // InitialState adds initial state to the resolver so that UpdateState doesn't 49 // need to be explicitly called after Dial. 50 func (r *Resolver) InitialState(s resolver.State) { 51 r.bootstrapState = &s 52 } 53 54 // Build returns itself for Resolver, because it's both a builder and a resolver. 55 func (r *Resolver) Build(ctx context.Context, target resolver.Target, cc resolver.ClientConn, opts ...resolver.ResolveNowOption) (resolver.Resolver, error) { 56 r.CC = cc 57 if r.bootstrapState != nil { 58 r.UpdateState(*r.bootstrapState) 59 } 60 return r, nil 61 } 62 63 // Scheme returns the test scheme. 64 func (r *Resolver) Scheme() string { 65 return r.scheme 66 } 67 68 // ResolveOneAddr is a noop for Resolver. 69 func (r *Resolver) ResolveOneAddr(ctx context.Context, opts ...resolver.ResolveOneAddrOption) (resolver.Address, error) { 70 return r.Addresses[0], nil 71 } 72 73 // ResolveAddr is a noop for Resolver. 74 func (r *Resolver) ResolveAddr(ctx context.Context, opts ...resolver.ResolveAddrOption) ([]resolver.Address, error) { 75 return r.Addresses, nil 76 } 77 78 // ResolveNow is a noop for Resolver. 79 func (r *Resolver) ResolveNow(ctx context.Context, opts ...resolver.ResolveNowOption) { 80 r.ResolveNowCallback(ctx, opts...) 81 } 82 83 // Close is a noop for Resolver. 84 func (*Resolver) Close() {} 85 86 // UpdateState calls CC.UpdateState. 87 func (r *Resolver) UpdateState(s resolver.State) { 88 _ = r.CC.UpdateState(s) 89 }