合并dns查询结果

This commit is contained in:
xljiulang 2021-08-18 00:19:29 +08:00
parent df065089c3
commit f360e99033

View File

@ -23,7 +23,7 @@ namespace FastGithub.DomainResolve
private readonly FastGithubConfig fastGithubConfig; private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<DomainResolver> logger; private readonly ILogger<DomainResolver> logger;
private readonly TimeSpan lookupTimeout = TimeSpan.FromSeconds(2d); private readonly TimeSpan lookupTimeout = TimeSpan.FromSeconds(1d);
private readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(2d); private readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(2d);
private readonly TimeSpan resolveCacheTimeSpan = TimeSpan.FromMinutes(2d); private readonly TimeSpan resolveCacheTimeSpan = TimeSpan.FromMinutes(2d);
private readonly ConcurrentDictionary<DnsEndPoint, SemaphoreSlim> semaphoreSlims = new(); private readonly ConcurrentDictionary<DnsEndPoint, SemaphoreSlim> semaphoreSlims = new();
@ -77,18 +77,18 @@ 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 pureDns = this.fastGithubConfig.PureDns; var pureDnsTask = this.LookupCoreAsync(this.fastGithubConfig.PureDns, endPoint, cancellationToken);
var fastDns = this.fastGithubConfig.FastDns; var fastDnsTask = this.LookupCoreAsync(this.fastGithubConfig.FastDns, endPoint, cancellationToken);
try var addresses = await Task.WhenAll(pureDnsTask, fastDnsTask);
var fastAddress = await this.GetFastIPAddressAsync(addresses.SelectMany(item => item), endPoint.Port, cancellationToken);
if (fastAddress != null)
{ {
return await LookupCoreAsync(pureDns, endPoint, cancellationToken); this.logger.LogInformation($"[{endPoint.Host}->{fastAddress}]");
} return fastAddress;
catch (Exception)
{
this.logger.LogWarning($"由于{pureDns}解析{endPoint.Host}失败,本次使用{fastDns}");
return await LookupCoreAsync(fastDns, endPoint, cancellationToken);
} }
throw new FastGithubException($"解析不到{endPoint.Host}可用的ip");
} }
/// <summary> /// <summary>
@ -98,21 +98,20 @@ namespace FastGithub.DomainResolve
/// <param name="endPoint"></param> /// <param name="endPoint"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
private async Task<IPAddress> LookupCoreAsync(IPEndPoint dns, DnsEndPoint endPoint, CancellationToken cancellationToken) private async Task<IEnumerable<IPAddress>> LookupCoreAsync(IPEndPoint dns, DnsEndPoint endPoint, CancellationToken cancellationToken)
{ {
var dnsClient = new DnsClient(dns); try
using var timeoutTokenSource = new CancellationTokenSource(this.lookupTimeout);
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
var addresses = await dnsClient.Lookup(endPoint.Host, RecordType.A, linkedTokenSource.Token);
var fastAddress = await this.GetFastIPAddressAsync(addresses, endPoint.Port, cancellationToken);
if (fastAddress != null)
{ {
this.logger.LogInformation($"[{endPoint.Host}->{fastAddress}]"); var dnsClient = new DnsClient(dns);
return fastAddress; using var timeoutTokenSource = new CancellationTokenSource(this.lookupTimeout);
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
return await dnsClient.Lookup(endPoint.Host, RecordType.A, linkedTokenSource.Token);
}
catch
{
this.logger.LogWarning($"dns({dns})无法解析{endPoint.Host}");
return Enumerable.Empty<IPAddress>();
} }
throw new FastGithubException($"dns{dns}解析不到{endPoint.Host}可用的ip");
} }
/// <summary> /// <summary>