dns触发tcp reset后缓存10分钟

This commit is contained in:
陈国伟 2021-11-29 14:38:32 +08:00
parent 94e52b0729
commit e790ab513a
2 changed files with 24 additions and 10 deletions

View File

@ -1,6 +1,6 @@
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<Version>2.1.1</Version> <Version>2.1.2</Version>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<IsWebConfigTransformDisabled>true</IsWebConfigTransformDisabled> <IsWebConfigTransformDisabled>true</IsWebConfigTransformDisabled>

View File

@ -9,7 +9,6 @@ using Microsoft.Extensions.Options;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -34,7 +33,7 @@ namespace FastGithub.DomainResolve
private readonly ConcurrentDictionary<string, SemaphoreSlim> semaphoreSlims = new(); private readonly ConcurrentDictionary<string, SemaphoreSlim> semaphoreSlims = new();
private readonly IMemoryCache dnsLookupCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); 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 TimeSpan maxTimeToLive = TimeSpan.FromMinutes(10d);
private readonly int resolveTimeout = (int)TimeSpan.FromSeconds(4d).TotalMilliseconds; private readonly int resolveTimeout = (int)TimeSpan.FromSeconds(4d).TotalMilliseconds;
@ -123,7 +122,6 @@ namespace FastGithub.DomainResolve
{ {
return value; return value;
} }
var result = await this.LookupCoreAsync(dns, endPoint, fastSort, cancellationToken); var result = await this.LookupCoreAsync(dns, endPoint, fastSort, cancellationToken);
return this.dnsLookupCache.Set(key, result.Addresses, result.TimeToLive); return this.dnsLookupCache.Set(key, result.Addresses, result.TimeToLive);
} }
@ -131,15 +129,11 @@ namespace FastGithub.DomainResolve
{ {
return Array.Empty<IPAddress>(); 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) catch (Exception ex)
{ {
this.logger.LogWarning($"{endPoint.Host}@{dns}->{ex.Message}"); 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 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>
/// 解析域名 /// 解析域名
/// </summary> /// </summary>