feat: delay for dns resolution

This commit is contained in:
Rekryt
2024-08-31 15:25:53 +03:00
parent 22cb1d3a8a
commit b6290fbe12
2 changed files with 17 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
COMPOSE_PROJECT_NAME="iplist" COMPOSE_PROJECT_NAME="iplist"
STORAGE_SAVE_INTERVAL="120" STORAGE_SAVE_INTERVAL="120"
SYS_MEMORY_LIMIT="512M" SYS_DNS_RESOLVE_DELAY="1000"
SYS_MEMORY_LIMIT="1024M"
SYS_TIMEZONE="Europe/Moscow" SYS_TIMEZONE="Europe/Moscow"
DEBUG="true" DEBUG="true"

View File

@@ -4,17 +4,23 @@ namespace OpenCCK\Domain\Helper;
use Amp\Dns\DnsConfig; use Amp\Dns\DnsConfig;
use Amp\Dns\DnsConfigLoader; use Amp\Dns\DnsConfigLoader;
use Amp\Dns\DnsException;
use Amp\Dns\DnsRecord; use Amp\Dns\DnsRecord;
use Amp\Dns\HostLoader; use Amp\Dns\HostLoader;
use Amp\Dns\Rfc1035StubDnsResolver; use Amp\Dns\Rfc1035StubDnsResolver;
use OpenCCK\Infrastructure\API\App; use OpenCCK\Infrastructure\API\App;
use Throwable;
use function Amp\delay;
use function Amp\Dns\dnsResolver; use function Amp\Dns\dnsResolver;
use function Amp\Dns\resolve; use function Amp\Dns\resolve;
readonly class DNSHelper { class DNSHelper {
private float $resolveDelay;
public function __construct(private array $dnsServers = []) { public function __construct(private array $dnsServers = []) {
$this->resolveDelay = (\OpenCCK\getEnv('SYS_DNS_RESOLVE_DELAY') ?? 500) / 1000;
} }
/** /**
@@ -45,23 +51,27 @@ readonly class DNSHelper {
$ipv4 = []; $ipv4 = [];
$ipv6 = []; $ipv6 = [];
foreach ($this->dnsServers as $server) { foreach ($this->dnsServers as $server) {
$this->setResolver([$server]);
try { try {
$this->setResolver([$server]);
$ipv4 = array_merge( $ipv4 = array_merge(
$ipv4, $ipv4,
array_map(fn(DnsRecord $record) => $record->getValue(), resolve($domain, DnsRecord::A)) array_map(fn(DnsRecord $record) => $record->getValue(), resolve($domain, DnsRecord::A))
); );
} catch (DnsException $e) { } catch (Throwable $e) {
App::getLogger()->error($e->getMessage(), [$server]); App::getLogger()->error($e->getMessage(), [$server]);
} }
delay($this->resolveDelay);
try { try {
$this->setResolver([$server]);
$ipv6 = array_merge( $ipv6 = array_merge(
$ipv6, $ipv6,
array_map(fn(DnsRecord $record) => $record->getValue(), resolve($domain, DnsRecord::AAAA)) array_map(fn(DnsRecord $record) => $record->getValue(), resolve($domain, DnsRecord::AAAA))
); );
} catch (DnsException $e) { } catch (Throwable $e) {
App::getLogger()->error($e->getMessage(), [$server]); App::getLogger()->error($e->getMessage(), [$server]);
} }
delay($this->resolveDelay);
} }
App::getLogger()->debug('resolve: ' . $domain, [count($ipv4), count($ipv6)]); App::getLogger()->debug('resolve: ' . $domain, [count($ipv4), count($ipv6)]);
return [$ipv4, $ipv6]; return [$ipv4, $ipv6];