gitee.com/h79/goutils@v1.22.10/thrift/resolver/resolver.go (about) 1 package resolver 2 3 import ( 4 "gitee.com/h79/goutils/common/attributes" 5 ) 6 7 var ( 8 // m is a map from scheme to resolver builder. 9 m = make(map[string]Builder) 10 ) 11 12 func Register(b Builder) { 13 m[b.Scheme()] = b 14 } 15 16 // Get returns the resolver builder registered with the given scheme. 17 // 18 // If no builder is register with the scheme, nil will be returned. 19 func Get(scheme string) Builder { 20 if b, ok := m[scheme]; ok { 21 return b 22 } 23 return nil 24 } 25 26 type Target struct { 27 Scheme string 28 Authority string 29 Endpoint string 30 } 31 32 // Builder creates a resolver that will be used to watch name resolution updates. 33 type Builder interface { 34 // Build creates a new resolver for the given target. 35 // 36 // gRPC dial calls Build synchronously, and fails if the returned error is 37 // not nil. 38 Build(target Target, cc Connector) (Resolver, error) 39 40 // Scheme returns the scheme supported by this resolver. 41 // Scheme is defined at https://github.com/grpc/grpc/blob/master/doc/naming.md. 42 Scheme() string 43 } 44 45 // Resolver watches for the updates on the specified target. 46 // Updates include address updates and service config updates. 47 type Resolver interface { 48 // ResolveNow will be called by gRPC to try to resolve the target name 49 // again. It's just a hint, resolver can ignore this if it's not necessary. 50 // 51 // It could be called multiple times concurrently. 52 ResolveNow() 53 54 // Close closes the resolver. 55 Close() 56 } 57 58 type Connector interface { 59 // UpdateState updates the state of the ClientConn appropriately. 60 UpdateState(State) 61 62 // ReportError notifies the ClientConn that the Resolver encountered an 63 // error. The ClientConn will notify the load balancer and begin calling 64 // ResolveNow on the Resolver with exponential backoff. 65 ReportError(error) 66 } 67 68 // Notice: This type is EXPERIMENTAL and may be changed or removed in a 69 // later release. 70 type Address struct { 71 // Addr is the server address on which a connection will be established. 72 Addr string 73 74 // ServerName is the name of this address. 75 // If non-empty, the ServerName is used as the transport certification authority for 76 // the address, instead of the hostname from the Dial target string. In most cases, 77 // this should not be set. 78 // 79 // If Method is GRPCLB, ServerName should be the name of the remote load 80 // balancer, not the name of the backend. 81 // 82 // WARNING: ServerName must only be populated with trusted values. It 83 // is insecure to populate it with data from untrusted inputs since untrusted 84 // values could be used to bypass the authority checks performed by TLS. 85 ServerName string 86 87 // Attributes contains arbitrary data about this address intended for 88 // consumption by the load balancing policy. 89 Attributes *attributes.Attributes 90 } 91 92 // State contains the current Resolver state relevant to the ClientConn. 93 type State struct { 94 // Addresses is the latest set of resolved addresses for the target. 95 Addresses []Address 96 97 // Attributes contains arbitrary data about the resolver intended for 98 // consumption by the load balancing policy. 99 Attributes *attributes.Attributes 100 }