github.com/hoveychen/kafka-go@v0.4.42/resolver.go (about) 1 package kafka 2 3 import ( 4 "context" 5 "net" 6 ) 7 8 // The Resolver interface is used as an abstraction to provide service discovery 9 // of the hosts of a kafka cluster. 10 type Resolver interface { 11 // LookupHost looks up the given host using the local resolver. 12 // It returns a slice of that host's addresses. 13 LookupHost(ctx context.Context, host string) (addrs []string, err error) 14 } 15 16 // BrokerResolver is an interface implemented by types that translate host 17 // names into a network address. 18 // 19 // This resolver is not intended to be a general purpose interface. Instead, 20 // it is tailored to the particular needs of the kafka protocol, with the goal 21 // being to provide a flexible mechanism for extending broker name resolution 22 // while retaining context that is specific to interacting with a kafka cluster. 23 // 24 // Resolvers must be safe to use from multiple goroutines. 25 type BrokerResolver interface { 26 // Returns the IP addresses of the broker passed as argument. 27 LookupBrokerIPAddr(ctx context.Context, broker Broker) ([]net.IPAddr, error) 28 } 29 30 // NewBrokerResolver constructs a Resolver from r. 31 // 32 // If r is nil, net.DefaultResolver is used instead. 33 func NewBrokerResolver(r *net.Resolver) BrokerResolver { 34 return brokerResolver{r} 35 } 36 37 type brokerResolver struct { 38 *net.Resolver 39 } 40 41 func (r brokerResolver) LookupBrokerIPAddr(ctx context.Context, broker Broker) ([]net.IPAddr, error) { 42 ipAddrs, err := r.LookupIPAddr(ctx, broker.Host) 43 if err != nil { 44 return nil, err 45 } 46 47 if len(ipAddrs) == 0 { 48 return nil, &net.DNSError{ 49 Err: "no addresses were returned by the resolver", 50 Name: broker.Host, 51 IsTemporary: true, 52 IsNotFound: true, 53 } 54 } 55 56 return ipAddrs, nil 57 }