Skip to content

Commit

Permalink
Merge pull request #962 from colinin/webhook-timeout
Browse files Browse the repository at this point in the history
feat(webhooks): user subscription rewrite timeout.
  • Loading branch information
colinin committed Jun 5, 2024
2 parents 97679d6 + d40be0e commit 22fcb3b
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 23 deletions.

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

0 comments on commit 22fcb3b

Please sign in to comment.