From 922d47fdab3f866921ea317d111b566cbeb271ad Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Mon, 21 Jun 2021 03:33:08 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84GithubContext?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Scanner/GithubContext.cs | 95 +++++++++++++++++-- FastGithub.Scanner/GithubContextHistory.cs | 77 --------------- FastGithub.Scanner/GithubDnsHttpHandler.cs | 4 +- ...textCollection.cs => GithubScanResults.cs} | 34 ++----- FastGithub.Scanner/GithubScanService.cs | 8 +- FastGithub.Scanner/IGithubScanResults.cs | 8 -- .../ScanMiddlewares/StatisticsMiddleware.cs | 2 +- .../ScannerServiceCollectionExtensions.cs | 2 +- 8 files changed, 105 insertions(+), 125 deletions(-) delete mode 100644 FastGithub.Scanner/GithubContextHistory.cs rename FastGithub.Scanner/{GithubContextCollection.cs => GithubScanResults.cs} (58%) diff --git a/FastGithub.Scanner/GithubContext.cs b/FastGithub.Scanner/GithubContext.cs index 8c2f55b..ac9a610 100644 --- a/FastGithub.Scanner/GithubContext.cs +++ b/FastGithub.Scanner/GithubContext.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Net; using System.Threading; @@ -10,9 +12,21 @@ namespace FastGithub.Scanner sealed class GithubContext : DomainAddress, IEquatable { /// - /// 获取或设置是否可用 + /// 最多保存最的近的10条记录 /// - public bool Available { get; set; } + private const int MAX_LOG_COUNT = 10; + + /// + /// 扫描记录 + /// + private record ScanLog(bool Available, TimeSpan Elapsed); + + /// + /// 扫描历史记录 + /// + private readonly Queue history = new(); + + /// /// 设置取消令牌 @@ -20,9 +34,21 @@ namespace FastGithub.Scanner public CancellationToken CancellationToken { get; } /// - /// 获取扫描历史信息 + /// 获取可用率 /// - public GithubContextHistory History { get; } = new(); + /// + public double AvailableRate => this.GetAvailableRate(); + + /// + /// 获取平均耗时 + /// + /// + public TimeSpan AvgElapsed => this.GetAvgElapsed(); + + /// + /// 获取或设置是否可用 + /// + public bool Available { get; set; } /// @@ -47,19 +73,74 @@ namespace FastGithub.Scanner this.CancellationToken = cancellationToken; } + /// + /// 获取可用率 + /// + /// + private double GetAvailableRate() + { + if (this.history.Count == 0) + { + return 0d; + } + + var availableCount = this.history.Count(item => item.Available); + return (double)availableCount / this.history.Count; + } + + /// + /// 获取平均耗时 + /// + /// + private TimeSpan GetAvgElapsed() + { + var availableCount = 0; + var availableElapsed = TimeSpan.Zero; + + foreach (var item in this.history) + { + if (item.Available == true) + { + availableCount += 1; + availableElapsed = availableElapsed.Add(item.Elapsed); + } + } + return availableCount == 0 ? TimeSpan.MaxValue : availableElapsed / availableCount; + } + + + /// + /// 添加扫描记录 + /// + /// 扫描耗时 + public void AddScanLog(TimeSpan elapsed) + { + var log = new ScanLog(this.Available, elapsed); + this.history.Enqueue(log); + while (this.history.Count > MAX_LOG_COUNT) + { + this.history.Dequeue(); + } + } + + /// + /// 是否相等 + /// + /// + /// public bool Equals(GithubContext? other) { return base.Equals(other); } /// - /// 转换为为统计信息 + /// 转换为统计信息 /// /// public string ToStatisticsString() { - var rate = Math.Round(this.History.AvailableRate * 100, 2); - return $"{{Address={this.Address}, AvailableRate={rate}%, AvgElapsed={this.History.AvgElapsed.TotalSeconds}s}}"; + var availableRate = Math.Round(this.AvailableRate * 100, 2); + return $"{{{nameof(Address)}={this.Address}, {nameof(AvailableRate)}={availableRate}%, {nameof(AvgElapsed)}={this.AvgElapsed.TotalSeconds}s}}"; } } } diff --git a/FastGithub.Scanner/GithubContextHistory.cs b/FastGithub.Scanner/GithubContextHistory.cs deleted file mode 100644 index 5f9f70e..0000000 --- a/FastGithub.Scanner/GithubContextHistory.cs +++ /dev/null @@ -1,77 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace FastGithub.Scanner -{ - /// - /// GithubContext的扫描历史 - /// - sealed class GithubContextHistory - { - /// - /// 最多保存最的近的10条记录 - /// - private const int MAX_LOG_COUNT = 10; - private record ScanLog(bool Available, TimeSpan Elapsed); - - private readonly Queue scanLogs = new(); - - /// - /// 获取可用率 - /// - /// - public double AvailableRate - { - get - { - if (this.scanLogs.Count == 0) - { - return 0d; - } - - var availableCount = this.scanLogs.Count(item => item.Available); - return (double)availableCount / this.scanLogs.Count; - } - } - - - /// - /// 获取平均耗时 - /// - /// - public TimeSpan AvgElapsed - { - get - { - var availableCount = 0; - var availableElapsed = TimeSpan.Zero; - - foreach (var item in this.scanLogs) - { - if (item.Available == true) - { - availableCount += 1; - availableElapsed = availableElapsed.Add(item.Elapsed); - } - } - - return availableCount == 0 ? TimeSpan.MaxValue : availableElapsed / availableCount; - } - } - - /// - /// 添加记录 - /// - /// 是否可用 - /// 扫描耗时 - public void Add(bool available, TimeSpan elapsed) - { - this.scanLogs.Enqueue(new ScanLog(available, elapsed)); - while (this.scanLogs.Count > MAX_LOG_COUNT) - { - this.scanLogs.Dequeue(); - } - } - } -} diff --git a/FastGithub.Scanner/GithubDnsHttpHandler.cs b/FastGithub.Scanner/GithubDnsHttpHandler.cs index abb498b..b349e44 100644 --- a/FastGithub.Scanner/GithubDnsHttpHandler.cs +++ b/FastGithub.Scanner/GithubDnsHttpHandler.cs @@ -13,12 +13,12 @@ namespace FastGithub.Scanner [Service(ServiceLifetime.Transient)] sealed class GithubDnsHttpHandler : DelegatingHandler { - private readonly GithubContextCollection scanResults; + private readonly GithubScanResults scanResults; /// /// Github的dns解析的httpHandler /// - public GithubDnsHttpHandler(GithubContextCollection scanResults) + public GithubDnsHttpHandler(GithubScanResults scanResults) { this.scanResults = scanResults; } diff --git a/FastGithub.Scanner/GithubContextCollection.cs b/FastGithub.Scanner/GithubScanResults.cs similarity index 58% rename from FastGithub.Scanner/GithubContextCollection.cs rename to FastGithub.Scanner/GithubScanResults.cs index 8997efa..f274003 100644 --- a/FastGithub.Scanner/GithubContextCollection.cs +++ b/FastGithub.Scanner/GithubScanResults.cs @@ -9,10 +9,10 @@ namespace FastGithub.Scanner /// GithubContext集合 /// [Service(ServiceLifetime.Singleton)] - sealed class GithubContextCollection : IGithubScanResults + sealed class GithubScanResults : IGithubScanResults { private readonly object syncRoot = new(); - private readonly List contextList = new(); + private readonly List contexts = new(); /// /// 添加GithubContext @@ -23,11 +23,11 @@ namespace FastGithub.Scanner { lock (this.syncRoot) { - if (this.contextList.Contains(context)) + if (this.contexts.Contains(context)) { return false; } - this.contextList.Add(context); + this.contexts.Add(context); return true; } } @@ -40,23 +40,7 @@ namespace FastGithub.Scanner { lock (this.syncRoot) { - return this.contextList.ToArray(); - } - } - - /// - /// 查询ip是否可用 - /// - /// - /// - /// - public bool IsAvailable(string domain, IPAddress address) - { - lock (this.syncRoot) - { - var target = new GithubContext(domain, address); - var context = this.contextList.Find(item => item.Equals(target)); - return context != null && context.Available; + return this.contexts.ToArray(); } } @@ -69,11 +53,11 @@ namespace FastGithub.Scanner { lock (this.syncRoot) { - return this.contextList - .Where(item => item.Domain == domain && item.History.AvailableRate > 0d) - .OrderByDescending(item => item.History.AvailableRate) + return this.contexts + .Where(item => item.Domain == domain && item.AvailableRate > 0d) + .OrderByDescending(item => item.AvailableRate) .ThenByDescending(item => item.Available) - .ThenBy(item => item.History.AvgElapsed) + .ThenBy(item => item.AvgElapsed) .Select(item => item.Address) .FirstOrDefault(); } diff --git a/FastGithub.Scanner/GithubScanService.cs b/FastGithub.Scanner/GithubScanService.cs index 5b0be45..bac71c3 100644 --- a/FastGithub.Scanner/GithubScanService.cs +++ b/FastGithub.Scanner/GithubScanService.cs @@ -15,7 +15,7 @@ namespace FastGithub.Scanner sealed class GithubScanService { private readonly GithubLookupFacotry lookupFactory; - private readonly GithubContextCollection scanResults; + private readonly GithubScanResults scanResults; private readonly ILoggerFactory loggerFactory; private readonly ILogger logger; @@ -31,7 +31,7 @@ namespace FastGithub.Scanner /// public GithubScanService( GithubLookupFacotry lookupFactory, - GithubContextCollection scanResults, + GithubScanResults scanResults, IServiceProvider appService, ILoggerFactory loggerFactory, ILogger logger) @@ -94,8 +94,8 @@ namespace FastGithub.Scanner var results = this.scanResults.ToArray(); var contexts = results .OrderBy(item => item.Domain) - .ThenByDescending(item => item.History.AvailableRate) - .ThenBy(item => item.History.AvgElapsed); + .ThenByDescending(item => item.AvailableRate) + .ThenBy(item => item.AvgElapsed); foreach (var context in contexts) { diff --git a/FastGithub.Scanner/IGithubScanResults.cs b/FastGithub.Scanner/IGithubScanResults.cs index b12be24..0dc1134 100644 --- a/FastGithub.Scanner/IGithubScanResults.cs +++ b/FastGithub.Scanner/IGithubScanResults.cs @@ -7,14 +7,6 @@ namespace FastGithub.Scanner /// public interface IGithubScanResults { - /// - /// 查询ip是否可用 - /// - /// - /// - /// - bool IsAvailable(string domain, IPAddress address); - /// /// 查找最优的ip /// diff --git a/FastGithub.Scanner/ScanMiddlewares/StatisticsMiddleware.cs b/FastGithub.Scanner/ScanMiddlewares/StatisticsMiddleware.cs index 1d95f5c..46a0a13 100644 --- a/FastGithub.Scanner/ScanMiddlewares/StatisticsMiddleware.cs +++ b/FastGithub.Scanner/ScanMiddlewares/StatisticsMiddleware.cs @@ -31,7 +31,7 @@ namespace FastGithub.Scanner.ScanMiddlewares stopwatch.Stop(); } - context.History.Add(context.Available, stopwatch.Elapsed); + context.AddScanLog(stopwatch.Elapsed); } } } diff --git a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs index ca46c4d..b80e58d 100644 --- a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs +++ b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs @@ -44,7 +44,7 @@ namespace FastGithub .AddServiceAndOptions(assembly, configuration) .AddHostedService() .AddHostedService() - .AddSingleton(appService => appService.GetRequiredService()); + .AddSingleton(appService => appService.GetRequiredService()); ; } }