func (ap *addressProvider) next(ctx context.Context) (*resolvedAddress, error) {
	if len(ap.resolvedAddrs) == 0 {
		// If we have ran out of addresses to resolve but we have
		// resolved some via the DNS cache, make another pass for
		// those with an empty DNS cache to refresh any stale entries.
		if len(ap.addrPool) == 0 && len(ap.cachedAddrPool) > 0 {
			ap.addrPool = ap.cachedAddrPool
			ap.cachedAddrPool = nil
			ap.dnsCache = emptyDNSCache{ap.dnsCache}
		}

		// Resolve the next host from the address pool
		if len(ap.addrPool) != 0 {
			next := ap.addrPool[0]
			ap.addrPool = ap.addrPool[1:]

			host, port, err := net.SplitHostPort(next)
			if err != nil {
				return nil, errors.Errorf("invalid address %q: %v", next, err)
			}

			ips := ap.dnsCache.Lookup(host)
			if len(ips) > 0 {
				ap.cachedAddrPool = append(ap.cachedAddrPool, next)
			} else if isNumericHost(host) {
				ips = []string{host}
			} else {
				var err error
				ips, err = lookupIPAddr(ctx, host, ap.ipAddrResolver)
				if err != nil {
					return nil, errors.Errorf("cannot resolve %q: %v", host, err)
				}
				ap.dnsCache.Add(host, ips)
				logger.Debugf("looked up %v -> %v", host, ips)
			}

			for _, ip := range ips {
				ap.resolvedAddrs = append(ap.resolvedAddrs, &resolvedAddress{
					host: next,
					ip:   ip,
					port: port,
				})
			}
		}
	}

	// Ran out of resolved addresses and cached addresses
	if len(ap.resolvedAddrs) == 0 {
		return nil, io.EOF
	}

	next := ap.resolvedAddrs[0]
	ap.resolvedAddrs = ap.resolvedAddrs[1:]
	return next, nil
}
// next returns back either a successfully resolved address or the error that
// occurred while attempting to resolve the next address candidate. Calls to
// next return io.EOF to indicate that no more addresses are available.
next returns back either a successfully resolved address or the error that
occurred while attempting to resolve the next address candidate. Calls to
next return io.EOF to indicate that no more addresses are available.
// next returns back either a successfully resolved address or the error that // occurred while attempting to resolve the next address candidate . Calls to // next return io . EOF to indicate that no more addresses are available .
func (ap *addressProvider) next(ctx context.Context) (*resolvedAddress, error) {
	if len(ap.resolvedAddrs) == 0 {
		if len(ap.addrPool) == 0 && len(ap.cachedAddrPool) > 0 {
			ap.addrPool = ap.cachedAddrPool
			ap.cachedAddrPool = nil
			ap.dnsCache = emptyDNSCache{ap.dnsCache}
		}
		if len(ap.addrPool) != 0 {
			next := ap.addrPool[0]
			ap.addrPool = ap.addrPool[1:]
			host, port, err := net.SplitHostPort(next)
			if err != nil {
				return nil, errors.Errorf("invalid address %q: %v", next, err)
			}
			ips := ap.dnsCache.Lookup(host)
			if len(ips) > 0 {
				ap.cachedAddrPool = append(ap.cachedAddrPool, next)
			} else if isNumericHost(host) {
				ips = []string{host}
			} else {
				var err error
				ips, err = lookupIPAddr(ctx, host, ap.ipAddrResolver)
				if err != nil {
					return nil, errors.Errorf("cannot resolve %q: %v", host, err)
				}
				ap.dnsCache.Add(host, ips)
				logger.Debugf("looked up %v -> %v", host, ips)
			}
			for _, ip := range ips {
				ap.resolvedAddrs = append(ap.resolvedAddrs, &resolvedAddress{
					host: next,
					ip:   ip,
					port: port,
				})
			}
		}
	}
	if len(ap.resolvedAddrs) == 0 {
		return nil, io.EOF
	}
	next := ap.resolvedAddrs[0]
	ap.resolvedAddrs = ap.resolvedAddrs[1:]
	return next, nil
}
func ( ap * addressProvider ) next ( ctx context . Context ) ( * resolvedAddress , error ) { if len ( ap . resolvedAddrs ) == 0 { if len ( ap . addrPool ) == 0 && len ( ap . cachedAddrPool ) > 0 { ap . addrPool = ap . cachedAddrPool ap . cachedAddrPool = nil ap . dnsCache = emptyDNSCache { ap . dnsCache } } if len ( ap . addrPool ) != 0 { next := ap . addrPool [ 0 ] ap . addrPool = ap . addrPool [ 1 : ] host , port , err := net . SplitHostPort ( next ) if err != nil { return nil , errors . Errorf ( " " , next , err ) } ips := ap . dnsCache . Lookup ( host ) if len ( ips ) > 0 { ap . cachedAddrPool = append ( ap . cachedAddrPool , next ) } else if isNumericHost ( host ) { ips = [ ] string { host } } else { var err error ips , err = lookupIPAddr ( ctx , host , ap . ipAddrResolver ) if err != nil { return nil , errors . Errorf ( " " , host , err ) } ap . dnsCache . Add ( host , ips ) logger . Debugf ( " " , host , ips ) } for _ , ip := range ips { ap . resolvedAddrs = append ( ap . resolvedAddrs , & resolvedAddress { host : next , ip : ip , port : port , } ) } } } if len ( ap . resolvedAddrs ) == 0 { return nil , io . EOF } next := ap . resolvedAddrs [ 0 ] ap . resolvedAddrs = ap . resolvedAddrs [ 1 : ] return next , nil }
next returns back either a successfully resolved address or the error that
occurred while attempting to resolve the next address candidate.
next returns back either a successfully resolved address or the error that occurred while attempting to resolve the next address candidate .
// If we have ran out of addresses to resolve but we have // resolved some via the DNS cache, make another pass for // those with an empty DNS cache to refresh any stale entries. // Resolve the next host from the address pool // Ran out of resolved addresses and cached addresses