Skip to content

Commit

Permalink
retry clearml calls on 429 responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnml1135 committed Oct 31, 2024
1 parent 6e9b736 commit 82c35dd
Showing 1 changed file with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Serval.Translation.V1;
using Polly.Extensions.Http;
using Serval.Translation.V1;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -155,30 +156,34 @@ public static IMachineBuilder AddClearMLService(this IMachineBuilder builder, st
if (connectionString is null)
throw new InvalidOperationException("ClearML connection string is required");

var policy = Policy
.Handle<HttpRequestException>()
.OrTransientHttpStatusCode()
.OrResult(msg => msg.StatusCode == HttpStatusCode.TooManyRequests)
.WaitAndRetryAsync(
7,
retryAttempt => TimeSpan.FromSeconds(2 * retryAttempt), // total 56, less than the 1 minute 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.AddHttpClient("ClearML")
.ConfigureHttpClient(httpClient => httpClient.BaseAddress = new Uri(connectionString!))
.AddTransientHttpErrorPolicy(b =>
b.WaitAndRetryAsync(
7,
retryAttempt => TimeSpan.FromSeconds(2 * retryAttempt), // total 56, less than the 1 minute 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;
}
)
);
.AddPolicyHandler(policy);

builder.Services.AddSingleton<IClearMLService, ClearMLService>();

Expand Down

0 comments on commit 82c35dd

Please sign in to comment.