Skip to content

Commit 91c0d6d

Browse files
committed
[Dns] added RetryExecutor
1 parent 9522ed8 commit 91c0d6d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

Query/RetryExecutor.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace React\Dns\Query;
4+
5+
class RetryExecutor implements ExecutorInterface
6+
{
7+
private $executor;
8+
private $cache;
9+
private $retries;
10+
11+
public function __construct(ExecutorInterface $executor, $retries = 2)
12+
{
13+
$this->executor = $executor;
14+
$this->retries = $retries;
15+
}
16+
17+
public function query($nameserver, Query $query, $callback, $errorback)
18+
{
19+
$this->tryQuery($nameserver, $query, $callback, $errorback, $this->retries);
20+
}
21+
22+
public function tryQuery($nameserver, Query $query, $callback, $errorback, $retries)
23+
{
24+
$that = $this;
25+
$errorback = function ($error) use ($nameserver, $query, $callback, $errorback, $retries, $that) {
26+
if (!$error instanceof TimeoutException) {
27+
$errorback($error);
28+
return;
29+
}
30+
if (0 >= $retries) {
31+
$error = new \RuntimeException(
32+
sprintf("DNS query for %s failed: too many retries", $query->name),
33+
0,
34+
$error
35+
);
36+
$errorback($error);
37+
return;
38+
}
39+
$that->tryQuery($nameserver, $query, $callback, $errorback, $retries-1);
40+
};
41+
$this->executor->query($nameserver, $query, $callback, $errorback);
42+
}
43+
}

Resolver/Factory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
use React\Dns\Protocol\Parser;
99
use React\Dns\Protocol\BinaryDumper;
1010
use React\EventLoop\LoopInterface;
11+
use React\Dns\Query\RetryExecutor;
1112

1213
class Factory
1314
{
1415
public function create($nameserver, LoopInterface $loop)
1516
{
1617
$nameserver = $this->addPortToServerIfMissing($nameserver);
17-
$executor = $this->createExecutor($loop);
18+
$executor = $this->createRetryExecutor($loop);
1819

1920
return new Resolver($nameserver, $executor);
2021
}
@@ -32,9 +33,14 @@ protected function createExecutor(LoopInterface $loop)
3233
return new Executor($loop, new Parser(), new BinaryDumper());
3334
}
3435

36+
protected function createRetryExecutor(LoopInterface $loop)
37+
{
38+
return new RetryExecutor($this->createExecutor($loop));
39+
}
40+
3541
protected function createCachedExecutor(LoopInterface $loop)
3642
{
37-
return new CachedExecutor($this->createExecutor($loop), new RecordCache());
43+
return new CachedExecutor($this->createRetryExecutor($loop), new RecordCache());
3844
}
3945

4046
protected function addPortToServerIfMissing($nameserver)

0 commit comments

Comments
 (0)