[英]Exponential backoff using azure service bus in C#
I am trying to implement exponential backoff with azure service bus.我正在尝试使用 azure 服务总线实现指数退避。 Basically i have a catch block and if any error currently what i am doing is i am asking it to retry after every 1 second and i am awaiting that.
基本上我有一个catch块,如果我目前正在做的任何错误是我要求它每1秒重试一次,我正在等待。
Goal: I want to use exponential delay.目标:我想使用指数延迟。 So basically after each retry i want exponentially increase the seconds and i dont want it to wait.
所以基本上在每次重试之后,我希望以指数方式增加秒数,我不希望它等待。 Till then it can process other messages.
到那时它可以处理其他消息。
Current catch block looks like:当前的 catch 块如下所示:
catch (Exception ex)
{
_logger.Error(ex, $"Failed to process request {requestId}");
totalAttempts++;
if (totalAttempts == MaxAttempts)
return new Response { Error = ex.ToString() };
await Task.Delay(TimeSpan.FromSeconds(1));
}
I tried the below but it is not exponentially increasing the time.我尝试了以下方法,但时间并没有成倍增加。 I am using this for the first time.
我第一次使用这个。 while (true)
而(真)
try
{
executemethod();
}
catch (Exception ex)
{
_logger.Error(ex, $"Failed to process request {requestId}");
totalAttempts++;
if (totalAttempts == MaxAttempts)
return new Response { Error = ex.ToString() };
queueClient.RetryPolicy = new RetryExponential(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 10);
}
I am not very much sure if i am doing correct.我不太确定我是否做对了。
Goal: need to retry with a back-off strategy and make the processing thread available for other messages while waiting for the next retry.Consider exponential back-off with dead-lettering after reaching max delivery attempts.目标:需要使用回退策略重试,并在等待下一次重试时使处理线程可用于其他消息。在达到最大传递尝试后考虑使用死信的指数回退。
Many Azure-oriented .NET libraries implement Retry internally.许多面向 Azure 的 .NET 库在内部实现了重试。 Service Bus client has it built-in as well.
服务总线客户端也内置了它。 Check out Retry guidance for Azure services .
查看Azure 服务的重试指南。 As per documentation:
根据文档:
When using the built-in RetryExponential implementation, do not implement a fallback operation as the policy reacts to Server Busy exceptions and automatically switches to an appropriate retry mode.
使用内置 RetryExponential 实现时,不要实现回退操作,因为该策略会对 Server Busy 异常做出反应并自动切换到适当的重试模式。
Other from that, you shall set policy before you make a request and not in the process.除此之外,您应在提出请求之前而不是在此过程中制定政策。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.