From e790ab513ae8894eccae4c52800ec9d5c052df75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Mon, 29 Nov 2021 14:38:32 +0800 Subject: [PATCH] =?UTF-8?q?dns=E8=A7=A6=E5=8F=91tcp=20reset=E5=90=8E?= =?UTF-8?q?=E7=BC=93=E5=AD=9810=E5=88=86=E9=92=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Build.props | 2 +- FastGithub.DomainResolve/DnsClient.cs | 32 +++++++++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index d1c9920..3b895e7 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 2.1.1 + 2.1.2 enable net6.0 true diff --git a/FastGithub.DomainResolve/DnsClient.cs b/FastGithub.DomainResolve/DnsClient.cs index 54911f1..cf928e3 100644 --- a/FastGithub.DomainResolve/DnsClient.cs +++ b/FastGithub.DomainResolve/DnsClient.cs @@ -9,7 +9,6 @@ using Microsoft.Extensions.Options; using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; @@ -34,7 +33,7 @@ namespace FastGithub.DomainResolve private readonly ConcurrentDictionary semaphoreSlims = new(); private readonly IMemoryCache dnsLookupCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); - private readonly TimeSpan minTimeToLive = TimeSpan.FromMinutes(1d); + private readonly TimeSpan minTimeToLive = TimeSpan.FromSeconds(30d); private readonly TimeSpan maxTimeToLive = TimeSpan.FromMinutes(10d); private readonly int resolveTimeout = (int)TimeSpan.FromSeconds(4d).TotalMilliseconds; @@ -123,7 +122,6 @@ namespace FastGithub.DomainResolve { return value; } - var result = await this.LookupCoreAsync(dns, endPoint, fastSort, cancellationToken); return this.dnsLookupCache.Set(key, result.Addresses, result.TimeToLive); } @@ -131,15 +129,11 @@ namespace FastGithub.DomainResolve { return Array.Empty(); } - catch (IOException ex) when (ex.InnerException is SocketException) - { - this.logger.LogWarning($"{endPoint.Host}@{dns}->{ex.Message}"); - return this.dnsLookupCache.Set(key, Array.Empty(), this.maxTimeToLive); - } catch (Exception ex) { this.logger.LogWarning($"{endPoint.Host}@{dns}->{ex.Message}"); - return Array.Empty(); + var expiration = IsTcpResetException(ex) ? this.maxTimeToLive : this.minTimeToLive; + return this.dnsLookupCache.Set(key, Array.Empty(), expiration); } finally { @@ -147,6 +141,26 @@ namespace FastGithub.DomainResolve } } + /// + /// 是否为因收到tcp reset导致的关闭 + /// + /// + /// + private static bool IsTcpResetException(Exception ex) + { + if (ex is SocketException socketException) + { + if (socketException.SocketErrorCode == SocketError.ConnectionReset) + { + return true; + } + } + + var inner = ex.InnerException; + return inner != null && IsTcpResetException(inner); + } + + /// /// 解析域名 ///