Skip to content

Commit

Permalink
只保留 socket异常缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Nov 24, 2021
1 parent aba0ea7 commit a5d79b4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.1.0</Version>
<Version>2.1.1</Version>
<Nullable>enable</Nullable>
<TargetFramework>net6.0</TargetFramework>
<IsWebConfigTransformDisabled>true</IsWebConfigTransformDisabled>
Expand Down
10 changes: 5 additions & 5 deletions FastGithub.DomainResolve/DnsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sealed class DnsClient
private readonly ILogger<DnsClient> logger;

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 maxTimeToLive = TimeSpan.FromMinutes(10d);
Expand Down Expand Up @@ -118,13 +118,13 @@ private async Task<IList<IPAddress>> LookupAsync(IPEndPoint dns, DnsEndPoint end

try
{
if (this.dnsCache.TryGetValue<IList<IPAddress>>(key, out var value))
if (this.dnsLookupCache.TryGetValue<IList<IPAddress>>(key, out var value))
{
return value;
}

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)
{
Expand All @@ -133,12 +133,12 @@ private async Task<IList<IPAddress>> LookupAsync(IPEndPoint dns, DnsEndPoint end
catch (SocketException ex)
{
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)
{
this.logger.LogWarning($"{endPoint.Host}@{dns}{ex.Message}");
return this.dnsCache.Set(key, Array.Empty<IPAddress>(), this.minTimeToLive);
return Array.Empty<IPAddress>();
}
finally
{
Expand Down

0 comments on commit a5d79b4

Please sign in to comment.