This is my Polly implementation, it has two policy, one timeout and one retry. The idea is that when sql time out, the timeout span will become longer, so sql server got more time to do the work.
However, when using a sp that takes minutes to finish to simulate timeout, I do not see the timeout policy fired 3 times(either attach a debugger or just search the output log). It fired once and then TimeoutRejectedException
will be thrown.
var timeoutPerTry = Policy
.TimeoutAsync(context =>
{
////enlarge timeout every time it happens
taskTimeoutInSeconds = (int)(timeoutMs / 1000);
Log.LogVerbose(
$"log something");
return TimeSpan.FromMilliseconds(timeoutMs);
}, TimeoutStrategy.Optimistic);
// retry SqlException up to MaxRetries
var retryPolicy = Policy
.Handle<SqlException>()
.RetryAsync(Constants.MaxRetries,
(response, calculatedWaitDuration, context) =>
{
Log.LogError(
$"Failed dynamic execution attempt. Retrying. {response.Message} - {response.StackTrace}");
});
try
{
////combine timeout policy and retry policy
var combinedPolicy = retryPolicy.WrapAsync(timeoutPerTry);
// ReSharper disable once AccessToDisposedClosure
var results =
await combinedPolicy.ExecuteAsync<IEnumerable<T>>(async () => {
var connectionString = ConnectionStringHelper.GetConnectionString(warehouseId);
using (var connection = new SqlConnection(connectionString)) // assumed no need for using block as closed by caller
{
await connection.OpenAsync();
using (var cmd = new SqlCommand
{
CommandType = commandType,
CommandTimeout = taskTimeoutInSeconds, // in secs
CommandText = "JerrySimulateSlowSp"
})
{
cmd.Parameters.AddRange(parameters.ToArray());
cmd.Connection = connection;
using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection))
{
return mapper.Map<IDataReader, IEnumerable<T>>(reader);
}
}
}
});
return results;
//cmd.Connection = null;
}
catch (SqlException ex) when (ex.Number == -2) // -2 is a sql timeout
{
throw new ThunderTimeoutException(Constants.HttpResponseTimeoutSql);
}
catch (TimeoutRejectedException)
{
throw new ThunderTimeoutException(Constants.HttpResponseTimeoutTask);
}