Here is my code
ThreadPool.SetMinThreads(1024, 256);
ServicePointManager.DefaultConnectionLimit = 256;
ServicePointManager.UseNagleAlgorithm = false;
ServicePointManager.Expect100Continue = false;
client.DefaultRequestOptions = new TableRequestOptions
{
MaximumExecutionTime = TimeSpan.FromSeconds(30), //Timeout requests after 30 seconds
RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(3), 4),
LocationMode = LocationMode.PrimaryThenSecondary
};
var tableEntityGroups = histories.Select(h => new TrackHistoryTableEntity(h)).GroupBy(e => e.PartitionKey).ToDictionary(g => g.Key, g => g.ToList());
List<Task> tasks = new List<Task>();
foreach (var kvp in tableEntityGroups)
{
//Merge Track history records with the same FixTaken second into one, taking the average
var mergedHistories = kvp.Value.GroupBy(v => v.RowKey).Select(g => new TrackHistoryTableEntity()
{
PartitionKey = g.First().PartitionKey,
RowKey = g.First().RowKey,
A = g.Select(v => v.A).Average(),
N = g.Select(v => v.N).Average(),
V = g.Select(v => v.V).Average(),
B = g.Select(v => v.B).Average(),
D = g.Select(v => v.D).Sum()
});
TableBatchOperation batchOperation = new TableBatchOperation();
foreach (var v in mergedHistories)
{
batchOperation.Add(TableOperation.InsertOrReplace(v));
if (batchOperation.Count >= 100)
{
tasks.Add(TrackHistoryTable.ExecuteBatchAsync(batchOperation));
batchOperation = new TableBatchOperation();
}
}
if (batchOperation.Count > 0)
{
tasks.Add(TrackHistoryTable.ExecuteBatchAsync(batchOperation));
}
var splitKey = kvp.Value[0].PartitionKey.Split('_');
tasks.Add(TrackHistoryTracksTable.ExecuteAsync(TableOperation.InsertOrReplace(new TableEntity(splitKey[0], Int32.Parse(splitKey[1]).ToString()))));
if (trackPartitionUpdates)
I've got a long-running application that's supposed to insert data every 2/3 seconds or so. The majority of the time, it works perfectly. However, I occasionally receive a time-out exception. Every time I checked, it was inserting roughly 50 records. I checked with a higher load, such as over 2000 rows. It performs admirably. It only throws a timeout exception a few times every day.