diff --git a/FastGithub/GithubHostedService.cs b/FastGithub/GithubHostedService.cs index 7067d44..79cb597 100644 --- a/FastGithub/GithubHostedService.cs +++ b/FastGithub/GithubHostedService.cs @@ -1,74 +1,29 @@ -using FastGithub.Middlewares; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; namespace FastGithub { - sealed class GithubHostedService : BackgroundService + sealed class GithubHostedService : IHostedService { - private readonly GithubDelegate githubDelegate; private readonly IServiceScopeFactory serviceScopeFactory; - private readonly ILogger logger; - public GithubHostedService( - IServiceProvider appServiceProvider, - IServiceScopeFactory serviceScopeFactory, - ILogger logger) + public GithubHostedService(IServiceScopeFactory serviceScopeFactory) { - this.githubDelegate = new GithubBuilder(appServiceProvider, ctx => Task.CompletedTask) - .Use() - .Use() - .Use() - .Build(); - this.serviceScopeFactory = serviceScopeFactory; - this.logger = logger; } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) + public Task StartAsync(CancellationToken cancellationToken) { - using var scope = this.serviceScopeFactory.CreateScope(); - var metaService = scope.ServiceProvider.GetRequiredService(); + var scope = this.serviceScopeFactory.CreateScope(); + var service = scope.ServiceProvider.GetRequiredService(); + return service.ScanAddressAsync(cancellationToken); + } - var meta = await metaService.GetMetaAsync(); - - if (meta != null) - { - var contexts = new List(); - var tasks = this.GetScanTasks(meta, contexts); - await Task.WhenAll(tasks); - - var orderByContexts = contexts - .Where(item => item.HttpElapsed != null) - .OrderBy(item => item.HttpElapsed); - - foreach (var context in orderByContexts) - { - this.logger.LogInformation($"{context.Address} {context.HttpElapsed}"); - } - } - - this.logger.LogInformation("扫描结束"); - } - - - private IEnumerable GetScanTasks(Meta meta, IList contexts) + public Task StopAsync(CancellationToken cancellationToken) { - foreach (var address in meta.ToIPv4Address()) - { - var context = new GithubContext - { - Address = address, - }; - contexts.Add(context); - yield return this.githubDelegate(context); - } + return Task.CompletedTask; } } } diff --git a/FastGithub/MetaService.cs b/FastGithub/GithubMetaService.cs similarity index 73% rename from FastGithub/MetaService.cs rename to FastGithub/GithubMetaService.cs index 646dcd0..fd7911c 100644 --- a/FastGithub/MetaService.cs +++ b/FastGithub/GithubMetaService.cs @@ -3,32 +3,33 @@ using Microsoft.Extensions.Options; using System; using System.Net.Http; using System.Net.Http.Json; +using System.Threading; using System.Threading.Tasks; namespace FastGithub { - sealed class MetaService + sealed class GithubMetaService { private readonly IHttpClientFactory httpClientFactory; private readonly IOptionsMonitor options; - private readonly ILogger logger; + private readonly ILogger logger; - public MetaService( + public GithubMetaService( IHttpClientFactory httpClientFactory, IOptionsMonitor options, - ILogger logger) + ILogger logger) { this.httpClientFactory = httpClientFactory; this.options = options; this.logger = logger; } - public async Task GetMetaAsync() + public async Task GetMetaAsync(CancellationToken cancellationToken = default) { try { var httpClient = this.httpClientFactory.CreateClient(); - return await httpClient.GetFromJsonAsync(this.options.CurrentValue.MetaUri); + return await httpClient.GetFromJsonAsync(this.options.CurrentValue.MetaUri, cancellationToken); } catch (Exception ex) { diff --git a/FastGithub/GithubService.cs b/FastGithub/GithubService.cs new file mode 100644 index 0000000..fbad549 --- /dev/null +++ b/FastGithub/GithubService.cs @@ -0,0 +1,61 @@ +using Microsoft.Extensions.Logging; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace FastGithub +{ + sealed class GithubService + { + private readonly GithubMetaService githubMetaService; + private readonly GithubDelegate githubDelegate; + private readonly ILogger logger; + + public GithubService( + GithubMetaService githubMetaService, + GithubDelegate githubDelegate, + ILogger logger) + { + this.githubMetaService = githubMetaService; + this.githubDelegate = githubDelegate; + this.logger = logger; + } + + public async Task ScanAddressAsync(CancellationToken cancellationToken = default) + { + var meta = await this.githubMetaService.GetMetaAsync(cancellationToken); + if (meta != null) + { + var contexts = new List(); + var scanTasks = this.GetMetaScanTasks(meta, contexts); + await Task.WhenAll(scanTasks); + + var sortedContexts = contexts + .Where(item => item.HttpElapsed != null) + .OrderBy(item => item.HttpElapsed); + + foreach (var context in sortedContexts) + { + this.logger.LogInformation($"{context.Address} {context.HttpElapsed}"); + } + } + + this.logger.LogInformation("扫描结束"); + } + + + private IEnumerable GetMetaScanTasks(Meta meta, IList contexts) + { + foreach (var address in meta.ToIPv4Address()) + { + var context = new GithubContext + { + Address = address, + }; + contexts.Add(context); + yield return this.githubDelegate(context); + } + } + } +} diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index 0f821fa..9c08ae8 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -1,6 +1,7 @@ using FastGithub.Middlewares; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using System.Threading.Tasks; namespace FastGithub { @@ -29,10 +30,20 @@ namespace FastGithub services .Configure(ctx.Configuration.GetSection("Github")) .AddHttpClient() - .AddTransient() + .AddTransient() + .AddTransient() + .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton(serviceProvider => + { + return new GithubBuilder(serviceProvider, ctx => Task.CompletedTask) + .Use() + .Use() + .Use() + .Build(); + }) .AddHostedService() ; });