From 0d594de9d72d26c18023ffc5df7b4c145b5a3124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 12 Sep 2025 15:58:39 +0200 Subject: [PATCH] Add concurrency to dns validator --- src/Appwrite/Network/Validator/DNS.php | 48 +++++++++++++++++++++----- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/src/Appwrite/Network/Validator/DNS.php b/src/Appwrite/Network/Validator/DNS.php index 93e633485f..4ef6c37b1a 100644 --- a/src/Appwrite/Network/Validator/DNS.php +++ b/src/Appwrite/Network/Validator/DNS.php @@ -2,6 +2,7 @@ namespace Appwrite\Network\Validator; +use Swoole\Coroutine\WaitGroup; use Utopia\DNS\Client; use Utopia\Domains\Domain; use Utopia\System\System; @@ -28,11 +29,11 @@ class DNS extends Validator */ protected array $dnsServers = []; - protected string $domain = ''; - protected string $resolver = ''; - protected array $recordValues = []; - protected int $count = 0; - protected string $reason = ''; + public string $domain = ''; + public string $resolver = ''; + public array $recordValues = []; + public int $count = 0; + public string $reason = ''; /** * @param string $target @@ -112,11 +113,40 @@ class DNS extends Validator */ public function isValid(mixed $value): bool { + // If single server, query it + if (\count($this->dnsServers) === 1) { + return $this->isValidWithDNSServer($value, $this->dnsServers[0]); + } + + // If multiple servers, concurrently query them + $wg = new WaitGroup(); + $failedValidator = null; foreach ($this->dnsServers as $dnsServer) { - $isValid = $this->isValidWithDNSServer($value, $dnsServer); - if (!$isValid) { - return false; - } + $wg->add(); + + \go(function () use ($value, $dnsServer, $wg, &$failedValidator) { + try { + $validator = new DNS($this->target, $this->type, $dnsServer); + $isValid = $validator->isValid($value); + if (!$isValid) { + $failedValidator = $validator; + } + } finally { + $wg->done(); + } + }); + } + + $wg->wait(); + + if (!\is_null($failedValidator)) { + $this->count = $failedValidator->count; + $this->domain = $failedValidator->domain; + $this->reason = $failedValidator->reason; + $this->recordValues = $failedValidator->recordValues; + $this->resolver = $failedValidator->resolver; + + return false; } return true;