1
0
mirror of https://github.com/danog/dns.git synced 2024-11-26 12:04:40 +01:00

Improve example formatting and add PTR example

This commit is contained in:
Niklas Keller 2017-09-12 17:05:40 +02:00
parent fcdf01de72
commit dd6e3c294a
5 changed files with 57 additions and 19 deletions

20
examples/_bootstrap.php Normal file
View File

@ -0,0 +1,20 @@
<?php
use Amp\Dns\Record;
require __DIR__ . "/../vendor/autoload.php";
function pretty_print_records(string $queryName, array $records) {
print "---------- " . $queryName . " " . str_repeat("-", 55 - strlen($queryName)) . " TTL --\r\n";
$format = "%-10s %-56s %-5d\r\n";
foreach ($records as $record) {
print sprintf($format, Record::getName($record->getType()), $record->getValue(), $record->getTtl());
}
}
function pretty_print_error(string $queryName, \Throwable $error) {
print "-- " . $queryName . " " . str_repeat("-", 70 - strlen($queryName)) . "\r\n";
print get_class($error) . ": " . $error->getMessage() . "\r\n";
}

View File

@ -1,6 +1,6 @@
<?php
require __DIR__ . "/../vendor/autoload.php";
require __DIR__ . "/_bootstrap.php";
use Amp\Dns;
use Amp\Loop;
@ -16,33 +16,27 @@ $domains = array_map(function ($line) {
array_shift($domains);
Loop::run(function () use ($domains) {
print "Starting sequential queries..." . PHP_EOL;
print "Starting sequential queries...\r\n\r\n";
$timings = [];
for ($i = 0; $i < 10; $i++) {
$start = microtime(1);
$domain = $domains[mt_rand(0, count($domains) - 1)];
print $domain . ": ";
$domain = $domains[random_int(0, count($domains) - 1)];
try {
$records = yield Dns\resolve($domain);
$records = array_map(function ($record) {
return $record->getValue();
}, $records);
print implode(", ", $records);
pretty_print_records($domain, yield Dns\resolve($domain));
} catch (Dns\ResolutionException $e) {
print get_class($e);
pretty_print_error($domain, $e);
}
$time = round(microtime(1) - $start, 2);
$timings[] = $time;
print " in " . $time . " ms" . PHP_EOL;
printf("%'-74s\r\n\r\n", " in " . $time . " ms");
}
print PHP_EOL;
print (array_sum($timings) / count($timings)) . " ms for an average query." . PHP_EOL;
$averageTime = array_sum($timings) / count($timings);
print "{$averageTime} ms for an average query." . PHP_EOL;
});

View File

@ -1,6 +1,6 @@
<?php
require __DIR__ . "/../vendor/autoload.php";
require __DIR__ . "/_bootstrap.php";
use Amp\Dns;
use Amp\Loop;
@ -22,5 +22,11 @@ $customConfigLoader = new class implements Dns\ConfigLoader {
Dns\resolver(new Dns\BasicResolver(null, $customConfigLoader));
Loop::run(function () {
var_dump(yield Dns\resolve("google.com"));
$hostname = "amphp.org";
try {
pretty_print_records($hostname, yield Dns\resolve($hostname));
} catch (Dns\ResolutionException $e) {
pretty_print_error($hostname, $e);
}
});

16
examples/ptr-record.php Normal file
View File

@ -0,0 +1,16 @@
<?php
require __DIR__ . "/_bootstrap.php";
use Amp\Dns;
use Amp\Loop;
Loop::run(function () {
$ip = "8.8.8.8";
try {
pretty_print_records($ip, yield Dns\query($ip, Dns\Record::PTR));
} catch (Dns\ResolutionException $e) {
pretty_print_error($ip, $e);
}
});

View File

@ -174,8 +174,10 @@ final class BasicResolver implements Resolver {
/** @inheritdoc */
public function query(string $name, int $type): Promise {
if (isset($this->pendingQueries[$type . " " . $name])) {
return $this->pendingQueries[$type . " " . $name];
$pendingQueryKey = $type . " " . $name;
if (isset($this->pendingQueries[$pendingQueryKey])) {
return $this->pendingQueries[$pendingQueryKey];
}
$promise = call(function () use ($name, $type) {