简化函数
This commit is contained in:
parent
889de3185c
commit
21a1d85af7
@ -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.Diagnostics.CodeAnalysis;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
@ -32,7 +31,6 @@ namespace FastGithub.DomainResolve
|
|||||||
private readonly ILogger<DnsClient> logger;
|
private readonly ILogger<DnsClient> logger;
|
||||||
|
|
||||||
private readonly ConcurrentDictionary<string, IPAddressCollection> domainIPAddressCollection = new();
|
private readonly ConcurrentDictionary<string, IPAddressCollection> domainIPAddressCollection = new();
|
||||||
|
|
||||||
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 dnsCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
|
||||||
private readonly TimeSpan defaultEmptyTtl = TimeSpan.FromSeconds(30d);
|
private readonly TimeSpan defaultEmptyTtl = TimeSpan.FromSeconds(30d);
|
||||||
@ -64,9 +62,9 @@ namespace FastGithub.DomainResolve
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async IAsyncEnumerable<IPAddress> ResolveAsync(string domain, [EnumeratorCancellation] CancellationToken cancellationToken)
|
public async IAsyncEnumerable<IPAddress> ResolveAsync(string domain, [EnumeratorCancellation] CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (this.TryGetPingedIPAddresses(domain, out var addresses))
|
if (this.domainIPAddressCollection.TryGetValue(domain, out var collection) && collection.Count > 0)
|
||||||
{
|
{
|
||||||
foreach (var address in addresses)
|
foreach (var address in collection.ToArray())
|
||||||
{
|
{
|
||||||
yield return address;
|
yield return address;
|
||||||
}
|
}
|
||||||
@ -101,24 +99,6 @@ namespace FastGithub.DomainResolve
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 尝试获取域名下已经过ping排序的IP地址
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="domain"></param>
|
|
||||||
/// <param name="addresses"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private bool TryGetPingedIPAddresses(string domain, [MaybeNullWhen(false)] out IPAddress[] addresses)
|
|
||||||
{
|
|
||||||
if (this.domainIPAddressCollection.TryGetValue(domain, out var collection) && collection.Count > 0)
|
|
||||||
{
|
|
||||||
addresses = collection.ToArray();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
addresses = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 解析域名
|
/// 解析域名
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -190,7 +170,7 @@ namespace FastGithub.DomainResolve
|
|||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
this.logger.LogInformation($"dns://{dns}无法解析{domain}:请求超时");
|
this.logger.LogInformation($"dns://{dns}无法解析{domain}:请求超时");
|
||||||
return Array.Empty<IPAddress>();
|
return Array.Empty<IPAddress>();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user