reactphp/dns

DNS resolve get duplicated ip result

v1talM opened this issue · 3 comments

1/400000 chance (maybe) to return once duplicated result

// Please add code examples if possible, so we can reproduce your steps
$dns = new Resolver($executor);
$result = $dns->resolveAll($service, Message::TYPE_A)->then($closure);
try {
    await($result);
} catch (\Throwable $e) {
    // print log here
}

// closure function here
$func =  static function ($ips) use (&$serverList) {
    if (count($ips) != count(array_unique($ips))) {
        echo "get ips duplicated!\n";
    }
};
clue commented

@v1talM Thanks for reaching out, interesting question!

The resolveAll() method is designed to return all DNS records as provided by the DNS server. It's common for DNS servers to return multiple IP addresses for a single hostname, even if some of those IPs are not unique. There are a few valid reasons why a DNS server may return duplicate IP addresses, in particular for load balancing (think a split between multiple servers not having the same capacity).

The DNS server's behavior is transparent to the client application, which should handle the returned results accordingly, either by using all provided IPs or implementing its own logic for IP selection or deduplication.

I hope this helps! 👍 If so, consider supporting this project, for example by becoming a sponsor ❤️

@v1talM Thanks for reaching out, interesting question!

The resolveAll() method is designed to return all DNS records as provided by the DNS server. It's common for DNS servers to return multiple IP addresses for a single hostname, even if some of those IPs are not unique. There are a few valid reasons why a DNS server may return duplicate IP addresses, in particular for load balancing (think a split between multiple servers not having the same capacity).

The DNS server's behavior is transparent to the client application, which should handle the returned results accordingly, either by using all provided IPs or implementing its own logic for IP selection or deduplication.

I hope this helps! 👍 If so, consider supporting this project, for example by becoming a sponsor ❤️

Thank you 👍 but i have other questions.

I compared the return results of both the dns_get_record() method and the resolveAll() method simultaneously, php code here:

// run this loop 100000, and print diff result to log
while ($loop > 0) {
    $domain = "search.kylin.svc";
    $systemDnsResolve = dns_get_record($domain, DNS_A);
    $dns->getRecord($domain, $onDnsResolved);
    $loop -= 1;
}

and get diff log content like this:
service:[search.kylin.svc] dns resolved diff, dns_get_record:["10.210.44.26","10.210.44.24"], sync_get:["10.210.44.26"]

I get two different ips by dns_get_record(), but get only one ip result by resolveAll(), so confused😭

In fact the search.kylin.svc domain have two ip(10.210.44.26, 10.210.44.24) and never change, but sometimes resolveAll() method only return one ip result.

clue commented

I am a bit lost, can you please clarify your question for me? Your original report was about duplicate IPs but the new snippet shows different IPs apparently.

Very broadly: I'm not sure what you're trying to achieve exactly, what exactly your code is supposed to show, why you would want to run this in a loop and what problem you're seeing specifically. But yes, it's possible if they use different inputs or rely on external factors that can vary. For example, a function that depends on a remote DNS server may return different replies at different times.