取消Options的监听

This commit is contained in:
xljiulang 2021-07-21 20:30:12 +08:00
parent dfa6bc1367
commit 475048c621
2 changed files with 8 additions and 25 deletions

View File

@ -2,7 +2,6 @@
using DNS.Protocol;
using DNS.Protocol.ResourceRecords;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Net;
@ -16,8 +15,6 @@ namespace FastGithub.Dns
/// </summary>
sealed class RequestResolver : IRequestResolver
{
private IRequestResolver requestResolver;
private readonly TimeSpan ttl = TimeSpan.FromMinutes(1d);
private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<RequestResolver> logger;
@ -26,24 +23,13 @@ namespace FastGithub.Dns
/// dns解析者
/// </summary>
/// <param name="fastGithubConfig"></param>
/// <param name="options"></param>
/// <param name="logger"></param>
public RequestResolver(
FastGithubConfig fastGithubConfig,
IOptionsMonitor<FastGithubOptions> options,
ILogger<RequestResolver> logger)
{
this.fastGithubConfig = fastGithubConfig;
this.logger = logger;
this.requestResolver = new UdpRequestResolver(fastGithubConfig.FastDns);
options.OnChange(opt => OptionsChanged(opt));
void OptionsChanged(FastGithubOptions opt)
{
var dns = opt.FastDns.ToIPEndPoint();
this.requestResolver = new UdpRequestResolver(dns);
}
}
/// <summary>
@ -78,7 +64,8 @@ namespace FastGithub.Dns
return response;
}
return await this.requestResolver.Resolve(request, cancellationToken);
var fastResolver = new UdpRequestResolver(fastGithubConfig.FastDns);
return await fastResolver.Resolve(request, cancellationToken);
}
}
}

View File

@ -1,7 +1,6 @@
using DNS.Client;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Net;
@ -15,9 +14,8 @@ namespace FastGithub.ReverseProxy
/// </summary>
sealed class DomainResolver
{
private DnsClient dnsClient;
private readonly IMemoryCache memoryCache;
private readonly IOptionsMonitor<FastGithubOptions> options;
private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<DomainResolver> logger;
private readonly TimeSpan cacheTimeSpan = TimeSpan.FromSeconds(10d);
@ -25,19 +23,16 @@ namespace FastGithub.ReverseProxy
/// 域名解析器
/// </summary>
/// <param name="memoryCache"></param>
/// <param name="options"></param>
/// <param name="fastGithubConfig"></param>
/// <param name="logger"></param>
public DomainResolver(
IMemoryCache memoryCache,
IOptionsMonitor<FastGithubOptions> options,
FastGithubConfig fastGithubConfig,
ILogger<DomainResolver> logger)
{
this.memoryCache = memoryCache;
this.options = options;
this.fastGithubConfig = fastGithubConfig;
this.logger = logger;
this.dnsClient = new DnsClient(options.CurrentValue.PureDns.ToIPEndPoint());
options.OnChange(opt => this.dnsClient = new DnsClient(opt.PureDns.ToIPEndPoint()));
}
/// <summary>
@ -68,6 +63,7 @@ namespace FastGithub.ReverseProxy
{
try
{
var dnsClient = new DnsClient(this.fastGithubConfig.PureDns);
var addresses = await dnsClient.Lookup(domain, DNS.Protocol.RecordType.A, cancellationToken);
var address = addresses?.FirstOrDefault();
if (address == null)
@ -87,7 +83,7 @@ namespace FastGithub.ReverseProxy
}
catch (Exception ex)
{
var dns = this.options.CurrentValue.PureDns;
var dns = this.fastGithubConfig.PureDns;
throw new FastGithubException($"dns({dns})服务器异常:{ex.Message}", ex);
}
}