只保留 socket异常缓存

This commit is contained in:
陈国伟 2021-11-24 15:50:46 +08:00
parent aba0ea7c2f
commit a5d79b40db
2 changed files with 6 additions and 6 deletions

View File

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

View File

@ -31,7 +31,7 @@ namespace FastGithub.DomainResolve
private readonly ILogger<DnsClient> logger; private readonly ILogger<DnsClient> logger;
private readonly ConcurrentDictionary<string, SemaphoreSlim> semaphoreSlims = new(); private readonly ConcurrentDictionary<string, SemaphoreSlim> semaphoreSlims = new();
private readonly IMemoryCache dnsCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); private readonly IMemoryCache dnsLookupCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
private readonly TimeSpan minTimeToLive = TimeSpan.FromSeconds(30d); private readonly TimeSpan minTimeToLive = TimeSpan.FromSeconds(30d);
private readonly TimeSpan maxTimeToLive = TimeSpan.FromMinutes(10d); private readonly TimeSpan maxTimeToLive = TimeSpan.FromMinutes(10d);
@ -118,13 +118,13 @@ namespace FastGithub.DomainResolve
try try
{ {
if (this.dnsCache.TryGetValue<IList<IPAddress>>(key, out var value)) if (this.dnsLookupCache.TryGetValue<IList<IPAddress>>(key, out var value))
{ {
return value; return value;
} }
var result = await this.LookupCoreAsync(dns, endPoint, fastSort, cancellationToken); var result = await this.LookupCoreAsync(dns, endPoint, fastSort, cancellationToken);
return this.dnsCache.Set(key, result.Addresses, result.TimeToLive); return this.dnsLookupCache.Set(key, result.Addresses, result.TimeToLive);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@ -133,12 +133,12 @@ namespace FastGithub.DomainResolve
catch (SocketException ex) catch (SocketException ex)
{ {
this.logger.LogWarning($"{endPoint.Host}@{dns}{ex.Message}"); this.logger.LogWarning($"{endPoint.Host}@{dns}{ex.Message}");
return this.dnsCache.Set(key, Array.Empty<IPAddress>(), this.minTimeToLive); return this.dnsLookupCache.Set(key, Array.Empty<IPAddress>(), this.minTimeToLive);
} }
catch (Exception ex) catch (Exception ex)
{ {
this.logger.LogWarning($"{endPoint.Host}@{dns}{ex.Message}"); this.logger.LogWarning($"{endPoint.Host}@{dns}{ex.Message}");
return this.dnsCache.Set(key, Array.Empty<IPAddress>(), this.minTimeToLive); return Array.Empty<IPAddress>();
} }
finally finally
{ {