github.com/rbisecke/kafka-go@v0.4.27/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 rslv := r.Resolver 43 if rslv == nil { 44 rslv = net.DefaultResolver 45 } 46 47 ipAddrs, err := r.LookupIPAddr(ctx, broker.Host) 48 if err != nil { 49 return nil, err 50 } 51 52 if len(ipAddrs) == 0 { 53 return nil, &net.DNSError{ 54 Err: "no addresses were returned by the resolver", 55 Name: broker.Host, 56 IsTemporary: true, 57 IsNotFound: true, 58 } 59 } 60 61 return ipAddrs, nil 62 }