refactor: 增加 GiteeHttpClient 复用 HttpClient 提高速度

This commit is contained in:
Argo Zhang 2019-08-14 17:19:00 +08:00
parent f21e26d26a
commit 96df7bfe76
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
4 changed files with 46 additions and 25 deletions

View File

@ -20,18 +20,16 @@ namespace Bootstrap.Admin.Controllers.Api
/// <summary>
/// 获取 Gitee 网站 Issues 信息
/// </summary>
/// <param name="httpClientFactory"></param>
/// <param name="client"></param>
/// <param name="userName"></param>
/// <param name="repoName"></param>
/// <param name="label"></param>
/// <param name="color"></param>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult> Issues([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
public async Task<ActionResult> Issues([FromServices]GiteeHttpClient client, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
{
var client = httpClientFactory.CreateClient();
client.Timeout = TimeSpan.FromMilliseconds(2000);
var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"), content =>
var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"), content =>
{
var regex = Regex.Matches(content, "<div class='ui mini circular label'>([\\d]+)</div>", RegexOptions.IgnoreCase);
var labels = new string[] { "open", "progressing", "closed", "rejected" };
@ -45,18 +43,16 @@ namespace Bootstrap.Admin.Controllers.Api
/// <summary>
/// 获取 Gitee 网站 Pulls 信息
/// </summary>
/// <param name="httpClientFactory"></param>
/// <param name="client"></param>
/// <param name="userName"></param>
/// <param name="repoName"></param>
/// <param name="label"></param>
/// <param name="color"></param>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult> Pulls([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
public async Task<ActionResult> Pulls([FromServices]GiteeHttpClient client, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
{
var client = httpClientFactory.CreateClient();
client.Timeout = TimeSpan.FromMilliseconds(2000);
var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls"), content =>
var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls"), content =>
{
var regex = Regex.Matches(content, "<div class='ui mini circular label'>([\\d]+)</div>", RegexOptions.IgnoreCase);
var labels = new string[] { "open", "merged", "closed" };
@ -69,18 +65,16 @@ namespace Bootstrap.Admin.Controllers.Api
/// <summary>
/// 获取 Gitee 网站 Releases 信息
/// </summary>
/// <param name="httpClientFactory"></param>
/// <param name="client"></param>
/// <param name="userName"></param>
/// <param name="repoName"></param>
/// <param name="label"></param>
/// <param name="color"></param>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult> Releases([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
public async Task<ActionResult> Releases([FromServices]GiteeHttpClient client, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
{
var client = httpClientFactory.CreateClient();
client.Timeout = TimeSpan.FromMilliseconds(2000);
var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases"), content =>
var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases"), content =>
{
var regex = Regex.Match(content, $"<a href=\"/{userName}/{repoName}/releases/([^\\s]+)\" target=\"_blank\">", RegexOptions.IgnoreCase);
return string.IsNullOrEmpty(content) ? "unknown" : regex.Groups[1].Value;
@ -91,7 +85,7 @@ namespace Bootstrap.Admin.Controllers.Api
/// <summary>
/// 获取 Gitee 网站 Builds 信息
/// </summary>
/// <param name="httpClientFactory"></param>
/// <param name="client"></param>
/// <param name="userName"></param>
/// <param name="projName"></param>
/// <param name="branchName"></param>
@ -99,10 +93,9 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="color"></param>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult> Builds([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "ArgoZhang", [FromQuery]string projName = "bootstrapadmin", [FromQuery]string branchName = "master", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
public async Task<ActionResult> Builds([FromServices]GiteeHttpClient client, [FromQuery]string userName = "ArgoZhang", [FromQuery]string projName = "bootstrapadmin", [FromQuery]string branchName = "master", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
{
var client = httpClientFactory.CreateClient();
var ret = await GetJsonAsync(() => client.GetAsJsonAsync<AppveyorBuildResult>($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}", null, new CancellationTokenSource(2000).Token), content =>
var ret = await GetJsonAsync(() => client.HttpClient.GetAsJsonAsync<AppveyorBuildResult>($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}", null, new CancellationTokenSource(2000).Token), content =>
{
return content == null ? "unknown" : content.Build.Version;
});

View File

@ -0,0 +1,27 @@
using System;
using System.Net.Http;
namespace Bootstrap.Admin
{
/// <summary>
/// Gitee HttpClient 操作类
/// </summary>
public class GiteeHttpClient
{
/// <summary>
/// HttpClient 实例
/// </summary>
public HttpClient HttpClient { get; private set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="client"></param>
public GiteeHttpClient(HttpClient client)
{
client.Timeout = TimeSpan.FromSeconds(10);
client.DefaultRequestHeaders.Connection.Add("keep-alive");
HttpClient = client;
}
}
}

View File

@ -15,16 +15,16 @@ namespace Bootstrap.Admin.HealthChecks
/// </summary>
public class GiteeHttpHealthCheck : IHealthCheck
{
private readonly HttpClient _client;
private readonly GiteeHttpClient _client;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="factory"></param>
/// <param name="client"></param>
/// <param name="accessor"></param>
public GiteeHttpHealthCheck(IHttpClientFactory factory, IHttpContextAccessor accessor)
public GiteeHttpHealthCheck(GiteeHttpClient client, IHttpContextAccessor accessor)
{
_client = factory.CreateClient();
_client.BaseAddress = new Uri($"{accessor.HttpContext.Request.Scheme}://{accessor.HttpContext.Request.Host}{accessor.HttpContext.Request.PathBase}");
_client = client;
_client.HttpClient.BaseAddress = new Uri($"{accessor.HttpContext.Request.Scheme}://{accessor.HttpContext.Request.Host}{accessor.HttpContext.Request.PathBase}");
}
/// <summary>
@ -42,7 +42,7 @@ namespace Bootstrap.Admin.HealthChecks
{
var sw = Stopwatch.StartNew();
Exception error = null;
var result = await _client.GetAsJsonAsync<object>($"/api/Gitee/{url}", ex => error = ex, cancellationToken);
var result = await _client.HttpClient.GetAsJsonAsync<object>($"/api/Gitee/{url}", ex => error = ex, cancellationToken);
sw.Stop();
data.Add(url, error == null ? $"{result} Elapsed: {sw.Elapsed}" : $"{result} Elapsed: {sw.Elapsed} Exception: {error}");
})).ToArray());

View File

@ -64,6 +64,7 @@ namespace Bootstrap.Admin
services.AddSwagger();
services.AddButtonAuthorization();
services.AddDemoTask();
services.AddHttpClient<GiteeHttpClient>();
services.AddHealthChecks().AddBootstrapAdminHealthChecks();
services.AddMvc(options =>
{