mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
perf: Use channels for DNS verification
This commit is contained in:
1 parent
2e5d7a53eb
commit
bd1f9bb16f
1 file changed
+17
-24
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Appwrite\Network\Validator;
|
||||
|
||||
use Swoole\Coroutine\WaitGroup;
|
||||
use Swoole\Coroutine\Channel;
|
||||
use Utopia\DNS\Message\Record;
|
||||
use Utopia\DNS\Validator\DNS as BaseDNS;
|
||||
|
||||
@@ -32,34 +32,27 @@ class DNS extends BaseDNS
|
||||
*/
|
||||
public function isValid(mixed $value): bool
|
||||
{
|
||||
$wg = new WaitGroup();
|
||||
$failedValidator = null;
|
||||
$serverCount = \count($this->dnsServers);
|
||||
$channel = new Channel($serverCount);
|
||||
|
||||
foreach ($this->dnsServers as $dnsServer) {
|
||||
$wg->add();
|
||||
|
||||
\go(function () use ($value, $dnsServer, $wg, &$failedValidator) {
|
||||
try {
|
||||
$validator = new BaseDNS($this->target, $this->type, $dnsServer);
|
||||
$isValid = $validator->isValid($value);
|
||||
|
||||
if (!$isValid) {
|
||||
$failedValidator = $validator;
|
||||
}
|
||||
} finally {
|
||||
$wg->done();
|
||||
}
|
||||
\go(function () use ($value, $dnsServer, $channel) {
|
||||
$validator = new BaseDNS($this->target, $this->type, $dnsServer);
|
||||
$valid = $validator->isValid($value);
|
||||
$channel->push(['valid' => $valid, 'validator' => $validator]);
|
||||
});
|
||||
}
|
||||
|
||||
$wg->wait();
|
||||
|
||||
if (!\is_null($failedValidator)) {
|
||||
$this->count = $failedValidator->count;
|
||||
$this->value = $failedValidator->value;
|
||||
$this->reason = $failedValidator->reason;
|
||||
$this->records = $failedValidator->records;
|
||||
return false;
|
||||
for ($i = 0; $i < $serverCount; $i++) {
|
||||
$result = $channel->pop();
|
||||
if (!$result['valid']) {
|
||||
$failed = $result['validator'];
|
||||
$this->count = $failed->count;
|
||||
$this->value = $failed->value;
|
||||
$this->reason = $failed->reason;
|
||||
$this->records = $failed->records;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user