|
@@ -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<string, SemaphoreSlim> 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<IPAddress>();
|
|
|
}
|
|
|
- catch (IOException ex) when (ex.InnerException is SocketException)
|
|
|
- {
|
|
|
- this.logger.LogWarning($"{endPoint.Host}@{dns}->{ex.Message}");
|
|
|
- return this.dnsLookupCache.Set(key, Array.Empty<IPAddress>(), this.maxTimeToLive);
|
|
|
- }
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
this.logger.LogWarning($"{endPoint.Host}@{dns}->{ex.Message}");
|
|
|
- return Array.Empty<IPAddress>();
|
|
|
+ var expiration = IsTcpResetException(ex) ? this.maxTimeToLive : this.minTimeToLive;
|
|
|
+ return this.dnsLookupCache.Set(key, Array.Empty<IPAddress>(), expiration);
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
@@ -147,6 +141,26 @@ namespace FastGithub.DomainResolve
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 是否为因收到tcp reset导致的关闭
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="ex"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 解析域名
|
|
|
/// </summary>
|