PureDns优先

This commit is contained in:
xljiulang 2021-08-18 01:13:02 +08:00
parent f360e99033
commit 8143f40243

View File

@ -77,17 +77,19 @@ namespace FastGithub.DomainResolve
/// <returns></returns> /// <returns></returns>
private async Task<IPAddress> LookupAsync(DnsEndPoint endPoint, CancellationToken cancellationToken) private async Task<IPAddress> LookupAsync(DnsEndPoint endPoint, CancellationToken cancellationToken)
{ {
var pureDnsTask = this.LookupCoreAsync(this.fastGithubConfig.PureDns, endPoint, cancellationToken); var dnsEndPoints = new[] { this.fastGithubConfig.PureDns, this.fastGithubConfig.FastDns };
var fastDnsTask = this.LookupCoreAsync(this.fastGithubConfig.FastDns, endPoint, cancellationToken); foreach (var dns in dnsEndPoints)
{
var addresses = await Task.WhenAll(pureDnsTask, fastDnsTask); var addresses = await this.LookupCoreAsync(dns, endPoint, cancellationToken);
var fastAddress = await this.GetFastIPAddressAsync(addresses.SelectMany(item => item), endPoint.Port, cancellationToken); var fastAddress = await this.GetFastIPAddressAsync(addresses, endPoint.Port, cancellationToken);
if (fastAddress != null) if (fastAddress != null)
{ {
this.logger.LogInformation($"[{endPoint.Host}->{fastAddress}]"); this.logger.LogInformation($"[{endPoint.Host}->{fastAddress}]");
return fastAddress; return fastAddress;
} }
}
throw new FastGithubException($"解析不到{endPoint.Host}可用的ip"); throw new FastGithubException($"解析不到{endPoint.Host}可用的ip");
} }
@ -123,6 +125,11 @@ namespace FastGithub.DomainResolve
/// <returns></returns> /// <returns></returns>
private async Task<IPAddress?> GetFastIPAddressAsync(IEnumerable<IPAddress> addresses, int port, CancellationToken cancellationToken) private async Task<IPAddress?> GetFastIPAddressAsync(IEnumerable<IPAddress> addresses, int port, CancellationToken cancellationToken)
{ {
if (addresses.Any() == false)
{
return default;
}
var tasks = addresses.Select(address => this.IsAvailableAsync(address, port, cancellationToken)); var tasks = addresses.Select(address => this.IsAvailableAsync(address, port, cancellationToken));
var fastTask = await Task.WhenAny(tasks); var fastTask = await Task.WhenAny(tasks);
return await fastTask; return await fastTask;