Skip to content

Commit

Permalink
What about 6 retries and not 3?
Browse files Browse the repository at this point in the history
  • Loading branch information
johnml1135 committed Oct 30, 2024
1 parent b6b9ae4 commit 9b5d2c9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"ptcc",
"Rebinder",
"stylesheet",
"timespan",
"upserted",
"USFM"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,26 @@ public static IMachineBuilder AddClearMLService(this IMachineBuilder builder, st
builder
.Services.AddHttpClient("ClearML")
.ConfigureHttpClient(httpClient => httpClient.BaseAddress = new Uri(connectionString!))
// Add retry policy; fail after approx. 2 + 4 + 8 = 14 seconds
.AddTransientHttpErrorPolicy(b =>
b.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
b.WaitAndRetryAsync(
6,
retryAttempt => TimeSpan.FromSeconds(new[] { 2, 4, 8, 12, 14, 16 }[retryAttempt]), // total 56 seconds, under the 1 minute https limit
onRetryAsync: (outcome, timespan, retryAttempt, context) =>
{
if (retryAttempt < 3)
return Task.CompletedTask;
// Log the retry attempt
var serviceProvider = builder.Services.BuildServiceProvider();
var logger = serviceProvider.GetService<ILogger<ClearMLService>>();
logger?.LogInformation(
"Retry {RetryAttempt} encountered an error. Waiting {Timespan} before next retry. Error: {ErrorMessage}",
retryAttempt,
timespan,
outcome.Exception?.Message
);
return Task.CompletedTask;
}
)
);

builder.Services.AddSingleton<IClearMLService, ClearMLService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ public static IServalBuilder AddWebhooks(this IServalBuilder builder)
{
builder
.Services.AddHttpClient<WebhookJob>()
// Add retry policy; fail after approx. 1.5 + 2.25 ... 1.5^6 ~= 31 seconds
.AddTransientHttpErrorPolicy(b =>
b.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
b.WaitAndRetryAsync(
6,
retryAttempt => TimeSpan.FromSeconds(new[] { 2, 4, 8, 12, 14, 16 }[retryAttempt]) // total 56 seconds, under the 1 minute https limit
)
);
builder.Services.AddScoped<IWebhookService, WebhookService>();
return builder;
Expand Down

0 comments on commit 9b5d2c9

Please sign in to comment.