ping间隔5分钟

This commit is contained in:
陈国伟 2021-09-29 13:09:48 +08:00
parent e212fdc5dc
commit 6fdcee67d2
2 changed files with 33 additions and 3 deletions

View File

@ -12,7 +12,7 @@ namespace FastGithub.DomainResolve
{ {
private readonly DnscryptProxy dnscryptProxy; private readonly DnscryptProxy dnscryptProxy;
private readonly DnsClient dnsClient; private readonly DnsClient dnsClient;
private readonly TimeSpan speedTestTimeSpan = TimeSpan.FromMinutes(2d); private readonly TimeSpan pingPeriodTimeSpan = TimeSpan.FromSeconds(30d);
/// <summary> /// <summary>
/// 域名解析后台服务 /// 域名解析后台服务
@ -38,7 +38,7 @@ namespace FastGithub.DomainResolve
while (stoppingToken.IsCancellationRequested == false) while (stoppingToken.IsCancellationRequested == false)
{ {
await this.dnsClient.PingAllDomainsAsync(stoppingToken); await this.dnsClient.PingAllDomainsAsync(stoppingToken);
await Task.Delay(this.speedTestTimeSpan, stoppingToken); await Task.Delay(this.pingPeriodTimeSpan, stoppingToken);
} }
} }

View File

@ -81,6 +81,11 @@ namespace FastGithub.DomainResolve
[DebuggerDisplay("Address = {Address}, PingElapsed = {PingElapsed}")] [DebuggerDisplay("Address = {Address}, PingElapsed = {PingElapsed}")]
private class IPAddressItem : IEquatable<IPAddressItem> private class IPAddressItem : IEquatable<IPAddressItem>
{ {
/// <summary>
/// Ping的时间点
/// </summary>
private int? pingTicks;
/// <summary> /// <summary>
/// 地址 /// 地址
/// </summary> /// </summary>
@ -99,13 +104,17 @@ namespace FastGithub.DomainResolve
{ {
this.Address = address; this.Address = address;
} }
/// <summary> /// <summary>
/// 发起ping请求 /// 发起ping请求
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public async Task PingAsync() public async Task PingAsync()
{ {
if (this.NeedToPing() == false)
{
return;
}
try try
{ {
using var ping = new Ping(); using var ping = new Ping();
@ -118,6 +127,27 @@ namespace FastGithub.DomainResolve
{ {
this.PingElapsed = TimeSpan.MaxValue; this.PingElapsed = TimeSpan.MaxValue;
} }
finally
{
this.pingTicks = Environment.TickCount;
}
}
/// <summary>
/// 是否需要ping
/// 5分钟内只ping一次
/// </summary>
/// <returns></returns>
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) public bool Equals(IPAddressItem? other)