mirror of
https://github.com/danog/dns.git
synced 2024-11-26 20:14:51 +01:00
Improve example formatting and add PTR example
This commit is contained in:
parent
fcdf01de72
commit
dd6e3c294a
20
examples/_bootstrap.php
Normal file
20
examples/_bootstrap.php
Normal 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";
|
||||
}
|
@ -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;
|
||||
});
|
||||
|
@ -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
16
examples/ptr-record.php
Normal 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);
|
||||
}
|
||||
});
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user