From f0d0fc7fd2fbc35fa0042c09cac4e785f68a1b20 Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Wed, 16 Jun 2021 19:57:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E6=94=BEIGithubScanResults=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Dns/GithubRequestResolver.cs | 10 ++--- FastGithub.Scanner/GithubContextCollection.cs | 28 +++++++------- .../GithubFullScanHostedService.cs | 4 +- .../GithubResultScanHostedService.cs | 4 +- FastGithub.Scanner/GithubScanService.cs | 37 +++++++------------ FastGithub.Scanner/IGithubScanResults.cs | 11 ++++++ FastGithub.Scanner/IGithubScanService.cs | 17 --------- .../ScannerServiceCollectionExtensions.cs | 8 +--- 8 files changed, 50 insertions(+), 69 deletions(-) create mode 100644 FastGithub.Scanner/IGithubScanResults.cs delete mode 100644 FastGithub.Scanner/IGithubScanService.cs diff --git a/FastGithub.Dns/GithubRequestResolver.cs b/FastGithub.Dns/GithubRequestResolver.cs index fcbac57..a3c1543 100644 --- a/FastGithub.Dns/GithubRequestResolver.cs +++ b/FastGithub.Dns/GithubRequestResolver.cs @@ -16,17 +16,17 @@ namespace FastGithub.Dns [Service(ServiceLifetime.Singleton)] sealed class GithubRequestResolver : IRequestResolver { - private readonly IGithubScanService githubScanService; + private readonly IGithubScanResults githubScanResults; private readonly IMemoryCache memoryCache; private readonly ILogger logger; private readonly TimeSpan TTL = TimeSpan.FromMinutes(10d); public GithubRequestResolver( - IGithubScanService githubScanService, + IGithubScanResults githubScanResults, IMemoryCache memoryCache, ILogger logger) { - this.githubScanService = githubScanService; + this.githubScanResults = githubScanResults; this.memoryCache = memoryCache; this.logger = logger; } @@ -69,14 +69,14 @@ namespace FastGithub.Dns var key = $"ttl:{domain}"; if (this.memoryCache.TryGetValue(key, out var address)) { - if (this.githubScanService.IsAvailable(domain, address)) + if (this.githubScanResults.IsAvailable(domain, address)) { return address; } this.memoryCache.Remove(key); } - address = this.githubScanService.FindBestAddress(domain); + address = this.githubScanResults.FindBestAddress(domain); if (address != null) { this.memoryCache.Set(key, address, ttl); diff --git a/FastGithub.Scanner/GithubContextCollection.cs b/FastGithub.Scanner/GithubContextCollection.cs index 648c44a..11206c7 100644 --- a/FastGithub.Scanner/GithubContextCollection.cs +++ b/FastGithub.Scanner/GithubContextCollection.cs @@ -1,11 +1,12 @@ -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; +using Microsoft.Extensions.DependencyInjection; +using System.Collections.Generic; using System.Linq; using System.Net; namespace FastGithub.Scanner { - sealed class GithubContextCollection + [Service(ServiceLifetime.Singleton)] + sealed class GithubContextCollection : IGithubScanResults { private readonly object syncRoot = new(); private readonly List contextList = new(); @@ -24,16 +25,6 @@ namespace FastGithub.Scanner } - public bool TryGet(string domain, IPAddress address, [MaybeNullWhen(false)] out GithubContext context) - { - lock (this.syncRoot) - { - var target = new GithubContext(domain, address); - context = this.contextList.Find(item => item.Equals(target)); - return context != null; - } - } - public GithubContext[] ToArray() { lock (this.syncRoot) @@ -42,6 +33,17 @@ namespace FastGithub.Scanner } } + + 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; + } + } + /// /// 查找又稳又快的ip /// diff --git a/FastGithub.Scanner/GithubFullScanHostedService.cs b/FastGithub.Scanner/GithubFullScanHostedService.cs index 6fc2061..be9d2b8 100644 --- a/FastGithub.Scanner/GithubFullScanHostedService.cs +++ b/FastGithub.Scanner/GithubFullScanHostedService.cs @@ -8,11 +8,11 @@ namespace FastGithub { sealed class GithubFullScanHostedService : BackgroundService { - private readonly IGithubScanService githubScanService; + private readonly GithubScanService githubScanService; private readonly IOptionsMonitor options; public GithubFullScanHostedService( - IGithubScanService githubScanService, + GithubScanService githubScanService, IOptionsMonitor options) { this.githubScanService = githubScanService; diff --git a/FastGithub.Scanner/GithubResultScanHostedService.cs b/FastGithub.Scanner/GithubResultScanHostedService.cs index 11ab4a0..62c1e26 100644 --- a/FastGithub.Scanner/GithubResultScanHostedService.cs +++ b/FastGithub.Scanner/GithubResultScanHostedService.cs @@ -8,11 +8,11 @@ namespace FastGithub { sealed class GithubResultScanHostedService : BackgroundService { - private readonly IGithubScanService githubScanService; + private readonly GithubScanService githubScanService; private readonly IOptionsMonitor options; public GithubResultScanHostedService( - IGithubScanService githubScanService, + GithubScanService githubScanService, IOptionsMonitor options) { this.githubScanService = githubScanService; diff --git a/FastGithub.Scanner/GithubScanService.cs b/FastGithub.Scanner/GithubScanService.cs index 9446363..3b89603 100644 --- a/FastGithub.Scanner/GithubScanService.cs +++ b/FastGithub.Scanner/GithubScanService.cs @@ -1,33 +1,34 @@ using FastGithub.Scanner.Middlewares; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System; using System.Linq; -using System.Net; using System.Threading; using System.Threading.Tasks; namespace FastGithub.Scanner { - [Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubScanService))] - sealed class GithubScanService : IGithubScanService + [Service(ServiceLifetime.Singleton)] + sealed class GithubScanService { private readonly GithubMetaService metaService; private readonly ILogger logger; - private readonly GithubContextCollection results = new(); + private readonly GithubContextCollection contextCollection; private readonly InvokeDelegate fullScanDelegate; private readonly InvokeDelegate resultScanDelegate; public GithubScanService( GithubMetaService metaService, - ILogger logger, - IPipelineBuilder pipelineBuilder) + GithubContextCollection contextCollection, + IServiceProvider appService, + ILogger logger) { this.metaService = metaService; + this.contextCollection = contextCollection; this.logger = logger; - - this.fullScanDelegate = pipelineBuilder - .New() + ; + this.fullScanDelegate = new PipelineBuilder(appService, ctx => Task.CompletedTask) .Use() .Use() .Use() @@ -35,8 +36,7 @@ namespace FastGithub.Scanner .Use() .Build(); - this.resultScanDelegate = pipelineBuilder - .New() + this.resultScanDelegate = new PipelineBuilder(appService, ctx => Task.CompletedTask) .Use() .Use() .Use() @@ -61,7 +61,7 @@ namespace FastGithub.Scanner await this.fullScanDelegate(context); if (context.Available == true) { - this.results.Add(context); + this.contextCollection.Add(context); } } } @@ -70,7 +70,7 @@ namespace FastGithub.Scanner { this.logger.LogInformation("结果扫描开始"); - var contexts = this.results.ToArray(); + var contexts = this.contextCollection.ToArray(); foreach (var context in contexts) { await this.resultScanDelegate(context); @@ -78,16 +78,5 @@ namespace FastGithub.Scanner this.logger.LogInformation("结果扫描结束"); } - - public IPAddress? FindBestAddress(string domain) - { - return this.results.FindBestAddress(domain); - } - - - public bool IsAvailable(string domain, IPAddress address) - { - return this.results.TryGet(domain, address, out var context) && context.Available; - } } } diff --git a/FastGithub.Scanner/IGithubScanResults.cs b/FastGithub.Scanner/IGithubScanResults.cs new file mode 100644 index 0000000..ef126ef --- /dev/null +++ b/FastGithub.Scanner/IGithubScanResults.cs @@ -0,0 +1,11 @@ +using System.Net; + +namespace FastGithub.Scanner +{ + public interface IGithubScanResults + { + bool IsAvailable(string domain, IPAddress address); + + IPAddress? FindBestAddress(string domain); + } +} diff --git a/FastGithub.Scanner/IGithubScanService.cs b/FastGithub.Scanner/IGithubScanService.cs deleted file mode 100644 index 50b2a35..0000000 --- a/FastGithub.Scanner/IGithubScanService.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Net; -using System.Threading; -using System.Threading.Tasks; - -namespace FastGithub.Scanner -{ - public interface IGithubScanService - { - Task ScanAllAsync(CancellationToken cancellationToken); - - Task ScanResultAsync(); - - bool IsAvailable(string domain, IPAddress address); - - IPAddress? FindBestAddress(string domain); - } -} \ No newline at end of file diff --git a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs index a3d9572..9fa7996 100644 --- a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs +++ b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs @@ -1,7 +1,6 @@ using FastGithub.Scanner; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using System.Threading.Tasks; namespace FastGithub { @@ -24,11 +23,8 @@ namespace FastGithub .AddServiceAndOptions(assembly, configuration) .AddHostedService() .AddHostedService() - .AddSingleton>(appService => - { - return new PipelineBuilder(appService, ctx => Task.CompletedTask); - }) - ; + .AddSingleton(appService => appService.GetRequiredService()); + ; } } }