Skip to content

Commit

Permalink
Retry pooled connection timeout (#243)
Browse files Browse the repository at this point in the history
* Refactor a bit

* Retry pooled connection failures
  • Loading branch information
adam-mccoy authored Feb 2, 2023
1 parent 103aa8c commit c307a0f
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,27 @@ sealed class SqlDatabaseTransientErrorDetectionStrategy : ITransientErrorDetecti

public bool IsTransient(Exception ex)
{
if (ex is TimeoutException) return true;
return ex switch
{
TimeoutException => true,
InvalidOperationException invalidOperationException => IsPooledConnectTimeout(invalidOperationException),
SqlException sqlException => IsTransientSqlException(sqlException),
_ => false
};
}

var sqlException = ex as SqlException;
if (sqlException == null) return false;
static bool IsPooledConnectTimeout(InvalidOperationException exception)
=> exception.Message.Contains("The timeout period elapsed prior to obtaining a connection from the pool.");

static bool IsTransientSqlException(SqlException exception)
{
// If this error was caused by throttling on the server we can augment the exception with more detail
// I don't feel awesome about mutating the exception directly but it seems the most pragmatic way to add value
var sqlErrors = sqlException.Errors.OfType<SqlError>().ToArray();
var sqlErrors = exception.Errors.OfType<SqlError>().ToArray();
var firstThrottlingError = sqlErrors.FirstOrDefault(x => x.Number == ThrottlingCondition.ThrottlingErrorNumber);
if (firstThrottlingError != null)
{
AugmentSqlExceptionWithThrottlingDetails(firstThrottlingError, sqlException);
AugmentSqlExceptionWithThrottlingDetails(firstThrottlingError, exception);
return true;
}

Expand Down

0 comments on commit c307a0f

Please sign in to comment.