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];
}
}
}