Browse Source

perf: Use IHttpClientFactory instead of HttpClient

pull/532/head
cKey 4 years ago
parent
commit
b64e10a932
  1. 12
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksModule.cs
  2. 15
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs
  3. 13
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs

12
aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksModule.cs

@ -17,11 +17,23 @@ namespace LINGYUN.Abp.Webhooks;
[DependsOn(typeof(AbpHttpClientModule))]
public class AbpWebhooksModule : AbpModule
{
internal const string WebhooksClient = "Abp.Webhooks.HttpClient";
public override void PreConfigureServices(ServiceConfigurationContext context)
{
AutoAddDefinitionProviders(context.Services);
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
var options = context.Services.ExecutePreConfiguredActions<AbpWebhooksOptions>();
context.Services.AddHttpClient(WebhooksClient, client =>
{
client.Timeout = options.TimeoutDuration;
});
}
private static void AutoAddDefinitionProviders(IServiceCollection services)
{
var definitionProviders = new List<Type>();

15
aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs

@ -5,12 +5,21 @@ namespace LINGYUN.Abp.Webhooks;
public class AbpWebhooksOptions
{
/// <summary>
/// 默认超时时间
/// </summary>
public TimeSpan TimeoutDuration { get; set; }
/// <summary>
/// 默认最大发送次数
/// </summary>
public int MaxSendAttemptCount { get; set; }
/// <summary>
/// 是否达到最大连续失败次数时自动取消订阅
/// </summary>
public bool IsAutomaticSubscriptionDeactivationEnabled { get; set; }
/// <summary>
/// 取消订阅前最大连续失败次数
/// </summary>
public int MaxConsecutiveFailCountBeforeDeactivateSubscription { get; set; }
public ITypeList<WebhookDefinitionProvider> DefinitionProviders { get; }

13
aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs

@ -15,15 +15,18 @@ namespace LINGYUN.Abp.Webhooks
private readonly AbpWebhooksOptions _options;
private readonly IWebhookManager _webhookManager;
private readonly IHttpClientFactory _httpClientFactory;
private const string FailedRequestDefaultContent = "Webhook Send Request Failed";
public DefaultWebhookSender(
IOptions<AbpWebhooksOptions> options,
IWebhookManager webhookManager)
IWebhookManager webhookManager,
IHttpClientFactory httpClientFactory)
{
_options = options.Value;
_webhookManager = webhookManager;
_httpClientFactory = httpClientFactory;
Logger = NullLogger<DefaultWebhookSender>.Instance;
}
@ -116,11 +119,8 @@ namespace LINGYUN.Abp.Webhooks
protected virtual async Task<(bool isSucceed, HttpStatusCode statusCode, string content)> SendHttpRequest(HttpRequestMessage request)
{
using (var client = new HttpClient
{
Timeout = _options.TimeoutDuration
})
{
var client = _httpClientFactory.CreateClient(AbpWebhooksModule.WebhooksClient);
var response = await client.SendAsync(request);
var isSucceed = response.IsSuccessStatusCode;
@ -130,5 +130,4 @@ namespace LINGYUN.Abp.Webhooks
return (isSucceed, statusCode, content);
}
}
}
}

Loading…
Cancel
Save