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