perf: Use channels for DNS verification

This commit is contained in:
Hemachandar committed 2026-03-24 10:35:16 +05:30
1 parent 2e5d7a53eb
commit bd1f9bb16f
1 file changed
+17 -24
+17 -24
View File
@@ -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;