ping间隔5分钟
This commit is contained in:
parent
e212fdc5dc
commit
6fdcee67d2
@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// 域名解析后台服务
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -81,6 +81,11 @@ namespace FastGithub.DomainResolve
|
||||
[DebuggerDisplay("Address = {Address}, PingElapsed = {PingElapsed}")]
|
||||
private class IPAddressItem : IEquatable<IPAddressItem>
|
||||
{
|
||||
/// <summary>
|
||||
/// Ping的时间点
|
||||
/// </summary>
|
||||
private int? pingTicks;
|
||||
|
||||
/// <summary>
|
||||
/// 地址
|
||||
/// </summary>
|
||||
@ -99,13 +104,17 @@ namespace FastGithub.DomainResolve
|
||||
{
|
||||
this.Address = address;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发起ping请求
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user