diff --git a/FastGithub.DomainResolve/DomainResolveHostedService.cs b/FastGithub.DomainResolve/DomainResolveHostedService.cs index 434298d..b15982e 100644 --- a/FastGithub.DomainResolve/DomainResolveHostedService.cs +++ b/FastGithub.DomainResolve/DomainResolveHostedService.cs @@ -12,7 +12,7 @@ namespace FastGithub.DomainResolve { private readonly DnscryptProxy dnscryptProxy; private readonly DnsClient dnsClient; - private readonly TimeSpan speedTestTimeSpan = TimeSpan.FromMinutes(2d); + private readonly TimeSpan pingPeriodTimeSpan = TimeSpan.FromSeconds(30d); /// /// 域名解析后台服务 @@ -38,7 +38,7 @@ namespace FastGithub.DomainResolve while (stoppingToken.IsCancellationRequested == false) { await this.dnsClient.PingAllDomainsAsync(stoppingToken); - await Task.Delay(this.speedTestTimeSpan, stoppingToken); + await Task.Delay(this.pingPeriodTimeSpan, stoppingToken); } } diff --git a/FastGithub.DomainResolve/IPAddressCollection.cs b/FastGithub.DomainResolve/IPAddressCollection.cs index 56ce21f..2fc1122 100644 --- a/FastGithub.DomainResolve/IPAddressCollection.cs +++ b/FastGithub.DomainResolve/IPAddressCollection.cs @@ -81,6 +81,11 @@ namespace FastGithub.DomainResolve [DebuggerDisplay("Address = {Address}, PingElapsed = {PingElapsed}")] private class IPAddressItem : IEquatable { + /// + /// Ping的时间点 + /// + private int? pingTicks; + /// /// 地址 /// @@ -99,13 +104,17 @@ namespace FastGithub.DomainResolve { this.Address = address; } - /// /// 发起ping请求 /// /// public async Task PingAsync() { + if (this.NeedToPing() == false) + { + return; + } + try { using var ping = new Ping(); @@ -118,6 +127,27 @@ namespace FastGithub.DomainResolve { this.PingElapsed = TimeSpan.MaxValue; } + finally + { + this.pingTicks = Environment.TickCount; + } + } + + /// + /// 是否需要ping + /// 5分钟内只ping一次 + /// + /// + private bool NeedToPing() + { + var ticks = this.pingTicks; + if (ticks == null) + { + return true; + } + + var pingTimeSpan = TimeSpan.FromMilliseconds(Environment.TickCount - ticks.Value); + return pingTimeSpan > TimeSpan.FromMinutes(5d); } public bool Equals(IPAddressItem? other)