1
0
mirror of https://github.com/danog/dns.git synced 2025-01-23 05:51:11 +01:00

Update for Revolt changes

This commit is contained in:
Aaron Piotrowski 2021-10-16 11:01:15 -05:00
parent 23cad3b07b
commit 0ee5a9c323
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
8 changed files with 27 additions and 27 deletions

View File

@ -21,7 +21,7 @@ require __DIR__ . '/examples/_bootstrap.php';
use Amp\Dns;
use Amp\Loop;
Loop::run(function () {
EventLoop::run(function () {
$githubIpv4 = yield Dns\resolve("github.com", Dns\Record::A);
pretty_print_records("github.com", $githubIpv4);

View File

@ -39,15 +39,15 @@
"php": ">=8",
"ext-json": "*",
"ext-filter": "*",
"amphp/amp": "dev-v3-revolt as 3",
"amphp/byte-stream": "dev-v2-revolt as 2",
"amphp/cache": "dev-v2-revolt as 2",
"amphp/amp": "v3.x-dev",
"amphp/byte-stream": "v2.x-dev",
"amphp/cache": "v2.x-dev",
"amphp/parser": "^1",
"amphp/windows-registry": "dev-v1-revolt as 1",
"amphp/windows-registry": "v1.x-dev",
"daverandom/libdns": "^2.0.1"
},
"require-dev": {
"amphp/phpunit-util": "dev-v2-revolt",
"amphp/phpunit-util": "v2.x-dev",
"phpunit/phpunit": "^9",
"amphp/php-cs-fixer-config": "dev-master"
},

View File

@ -18,7 +18,7 @@ composer require amphp/dns
It respects the system's hosts file on Unix and Windows based systems, so it works just fine in environments like Docker with named containers.
The package uses a global default resolver with can be accessed and changed via `Amp\Dns\resolver()`. If an argument other than `null` is given, the given resolver is used as global instance. The instance is automatically bound to the current event loop. If you replace the event loop via `Amp\Loop::set()`, then you have to set a new global resolver.
The package uses a global default resolver with can be accessed and changed via `Amp\Dns\resolver()`. If an argument other than `null` is given, the given resolver is used as global instance.
Usually you don't have to change the resolver. If you want to use a custom configuration for a certain request, you can create a new resolver instance and use that instead of changing the global one.
@ -44,7 +44,7 @@ $records = yield Amp\Dns\resolve("github.com", Amp\Dns\Record::A);
### Custom Queries
`Amp\Dns\query` supports the various other DNS record types such as `MX`, `PTR`, or `TXT`. It automatically rewrites passed IP addresses for `PTR` lookups.
```php
/** @var Amp\Dns\Record[] $records */
$records = Amp\Dns\query("google.com", Amp\Dns\Record::MX);
@ -57,14 +57,14 @@ $records = Amp\Dns\query("8.8.8.8", Amp\Dns\Record::PTR);
### Caching
The `Rfc1035StubResolver` caches responses by default in an `Amp\Cache\ArrayCache`. You can set any other `Amp\Cache\Cache` implementation by creating a custom instance of `Rfc1035StubResolver` and setting that via `Amp\Dns\resolver()`, but it's usually unnecessary. If you have a lot of very short running scripts, you might want to consider using a local DNS resolver with a cache instead of setting a custom cache implementation, such as `dnsmasq`.
The `Rfc1035StubResolver` caches responses by default in an `Amp\Cache\ArrayCache`. You can set any other `Amp\Cache\Cache` implementation by creating a custom instance of `Rfc1035StubResolver` and setting that via `Amp\Dns\resolver()`, but it's usually unnecessary. If you have a lot of very short running scripts, you might want to consider using a local DNS resolver with a cache instead of setting a custom cache implementation, such as `dnsmasq`.
### Reloading Configuration
The `Rfc1035StubResolver` (which is the default resolver shipping with that package) will cache the configuration of `/etc/resolv.conf` / the Windows Registry and the read host files by default. If you wish to reload them, you can set a periodic timer that requests a background reload of the configuration.
```php
Loop::repeat(60000, function () use ($resolver) {
EventLoop::repeat(600, function () use ($resolver) {
yield Amp\Dns\resolver()->reloadConfig();
});
```

View File

@ -14,7 +14,7 @@ use LibDNS\Messages\Message;
use LibDNS\Messages\MessageFactory;
use LibDNS\Messages\MessageTypes;
use LibDNS\Records\Question;
use function Revolt\EventLoop\queue;
use function Revolt\launch;
/** @internal */
abstract class Socket
@ -48,7 +48,7 @@ abstract class Socket
}
private function fetch(): void {
queue(function (): void {
launch(function (): void {
try {
$this->handleResolution(null, $this->receive());
} catch (\Throwable $exception) {

View File

@ -12,12 +12,12 @@ use LibDNS\Decoder\DecoderFactory;
use LibDNS\Encoder\Encoder;
use LibDNS\Encoder\EncoderFactory;
use LibDNS\Messages\Message;
use Revolt\EventLoop\Loop;
use Revolt\EventLoop;
/** @internal */
final class TcpSocket extends Socket
{
public static function connect(string $uri, int $timeout = 5000): self
public static function connect(string $uri, float $timeout = 5): self
{
if (!$socket = @\stream_socket_client($uri, $errno, $errstr, 0, STREAM_CLIENT_ASYNC_CONNECT)) {
throw new DnsException(\sprintf(
@ -32,15 +32,15 @@ final class TcpSocket extends Socket
$deferred = new Deferred;
$watcher = Loop::onWritable($socket, static function (string $watcher) use ($socket, $deferred): void {
Loop::cancel($watcher);
$watcher = EventLoop::onWritable($socket, static function (string $watcher) use ($socket, $deferred): void {
EventLoop::cancel($watcher);
$deferred->complete(new self($socket));
});
try {
return $deferred->getFuture()->await(new TimeoutCancellationToken($timeout));
} catch (CancelledException) {
Loop::cancel($watcher);
EventLoop::cancel($watcher);
throw new TimeoutException("Name resolution timed out, could not connect to server at $uri");
}
}

View File

@ -12,7 +12,7 @@ use Amp\Future;
use LibDNS\Messages\Message;
use LibDNS\Records\Question;
use LibDNS\Records\QuestionFactory;
use Revolt\EventLoop\Loop;
use Revolt\EventLoop;
use function Amp\coroutine;
final class Rfc1035StubResolver implements Resolver
@ -51,7 +51,7 @@ final class Rfc1035StubResolver implements Resolver
public function __construct(Cache $cache = null, ConfigLoader $configLoader = null)
{
$this->cache = $cache ?? new ArrayCache(5000 /* default gc interval */, 256 /* size */);
$this->cache = $cache ?? new ArrayCache(maxSize: 256);
$this->configLoader = $configLoader ?? (\stripos(PHP_OS, "win") === 0
? new WindowsConfigLoader
: new UnixConfigLoader);
@ -60,7 +60,7 @@ final class Rfc1035StubResolver implements Resolver
$this->blockingFallbackResolver = new BlockingFallbackResolver;
$sockets = &$this->sockets;
$this->gcWatcher = Loop::repeat(5000, static function () use (&$sockets) {
$this->gcWatcher = EventLoop::repeat(5, static function () use (&$sockets): void {
if (!$sockets) {
return;
}
@ -75,12 +75,12 @@ final class Rfc1035StubResolver implements Resolver
}
});
Loop::unreference($this->gcWatcher);
EventLoop::unreference($this->gcWatcher);
}
public function __destruct()
{
Loop::cancel($this->gcWatcher);
EventLoop::cancel($this->gcWatcher);
}
/** @inheritdoc */
@ -317,7 +317,7 @@ final class Rfc1035StubResolver implements Resolver
// UDP sockets are never reused, they're not in the $this->sockets map
if ($protocol === "udp") {
// Defer call, because it interferes with the unreference() call in Internal\Socket otherwise
Loop::defer(static function () use ($socket): void {
EventLoop::defer(static function () use ($socket): void {
$socket->close();
});
}
@ -367,7 +367,7 @@ final class Rfc1035StubResolver implements Resolver
}, $result[$type]);
} catch (TimeoutException $e) {
// Defer call, because it might interfere with the unreference() call in Internal\Socket otherwise
Loop::defer(function () use ($socket, $uri): void {
EventLoop::defer(function () use ($socket, $uri): void {
unset($this->sockets[$uri]);
$socket->close();
});

View File

@ -2,7 +2,7 @@
namespace Amp\Dns;
use Revolt\EventLoop\Loop;
use Revolt\EventLoop;
/**
* Retrieve the application-wide dns resolver instance.
@ -15,7 +15,7 @@ function resolver(Resolver $resolver = null): Resolver
{
static $map;
$map ??= new \WeakMap();
$driver = Loop::getDriver();
$driver = EventLoop::getDriver();
if ($resolver) {
return $map[$driver] = $resolver;

View File

@ -13,7 +13,7 @@ class TcpSocketTest extends SocketTest
public function testTimeout(): void
{
$this->expectException(Dns\TimeoutException::class);
Dns\Internal\TcpSocket::connect("tcp://8.8.8.8:53", 0);
Dns\Internal\TcpSocket::connect("tcp://8.8.9.9:53", 0);
}
public function testInvalidUri(): void