gpt4 book ai didi

Azure表存储: track retries

转载 作者:行者123 更新时间:2023-12-05 00:29:19 25 4
gpt4 key购买 nike

我正在使用微软的标准示例将新实体插入到表中。有没有办法跟踪是否执行了重试?

代码:

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("people");
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
TableOperation insertOperation = TableOperation.Insert(customer1);
table.Execute(insertOperation);

使用 TransientFaultHandlingFramework 很容易做到:

var retryPol = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(retryStrategy); 
retryPol.Retrying += (obj, eventArgs) =>
{
var msg = String.Format("Retrying, CurrentRetryCount = {0} , Delay = {1}, Exception = {2}", eventArgs.CurrentRetryCount, eventArgs.Delay, eventArgs.LastException.Message);
System.Diagnostics.Debug.WriteLine(msg);
};

最佳答案

您可以使用Enterprise Library Transient Fault Handling Application BlockRob's answer 中所述:

var retryPol = new RetryPolicy<StorageTransientErrorDetectionStrategy>(retryStrategy); 
retryPol.Retrying += (obj, eventArgs) =>
{
var msg = String.Format("Retrying, CurrentRetryCount = {0} , Delay = {1}, Exception = {2}", eventArgs.CurrentRetryCount, eventArgs.Delay, eventArgs.LastException.Message);
System.Diagnostics.Debug.WriteLine(msg);
};

var options = new TableRequestOptions { RetryPolicy = new RetryPolicies.NoRetry() };

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("people");
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
TableOperation insertOperation = TableOperation.Insert(customer1);

retryPol.ExecuteAction<TableResult>(() => { return table.Execute(insertOperation, options); });

如果您更愿意单独使用 Windows Azure 存储客户端库,则可以创建引发事件的自定义重试策略,如下所示:

public class EventExponentialRetry : IRetryPolicy
{
private static readonly TimeSpan DefaultClientBackoff = TimeSpan.FromSeconds(4.0);
private const int DefaultClientRetryCount = 3;
private TimeSpan deltaBackoff;
private int maximumAttempts;
private ExponentialRetry retry;

public event EventHandler<RetryEventArgs> RaiseRetryEvent;

public EventExponentialRetry()
{
Initialize(DefaultClientBackoff, DefaultClientRetryCount);
}

public EventExponentialRetry(TimeSpan deltaBackoff, int maxAttempts)
{
Initialize(deltaBackoff, maxAttempts);
}

private void Initialize(TimeSpan deltaBackoff, int maxAttempts)
{
this.deltaBackoff = deltaBackoff;
this.maximumAttempts = maxAttempts;
retry = new ExponentialRetry(this.deltaBackoff, this.maximumAttempts);
}

public IRetryPolicy CreateInstance()
{
EventExponentialRetry newInstance = new EventExponentialRetry(this.deltaBackoff, this.maximumAttempts);
newInstance.RaiseRetryEvent = this.RaiseRetryEvent;
return newInstance;
}

public bool ShouldRetry(int currentRetryCount, int statusCode, Exception lastException, out TimeSpan retryInterval, OperationContext operationContext)
{
bool shouldRetry = retry.ShouldRetry(currentRetryCount, statusCode, lastException, out retryInterval, operationContext);
if (shouldRetry)
{
OnRaiseRetryEvent(new RetryEventArgs(currentRetryCount, statusCode, lastException, retryInterval, operationContext));
}
return shouldRetry;
}

protected virtual void OnRaiseRetryEvent(RetryEventArgs e)
{
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event is raised.
EventHandler<RetryEventArgs> handler = RaiseRetryEvent;

// Event will be null if there are no subscribers.
if (handler != null)
{
// Use the () operator to raise the event.
handler(this, e);
}
}
}

请参阅CustomAzureStorageRetryPolicySample GitHub project获取完整示例。

关于Azure表存储: track retries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17472803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com