From 795970c1b13aa095ca0836a98d836aabbac4b470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Sat, 20 Nov 2021 11:27:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=9F=E5=90=8DIP=E5=85=B3=E7=B3=BB=E7=BC=93?= =?UTF-8?q?=E5=AD=9810=E5=88=86=E9=92=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.DomainResolve/IPAddressService.cs | 21 ++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/FastGithub.DomainResolve/IPAddressService.cs b/FastGithub.DomainResolve/IPAddressService.cs index 28f3671..25e7ce2 100644 --- a/FastGithub.DomainResolve/IPAddressService.cs +++ b/FastGithub.DomainResolve/IPAddressService.cs @@ -13,26 +13,27 @@ using System.Threading.Tasks; namespace FastGithub.DomainResolve { /// - /// IP状态服务 - /// 状态缓存5分钟 - /// 连接超时5秒 + /// IP服务 + /// 域名IP关系缓存10分钟 + /// IPEndPoint时延缓存5分钟 + /// IPEndPoint连接超时5秒 /// sealed class IPAddressService { private record DomainAddress(string Domain, IPAddress Address); - private readonly TimeSpan domainExpiration = TimeSpan.FromMinutes(5d); + private readonly TimeSpan domainAddressExpiration = TimeSpan.FromMinutes(10d); private readonly IMemoryCache domainAddressCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); private record AddressElapsed(IPAddress Address, TimeSpan Elapsed); - private readonly TimeSpan brokeExpiration = TimeSpan.FromMinutes(1d); - private readonly TimeSpan normalExpiration = TimeSpan.FromMinutes(5d); + private readonly TimeSpan brokeElapsedExpiration = TimeSpan.FromMinutes(1d); + private readonly TimeSpan normaleElapsedExpiration = TimeSpan.FromMinutes(5d); private readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(5d); private readonly IMemoryCache addressElapsedCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); private readonly DnsClient dnsClient; /// - /// IP状态服务 + /// IP服务 /// /// public IPAddressService(DnsClient dnsClient) @@ -66,7 +67,7 @@ namespace FastGithub.DomainResolve { ipEndPoints.Add(new IPEndPoint(address, dnsEndPoint.Port)); var domainAddress = new DomainAddress(dnsEndPoint.Host, address); - this.domainAddressCache.Set(domainAddress, default(object), this.domainExpiration); + this.domainAddressCache.Set(domainAddress, default(object), this.domainAddressExpiration); } if (ipEndPoints.Count == 0) @@ -107,14 +108,14 @@ namespace FastGithub.DomainResolve await socket.ConnectAsync(endPoint, linkedTokenSource.Token); addressElapsed = new AddressElapsed(endPoint.Address, stopWatch.Elapsed); - return this.addressElapsedCache.Set(endPoint, addressElapsed, this.normalExpiration); + return this.addressElapsedCache.Set(endPoint, addressElapsed, this.normaleElapsedExpiration); } catch (Exception) { cancellationToken.ThrowIfCancellationRequested(); addressElapsed = new AddressElapsed(endPoint.Address, TimeSpan.MaxValue); - var expiration = NetworkInterface.GetIsNetworkAvailable() ? this.normalExpiration : this.brokeExpiration; + var expiration = NetworkInterface.GetIsNetworkAvailable() ? this.normaleElapsedExpiration : this.brokeElapsedExpiration; return this.addressElapsedCache.Set(endPoint, addressElapsed, expiration); } finally