Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webhooks): user subscription rewrite timeout. #962

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace LY.MicroService.WebhooksManagement.EntityFrameworkCore.Migrations
{
/// <inheritdoc />
public partial class AddTimeoutDurationToSubscription : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "TimeoutDuration",
table: "AbpWebhooksSubscriptions",
type: "int",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "TimeoutDuration",
table: "AbpWebhooksSubscriptions");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("_Abp_DatabaseProvider", EfCoreDatabaseProvider.MySql)
.HasAnnotation("ProductVersion", "7.0.2")
.HasAnnotation("ProductVersion", "8.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 64);

modelBuilder.Entity("LINGYUN.Abp.WebhooksManagement.WebhookDefinitionRecord", b =>
Expand Down Expand Up @@ -226,6 +226,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<Guid?>("TenantId")
.HasColumnType("char(36)");

b.Property<int?>("TimeoutDuration")
.HasColumnType("int");

b.Property<string>("WebhookUri")
.IsRequired()
.HasMaxLength(255)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public AbpWebhooksOptions()
{ "X-Requested-From", "abp-webhooks" },
};

DefaultAgentIdentifier = "Abp Webhooks";
DefaultAgentIdentifier = "Abp-Webhooks";

var assembly = typeof(AbpWebhooksOptions).Assembly;
var versionAttr = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
var versionAttr = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
if (versionAttr != null)
{
DefaultAgentIdentifier += " " + versionAttr.InformationalVersion;
DefaultAgentIdentifier += "/" + versionAttr.Version;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ await _backgroundJobManager.EnqueueAsync(new WebhookSenderArgs
Headers = headersToSend,
Secret = webhookSubscription.Secret,
WebhookUri = webhookSubscription.WebhookUri,
SendExactSameData = sendExactSameData
SendExactSameData = sendExactSameData,
TimeoutDuration = webhookSubscription.TimeoutDuration,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public async Task<Guid> SendWebhookAsync(WebhookSenderArgs webhookSenderArgs)

try
{
var response = await SendHttpRequest(request);
var client = CreateWebhookClient(webhookSenderArgs);

var response = await SendHttpRequest(client, request);

isSucceed = response.IsSuccessStatusCode;
statusCode = response.StatusCode;
Expand Down Expand Up @@ -112,6 +114,18 @@ protected virtual HttpRequestMessage CreateWebhookRequestMessage(WebhookSenderAr
return new HttpRequestMessage(HttpMethod.Post, webhookSenderArgs.WebhookUri);
}

protected virtual HttpClient CreateWebhookClient(WebhookSenderArgs webhookSenderArgs)
{
var client = _httpClientFactory.CreateClient(AbpWebhooksModule.WebhooksClient);
if (webhookSenderArgs.TimeoutDuration.HasValue &&
(webhookSenderArgs.TimeoutDuration >= 10 && webhookSenderArgs.TimeoutDuration <= 300))
{
client.Timeout = TimeSpan.FromSeconds(webhookSenderArgs.TimeoutDuration.Value);
}

return client;
}

protected virtual void AddAdditionalHeaders(HttpRequestMessage request, WebhookSenderArgs webhookSenderArgs)
{
foreach (var header in _options.DefaultHttpHeaders)
Expand Down Expand Up @@ -169,10 +183,8 @@ protected virtual void AddAdditionalHeaders(HttpRequestMessage request, WebhookS
}
}

protected async virtual Task<HttpResponseMessage> SendHttpRequest(HttpRequestMessage request)
protected async virtual Task<HttpResponseMessage> SendHttpRequest(HttpClient client, HttpRequestMessage request)
{
var client = _httpClientFactory.CreateClient(AbpWebhooksModule.WebhooksClient);

return await client.SendAsync(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public class WebhookSenderArgs
/// </para>
/// </summary>
public bool SendExactSameData { get; set; }
/// <summary>
/// Request timeout time, in seconds
/// </summary>
public int? TimeoutDuration { get; set; }

public WebhookSenderArgs()
{
Expand Down
Loading
Loading