Skip to content

Commit

Permalink
down ported double check pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
dei79 committed Jan 7, 2024
1 parent adc3ff0 commit 0764388
Showing 1 changed file with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,33 @@ public static async Task<Response<IReadOnlyList<Response>>> SubmitTransactionWit
// check the exception
if (allowAutoCreate && ex.ErrorCode.Equals("TableNotFound"))
{
// try to create the table
await tc.CreateAsync();
// This is a double check pattern to ensure that two independent processes
// who are trying to create the table in parallel do not end up in an unhandled
// situation.
try
{
// try to create the table
await tc.CreateAsync();
}
catch (TableTransactionFailedException doubleCheckEx)
{
// check if we have an errorCode if not the system throws the exception
// to the caller
if (String.IsNullOrEmpty(doubleCheckEx.ErrorCode))
{
ExceptionDispatchInfo.Capture(ex).Throw();
return null;
}

// Every error except the TableAlreadyExists is thrown to the caller but
// in the case the system is trying to create the table in parallel we
// ignore the error and execute the transaction!
if (!doubleCheckEx.ErrorCode.Equals("TableAlreadyExists"))
{
ExceptionDispatchInfo.Capture(ex).Throw();
return null;
}
}

// retry
return await tc.SubmitTransactionAsync(transactionActions, cancellationToken);
Expand Down

0 comments on commit 0764388

Please sign in to comment.