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

What about 6 retries and not 3? #526

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Loading