fix: retry transient postgres polling saves
This commit is contained in:
@@ -743,15 +743,40 @@ public sealed class LiveRoomPollingBackgroundService : BackgroundService, ILiveR
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
return;
|
||||
}
|
||||
catch (DbUpdateException ex) when (attempt < 5 && IsTransientDatabaseException(ex))
|
||||
catch (Exception ex) when (attempt < 5 && IsTransientDatabaseException(ex))
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(300 * Math.Pow(2, attempt - 1)), cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsTransientDatabaseException(DbUpdateException exception) =>
|
||||
exception.InnerException is NpgsqlException npgsqlException && npgsqlException.IsTransient;
|
||||
private static bool IsTransientDatabaseException(Exception exception)
|
||||
{
|
||||
if (exception is DbUpdateException dbUpdateException)
|
||||
{
|
||||
return dbUpdateException.InnerException is not null &&
|
||||
IsTransientDatabaseException(dbUpdateException.InnerException);
|
||||
}
|
||||
|
||||
if (exception is NpgsqlException npgsqlException && npgsqlException.IsTransient)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (exception is TimeoutException or IOException)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (exception is InvalidOperationException invalidOperationException &&
|
||||
invalidOperationException.Message.Contains("transient failure", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return exception.InnerException is not null &&
|
||||
IsTransientDatabaseException(exception.InnerException);
|
||||
}
|
||||
|
||||
private static string? Truncate(string? value, int maxLength)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user