1
0
mirror of synced 2024-12-23 01:33:07 +01:00
Tapeti/Tapeti.Flow.SQL/SqlRetryHelper.cs

61 lines
1.6 KiB
C#
Raw Normal View History

2019-10-10 17:19:28 +02:00
using System;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
2019-10-10 17:19:28 +02:00
namespace Tapeti.Flow.SQL
{
internal class SqlRetryHelper
{
public static readonly TimeSpan[] ExponentialBackoff = {
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(8),
TimeSpan.FromSeconds(13),
TimeSpan.FromSeconds(21),
TimeSpan.FromSeconds(34),
TimeSpan.FromSeconds(55)
};
public static async Task Execute(Func<Task> callback)
{
var retryAttempt = 0;
while (true)
{
try
{
await callback().ConfigureAwait(false);
2019-10-10 17:19:28 +02:00
break;
}
catch (SqlException e)
{
if (SqlExceptionHelper.IsTransientError(e))
{
await Task.Delay(ExponentialBackoff[retryAttempt]).ConfigureAwait(false);
2019-10-10 17:19:28 +02:00
if (retryAttempt < ExponentialBackoff.Length - 1)
retryAttempt++;
}
else
throw;
}
}
}
public static async Task<T> Execute<T>(Func<Task<T>> callback)
{
var returnValue = default(T);
await Execute(async () =>
{
returnValue = await callback().ConfigureAwait(false);
}).ConfigureAwait(false);
2019-10-10 17:19:28 +02:00
return returnValue!;
2019-10-10 17:19:28 +02:00
}
}
}