Skip to content

Commit

Permalink
network异常
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Nov 26, 2021
1 parent f5e7e4c commit 59fde28
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions FastGithub.DomainResolve/IPAddressService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ private record DomainAddress(string Domain, IPAddress Address);
private readonly IMemoryCache domainAddressCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));

private record AddressElapsed(IPAddress Address, TimeSpan Elapsed);
private readonly TimeSpan brokeElapsedExpiration = TimeSpan.FromMinutes(1d);
private readonly TimeSpan normaleElapsedExpiration = TimeSpan.FromMinutes(5d);
private readonly TimeSpan problemElapsedExpiration = TimeSpan.FromMinutes(1d);
private readonly TimeSpan normalElapsedExpiration = TimeSpan.FromMinutes(5d);
private readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(5d);
private readonly IMemoryCache addressElapsedCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));

Expand Down Expand Up @@ -107,14 +107,14 @@ private async Task<AddressElapsed> GetAddressElapsedAsync(IPEndPoint endPoint, C
await socket.ConnectAsync(endPoint, linkedTokenSource.Token);

addressElapsed = new AddressElapsed(endPoint.Address, stopWatch.Elapsed);
return this.addressElapsedCache.Set(endPoint, addressElapsed, this.normaleElapsedExpiration);
return this.addressElapsedCache.Set(endPoint, addressElapsed, this.normalElapsedExpiration);
}
catch (Exception ex)
{
cancellationToken.ThrowIfCancellationRequested();

addressElapsed = new AddressElapsed(endPoint.Address, TimeSpan.MaxValue);
var expiration = IsLocalNetworkProblem(ex) ? this.brokeElapsedExpiration : this.normaleElapsedExpiration;
var expiration = IsLocalNetworkProblem(ex) ? this.problemElapsedExpiration : this.normalElapsedExpiration;
return this.addressElapsedCache.Set(endPoint, addressElapsed, expiration);
}
finally
Expand All @@ -136,9 +136,7 @@ private static bool IsLocalNetworkProblem(Exception ex)
}

var code = socketException.SocketErrorCode;
return code == SocketError.NetworkDown ||
code == SocketError.NetworkUnreachable ||
code == SocketError.HostUnreachable;
return code == SocketError.NetworkDown || code == SocketError.NetworkUnreachable;
}
}
}

0 comments on commit 59fde28

Please sign in to comment.