From 65a038a3a5517e2f3002102c2193c7332c9ed692 Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Wed, 21 Jul 2021 21:03:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96FastGithubConfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Core/FastGithub.Core.csproj | 1 + FastGithub.Core/FastGithubConfig.cs | 71 ++++++++++++++++---------- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/FastGithub.Core/FastGithub.Core.csproj b/FastGithub.Core/FastGithub.Core.csproj index 7075b28..34da548 100644 --- a/FastGithub.Core/FastGithub.Core.csproj +++ b/FastGithub.Core/FastGithub.Core.csproj @@ -6,6 +6,7 @@ + diff --git a/FastGithub.Core/FastGithubConfig.cs b/FastGithub.Core/FastGithubConfig.cs index af47a19..f0413b3 100644 --- a/FastGithub.Core/FastGithubConfig.cs +++ b/FastGithub.Core/FastGithubConfig.cs @@ -1,4 +1,6 @@ -using Microsoft.Extensions.Options; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -12,51 +14,61 @@ namespace FastGithub /// public class FastGithubConfig { - /// - /// 域名与配置缓存 - /// - [AllowNull] + private readonly ILogger logger; private ConcurrentDictionary domainConfigCache; - /// /// 未污染的dns - /// - [AllowNull] + /// public IPEndPoint PureDns { get; private set; } /// /// 速度快的dns /// - [AllowNull] public IPEndPoint FastDns { get; private set; } /// /// 获取域名配置 - /// - [AllowNull] + /// public Dictionary DomainConfigs { get; private set; } /// /// FastGithub配置 - /// + /// /// - public FastGithubConfig(IOptionsMonitor options) + /// + public FastGithubConfig( + IOptionsMonitor options, + ILogger logger) { - this.Init(options.CurrentValue); - options.OnChange(opt => this.Init(opt)); + this.logger = logger; + + var opt = options.CurrentValue; + this.domainConfigCache = new ConcurrentDictionary(); + this.PureDns = opt.PureDns.ToIPEndPoint(); + this.FastDns = opt.FastDns.ToIPEndPoint(); + this.DomainConfigs = opt.DomainConfigs.ToDictionary(kv => new DomainMatch(kv.Key), kv => kv.Value); + + options.OnChange(opt => this.Update(opt)); } /// - /// 初始化 + /// 更新配置 /// /// - private void Init(FastGithubOptions options) + private void Update(FastGithubOptions options) { - this.domainConfigCache = new ConcurrentDictionary(); - this.PureDns = options.PureDns.ToIPEndPoint(); - this.FastDns = options.FastDns.ToIPEndPoint(); - this.DomainConfigs = options.DomainConfigs.ToDictionary(kv => new DomainMatch(kv.Key), kv => kv.Value); + try + { + this.domainConfigCache = new ConcurrentDictionary(); + this.PureDns = options.PureDns.ToIPEndPoint(); + this.FastDns = options.FastDns.ToIPEndPoint(); + this.DomainConfigs = options.DomainConfigs.ToDictionary(kv => new DomainMatch(kv.Key), kv => kv.Value); + } + catch (Exception ex) + { + this.logger.LogError(ex.Message); + } } /// @@ -77,14 +89,19 @@ namespace FastGithub /// public bool TryGetDomainConfig(string domain, [MaybeNullWhen(false)] out DomainConfig value) { - value = this.domainConfigCache.GetOrAdd(domain, GetDomainConfig); + value = this.domainConfigCache.GetOrAdd(domain, this.GetDomainConfig); return value != null; + } - DomainConfig? GetDomainConfig(string domain) - { - var key = this.DomainConfigs.Keys.FirstOrDefault(item => item.IsMatch(domain)); - return key == null ? null : this.DomainConfigs[key]; - } + /// + /// 获取域名配置 + /// + /// + /// + private DomainConfig? GetDomainConfig(string domain) + { + var key = this.DomainConfigs.Keys.FirstOrDefault(item => item.IsMatch(domain)); + return key == null ? null : this.DomainConfigs[key]; } } }