Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Aug 30, 2021
1 parent b4ff928 commit d655a5a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions FastGithub.DomainResolve/DomainResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ sealed class DomainResolver : IDomainResolver
private readonly ILogger<DomainResolver> logger;

private readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(5d);
private readonly TimeSpan disableIPExpiration = TimeSpan.FromMinutes(2d);

private readonly TimeSpan dnscryptExpiration = TimeSpan.FromMinutes(10d);
private readonly TimeSpan fallbackExpiration = TimeSpan.FromMinutes(2d);
private readonly TimeSpan loopbackExpiration = TimeSpan.FromSeconds(5d);
Expand Down Expand Up @@ -133,7 +135,6 @@ public async Task<IPAddress> ResolveAsync(DnsEndPoint domain, CancellationToken
expiration = this.loopbackExpiration;
}

this.logger.LogInformation($"[{domain.Host}->{address}]");
this.domainResolveCache.Set(domain, address, expiration);
return address;
}
Expand Down Expand Up @@ -171,7 +172,16 @@ public async Task<IPAddress> ResolveAsync(DnsEndPoint domain, CancellationToken
var dnsClient = new DnsClient(dns);
var addresses = await dnsClient.Lookup(domain.Host, RecordType.A, cancellationToken);
addresses = addresses.Where(address => this.disableIPAddressCache.TryGetValue(address, out _) == false).ToList();
return await this.FindFastValueAsync(addresses, domain.Port, cancellationToken);
var address = await this.FindFastValueAsync(addresses, domain.Port, cancellationToken);
if (address == null)
{
this.logger.LogWarning($"dns({dns})解析不到{domain.Host}可用的ip解析");
}
else
{
this.logger.LogInformation($"dns({dns}): [{domain.Host}->{address}]");
}
return address;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -224,12 +234,12 @@ public async Task<IPAddress> ResolveAsync(DnsEndPoint domain, CancellationToken
}
catch (OperationCanceledException)
{
this.SetDisabled(address, TimeSpan.FromMilliseconds(2d));
this.SetDisabled(address, this.disableIPExpiration);
return default;
}
catch (Exception)
{
this.SetDisabled(address, TimeSpan.FromMilliseconds(1d));
this.SetDisabled(address, this.disableIPExpiration);
await Task.Delay(this.connectTimeout, cancellationToken);
return default;
}
Expand Down

0 comments on commit d655a5a

Please sign in to comment.