dns icon indicating copy to clipboard operation
dns copied to clipboard

Async example?

Open dragonattack opened this issue 2 years ago • 1 comments

Can you please share a link to example of async dns query loop of 500 domains for example.

Of course, simply changing Dns\resolve($domain) in /examples/benchmark.php to:

        $host = Amp\Future\awaitFirst([
            Amp\async(fn() => Dns\resolve($domain, Dns\DnsRecord::A)),
        ]);

doesn't make any difference.

dragonattack avatar Jul 18 '23 09:07 dragonattack

Apologies for the (very) late reply, this issue slipped under my radar.

To perform multiple queries simultaneously (or multiple of any operation which will await I/O), use Amp\async() in conjunction with Amp\Future\await() to await the set of futures created from async().

$listOfDomains = [...]; // List of domain names to query.

$futures = [];
foreach ($listOfDomains as $domain) {
    $futures[$domain] = Amp\async(Dns\resolve(...), $domain);
}

$resolvedRecords = Amp\Future\await($futures);

Note it may not be a good idea to initiate 500 DNS requests simultaneously. As with many simultaneous async operations, it may be a good idea to throttle the number of simultaneous requests in-flight. See amphp/sync and amphp/pipeline for tools to do just this.

trowski avatar Nov 18 '23 14:11 trowski