Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 111 additions & 25 deletions ManagedCode.Repository.AzureTable/AzureTableAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -14,9 +15,10 @@ namespace ManagedCode.Repository.AzureTable
{
public class AzureTableAdapter<T> where T : class, ITableEntity, new()
{
private readonly ILogger _logger;
private readonly bool _allowTableCreation = true;
private readonly CloudStorageAccount _cloudStorageAccount;
private readonly ILogger _logger;
private readonly int _retryCount = 25;
private readonly object _sync = new();
private CloudTableClient _cloudTableClient;
private bool _tableClientInitialized;
Expand Down Expand Up @@ -48,7 +50,7 @@ public Task<bool> DropTable(CancellationToken token)
return table.DeleteIfExistsAsync(token);
}

public CloudTable GetTable()
private CloudTable GetTable()
{
lock (_sync)
{
Expand Down Expand Up @@ -81,15 +83,53 @@ public CloudTable GetTable()
public async Task<T> ExecuteAsync<T>(TableOperation operation, CancellationToken token = default) where T : class
{
var table = GetTable();
var result = await table.ExecuteAsync(operation, token).ConfigureAwait(false);
return result.Result as T;
var retryCount = _retryCount;
do
{
try
{
var result = await table.ExecuteAsync(operation, token).ConfigureAwait(false);
return result.Result as T;
}
catch (StorageException e) when (e.RequestInformation.HttpStatusCode == (int) HttpStatusCode.TooManyRequests)
{
retryCount--;
if (retryCount == 0)
{
throw;
}

await WaitRandom(token);
}
} while (retryCount > 0);

throw new Exception(nameof(HttpStatusCode.TooManyRequests));
}

public async Task<object> ExecuteAsync(TableOperation operation, CancellationToken token = default)
{
var table = GetTable();
var result = await table.ExecuteAsync(operation, token).ConfigureAwait(false);
return result.Result;
var retryCount = _retryCount;
do
{
try
{
var result = await table.ExecuteAsync(operation, token).ConfigureAwait(false);
return result.Result;
}
catch (StorageException e) when (e.RequestInformation.HttpStatusCode == (int) HttpStatusCode.TooManyRequests)
{
retryCount--;
if (retryCount == 0)
{
throw;
}

await WaitRandom(token);
}
} while (retryCount > 0);

throw new Exception(nameof(HttpStatusCode.TooManyRequests));
}

public async Task<int> ExecuteBatchAsync(IEnumerable<TableOperation> operations, CancellationToken token = default)
Expand All @@ -105,32 +145,54 @@ public async Task<int> ExecuteBatchAsync(IEnumerable<TableOperation> operations,
batchOperation.Add(item);
if (batchOperation.Count == BatchSize)
{
try
{
var result = await table.ExecuteBatchAsync(batchOperation, token).ConfigureAwait(false);
totalCount += result.Count;
batchOperation = new TableBatchOperation();
}
catch (Exception e)
var retryCount = _retryCount;
do
{
// skip
}
try
{
var result = await table.ExecuteBatchAsync(batchOperation, token).ConfigureAwait(false);
totalCount += result.Count;
batchOperation = new TableBatchOperation();
break;
}
catch (StorageException e) when (e.RequestInformation.HttpStatusCode == (int) HttpStatusCode.TooManyRequests)
{
retryCount--;
if (retryCount == 0)
{
throw;
}

await WaitRandom(token);
}
} while (retryCount > 0);
}

token.ThrowIfCancellationRequested();
}

if (batchOperation.Count > 0)
{
try
{
var result = await table.ExecuteBatchAsync(batchOperation, token).ConfigureAwait(false);
totalCount += result.Count;
}
catch (Exception e)
var retryCount = _retryCount;
do
{
// skip
}
try
{
var result = await table.ExecuteBatchAsync(batchOperation, token).ConfigureAwait(false);
totalCount += result.Count;
break;
}
catch (StorageException e) when (e.RequestInformation.HttpStatusCode == (int) HttpStatusCode.TooManyRequests)
{
retryCount--;
if (retryCount == 0)
{
throw;
}

await WaitRandom(token);
}
} while (retryCount > 0);
}
}

Expand Down Expand Up @@ -235,8 +297,27 @@ public async IAsyncEnumerable<T> Query<T>(

do
{
var results = await table.ExecuteQuerySegmentedAsync(query, token, cancellationToken)
.ConfigureAwait(false);
TableQuerySegment<T> results = null;
var retryCount = _retryCount;
do
{
try
{
results = await table.ExecuteQuerySegmentedAsync(query, token, cancellationToken)
.ConfigureAwait(false);
break;
}
catch (StorageException e) when (e.RequestInformation.HttpStatusCode == (int) HttpStatusCode.TooManyRequests)
{
retryCount--;
if (retryCount == 0)
{
throw;
}

await WaitRandom(cancellationToken);
}
} while (retryCount > 0);

token = results.ContinuationToken;

Expand Down Expand Up @@ -266,5 +347,10 @@ public async IAsyncEnumerable<T> Query<T>(
}
} while (token != null);
}

private Task WaitRandom(CancellationToken token)
{
return Task.Delay(new Random().Next(750, 3500), token);
}
}
}
Loading