1
0
mirror of https://github.com/danog/dns.git synced 2025-01-22 13:31:12 +01:00

Awaitable → Promise

This commit is contained in:
Aaron Piotrowski 2016-11-15 10:42:15 -06:00
parent 429ca48c94
commit 0ff5c8b802
3 changed files with 27 additions and 30 deletions

View File

@ -2,15 +2,17 @@
namespace Amp\Dns;
use Amp\{ Coroutine, Deferred, Failure, MultiReasonException, Success, TimeoutException };
use Amp\{ CallableMaker, Coroutine, Deferred, Failure, MultiReasonException, Success, TimeoutException };
use Amp\Cache\ArrayCache;
use Amp\File\FilesystemException;
use Interop\Async\Awaitable;
use Interop\Async\Promise;
use LibDNS\{ Decoder\DecoderFactory, Encoder\EncoderFactory };
use LibDNS\Messages\{ MessageFactory, MessageTypes };
use LibDNS\Records\QuestionFactory;
class DefaultResolver implements Resolver {
use CallableMaker;
private $messageFactory;
private $questionFactory;
private $encoder;
@ -54,7 +56,7 @@ class DefaultResolver implements Resolver {
/**
* {@inheritdoc}
*/
public function resolve(string $name, array $options = []): Awaitable {
public function resolve(string $name, array $options = []): Promise {
if (!$inAddr = @\inet_pton($name)) {
if ($this->isValidHostName($name)) {
$types = empty($options["types"]) ? [Record::A, Record::AAAA] : (array) $options["types"];
@ -70,16 +72,16 @@ class DefaultResolver implements Resolver {
/**
* {@inheritdoc}
*/
public function query(string $name, $type, array $options = []): Awaitable {
public function query(string $name, $type, array $options = []): Promise {
$types = (array) $type;
if (empty($options["recurse"])) {
$awaitable = new Coroutine($this->doResolve($name, $types, $options));
$promise = new Coroutine($this->doResolve($name, $types, $options));
} else {
$awaitable = new Coroutine($this->doRecurse($name, $types, $options));
$promise = new Coroutine($this->doRecurse($name, $types, $options));
}
return $this->pipeResult($awaitable, $types);
return $this->pipeResult($promise, $types);
}
private function isValidHostName($name) {
@ -88,8 +90,8 @@ class DefaultResolver implements Resolver {
}
// flatten $result while preserving order according to $types (append unspecified types for e.g. Record::ALL queries)
private function pipeResult($awaitable, array $types) {
return \Amp\pipe($awaitable, function (array $result) use ($types) {
private function pipeResult($promise, array $types) {
return \Amp\pipe($promise, function (array $result) use ($types) {
$retval = [];
foreach ($types as $type) {
if (isset($result[$type])) {
@ -199,7 +201,7 @@ class DefaultResolver implements Resolver {
$server->pendingRequests[$requestId] = true;
$this->pendingRequests[$requestId] = [$deferred, $name, $type, $uri];
return $deferred->getAwaitable();
return $deferred->promise();
}
private function doResolve($name, array $types, $options) {
@ -250,13 +252,14 @@ class DefaultResolver implements Resolver {
} else {
$uri = $this->parseCustomServerUri($options["server"]);
}
$promises = [];
foreach ($types as $type) {
$awaitables[] = $this->doRequest($uri, $name, $type);
$promises[] = $this->doRequest($uri, $name, $type);
}
try {
list( , $resultArr) = yield \Amp\timeout(\Amp\some($awaitables), $timeout);
list( , $resultArr) = yield \Amp\timeout(\Amp\some($promises), $timeout);
foreach ($resultArr as $value) {
$result += $value;
}
@ -456,14 +459,14 @@ class DefaultResolver implements Resolver {
$server->buffer = "";
$server->length = INF;
$server->pendingRequests = [];
$server->watcherId = \Amp\onReadable($socket, $this->makePrivateCallable("onReadable"));
$server->watcherId = \Amp\onReadable($socket, $this->callableFromInstanceMethod("onReadable"));
\Amp\unreference($server->watcherId);
$this->serverIdMap[$id] = $server;
$this->serverUriMap[$uri] = $server;
if (\substr($uri, 0, 6) == "tcp://") {
$deferred = new Deferred;
$server->connect = $deferred->getAwaitable();
$server->connect = $deferred->promise();
$watcher = \Amp\onWritable($server->socket, static function($watcher) use ($server, $deferred, &$timer) {
\Amp\cancel($watcher);
\Amp\cancel($timer);
@ -620,12 +623,4 @@ class DefaultResolver implements Resolver {
$deferred->resolve($result);
}
}
private function makePrivateCallable($method): \Closure {
if (\PHP_VERSION_ID >= 70100) {
return \Closure::fromCallable([$this, $method]);
}
return (new \ReflectionClass($this))->getMethod($method)->getClosure($this);
}
}

View File

@ -2,14 +2,16 @@
namespace Amp\Dns;
use Interop\Async\Promise;
interface Resolver {
/**
* @see \Amp\Dns\resolve
*/
public function resolve(string $name, array $options = []);
public function resolve(string $name, array $options = []): Promise;
/**
* @see \Amp\Dns\query
*/
public function query(string $name, $type, array $options = []);
public function query(string $name, $type, array $options = []): Promise;
}

View File

@ -2,7 +2,7 @@
namespace Amp\Dns;
use Interop\Async\{ Awaitable, Loop };
use Interop\Async\{ Loop, Promise };
const LOOP_STATE_IDENTIFIER = Resolver::class;
@ -61,10 +61,10 @@ function driver(): Resolver {
*
* @param string $name The hostname to resolve
* @param array $options
* @return \Interop\Async\Awaitable
* @return \Interop\Async\Promise
* @TODO add boolean "clear_cache" option flag
*/
function resolve(string $name, array $options = []): Awaitable {
function resolve(string $name, array $options = []): Promise {
return resolver()->resolve($name, $options);
}
/**
@ -73,8 +73,8 @@ function resolve(string $name, array $options = []): Awaitable {
* @param string $name Unlike resolve(), query() allows for requesting _any_ name (as DNS RFC allows for arbitrary strings)
* @param int|int[] $type Use constants of Amp\Dns\Record
* @param array $options @see resolve documentation
* @return \Interop\Async\Awaitable
* @return \Interop\Async\Promise
*/
function query(string $name, $type, array $options = []): Awaitable {
function query(string $name, $type, array $options = []): Promise {
return resolver()->query($name, $type, $options);
}