diff --git a/Directory.Build.props b/Directory.Build.props index 03c3396..aec40f3 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 2.0.8 + 2.0.9 enable net6.0 true diff --git a/FastGithub.DomainResolve/DnsClient.cs b/FastGithub.DomainResolve/DnsClient.cs index 3e4c7a7..2d87973 100644 --- a/FastGithub.DomainResolve/DnsClient.cs +++ b/FastGithub.DomainResolve/DnsClient.cs @@ -61,14 +61,15 @@ namespace FastGithub.DomainResolve /// 解析域名 /// /// 远程结节 + /// 是否使用快速排序 /// /// - public async IAsyncEnumerable ResolveAsync(DnsEndPoint endPoint, [EnumeratorCancellation] CancellationToken cancellationToken) + public async IAsyncEnumerable ResolveAsync(DnsEndPoint endPoint, bool fastSort, [EnumeratorCancellation] CancellationToken cancellationToken) { var hashSet = new HashSet(); foreach (var dns in this.GetDnsServers()) { - var addresses = await this.LookupAsync(dns, endPoint, cancellationToken); + var addresses = await this.LookupAsync(dns, endPoint, fastSort, cancellationToken); foreach (var address in addresses) { if (hashSet.Add(address) == true) @@ -106,9 +107,10 @@ namespace FastGithub.DomainResolve /// /// /// + /// /// /// - private async Task LookupAsync(IPEndPoint dns, DnsEndPoint endPoint, CancellationToken cancellationToken = default) + private async Task LookupAsync(IPEndPoint dns, DnsEndPoint endPoint, bool fastSort, CancellationToken cancellationToken = default) { var key = $"{dns}/{endPoint}"; var semaphore = this.semaphoreSlims.GetOrAdd(key, _ => new SemaphoreSlim(1, 1)); @@ -121,7 +123,7 @@ namespace FastGithub.DomainResolve return value; } - var result = await this.LookupCoreAsync(dns, endPoint, cancellationToken); + var result = await this.LookupCoreAsync(dns, endPoint, fastSort, cancellationToken); this.dnsCache.Set(key, result.Addresses, result.TimeToLive); return result.Addresses; } @@ -146,9 +148,10 @@ namespace FastGithub.DomainResolve /// /// /// + /// /// /// - private async Task LookupCoreAsync(IPEndPoint dns, DnsEndPoint endPoint, CancellationToken cancellationToken = default) + private async Task LookupCoreAsync(IPEndPoint dns, DnsEndPoint endPoint, bool fastSort, CancellationToken cancellationToken = default) { if (endPoint.Host == LOCALHOST) { @@ -170,7 +173,7 @@ namespace FastGithub.DomainResolve return new LookupResult(addresses, this.minTimeToLive); } - if (addresses.Length > 1) + if (fastSort && addresses.Length > 1) { addresses = await OrderByConnectAnyAsync(addresses, endPoint.Port, cancellationToken); } @@ -237,8 +240,13 @@ namespace FastGithub.DomainResolve /// private static async Task OrderByConnectAnyAsync(IPAddress[] addresses, int port, CancellationToken cancellationToken) { - var tasks = addresses.Select(address => ConnectAsync(address, port, cancellationToken)); - var fastestAddress = await await Task.WhenAny(tasks); + using var controlTokenSource = new CancellationTokenSource(connectTimeout); + using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, controlTokenSource.Token); + + var connectTasks = addresses.Select(address => ConnectAsync(address, port, linkedTokenSource.Token)); + var fastestAddress = await await Task.WhenAny(connectTasks); + controlTokenSource.Cancel(); + if (fastestAddress == null) { return addresses; @@ -266,10 +274,8 @@ namespace FastGithub.DomainResolve { try { - using var timeoutTokenSource = new CancellationTokenSource(connectTimeout); - using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token); using var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); - await socket.ConnectAsync(address, port, linkedTokenSource.Token); + await socket.ConnectAsync(address, port, cancellationToken); return address; } catch (Exception) diff --git a/FastGithub.DomainResolve/DomainResolver.cs b/FastGithub.DomainResolve/DomainResolver.cs index 2c66b5b..ea8313a 100644 --- a/FastGithub.DomainResolve/DomainResolver.cs +++ b/FastGithub.DomainResolve/DomainResolver.cs @@ -65,7 +65,7 @@ namespace FastGithub.DomainResolve else { this.dnsEndPointAddressElapseds.TryAdd(endPoint, IPAddressElapsedCollection.Empty); - await foreach (var adddress in this.dnsClient.ResolveAsync(endPoint, cancellationToken)) + await foreach (var adddress in this.dnsClient.ResolveAsync(endPoint, fastSort: true, cancellationToken)) { this.logger.LogInformation($"{endPoint.Host}->{adddress}"); yield return adddress; @@ -86,7 +86,7 @@ namespace FastGithub.DomainResolve { var dnsEndPoint = keyValue.Key; var addresses = new List(); - await foreach (var adddress in this.dnsClient.ResolveAsync(dnsEndPoint, cancellationToken)) + await foreach (var adddress in this.dnsClient.ResolveAsync(dnsEndPoint, fastSort: false, cancellationToken)) { addresses.Add(adddress); }