SelectiveTransportExecutor
in package
implements
ExecutorInterface
Send DNS queries over a UDP or TCP/IP stream transport.
This class will automatically choose the correct transport protocol to send a DNS query to your DNS server. It will always try to send it over the more efficient UDP transport first. If this query yields a size related issue (truncated messages), it will retry over a streaming TCP/IP transport.
For more advanced usages one can utilize this class directly.
The following example looks up the IPv6
address for reactphp.org
.
$executor = new SelectiveTransportExecutor($udpExecutor, $tcpExecutor);
$executor->query(
new Query($name, Message::TYPE_AAAA, Message::CLASS_IN)
)->then(function (Message $message) {
foreach ($message->answers as $answer) {
echo 'IPv6: ' . $answer->data . PHP_EOL;
}
}, 'printf');
Note that this executor only implements the logic to select the correct
transport for the given DNS query. Implementing the correct transport logic,
implementing timeouts and any retry logic is left up to the given executors,
see also UdpTransportExecutor
and
TcpTransportExecutor
for more details.
Note that this executor is entirely async and as such allows you to execute
any number of queries concurrently. You should probably limit the number of
concurrent queries in your application or you're very likely going to face
rate limitations and bans on the resolver end. For many common applications,
you may want to avoid sending the same query multiple times when the first
one is still pending, so you will likely want to use this in combination with
a CoopExecutor
like this:
$executor = new CoopExecutor(
new SelectiveTransportExecutor(
$datagramExecutor,
$streamExecutor
)
);
Table of Contents
Interfaces
Properties
- $datagramExecutor : mixed
- $streamExecutor : mixed
Methods
- __construct() : mixed
- query() : PromiseInterface<string|int, Message>
- Executes a query and will return a response message
Properties
$datagramExecutor
private
mixed
$datagramExecutor
$streamExecutor
private
mixed
$streamExecutor
Methods
__construct()
public
__construct(ExecutorInterface $datagramExecutor, ExecutorInterface $streamExecutor) : mixed
Parameters
- $datagramExecutor : ExecutorInterface
- $streamExecutor : ExecutorInterface
query()
Executes a query and will return a response message
public
query(Query $query) : PromiseInterface<string|int, Message>
It returns a Promise which either fulfills with a response
React\Dns\Model\Message
on success or rejects with an Exception
if
the query is not successful. A response message may indicate an error
condition in its rcode
, but this is considered a valid response message.
$executor->query($query)->then(
function (React\Dns\Model\Message $response) {
// response message successfully received
var_dump($response->rcode, $response->answers);
},
function (Exception $error) {
// failed to query due to $error
}
);
The returned Promise MUST be implemented in such a way that it can be cancelled when it is still pending. Cancelling a pending promise MUST reject its value with an Exception. It SHOULD clean up any underlying resources and references as applicable.
$promise = $executor->query($query);
$promise->cancel();
Parameters
- $query : Query