简体   繁体   English

在 C# 中使用 azure 服务总线的指数退避

[英]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.除此之外,您应在提出请求之前而不是在此过程中制定政策。

For retry policies, you can also use an external library like Polly .对于重试策略,您还可以使用Polly等外部库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用服务总线作为触发器的 C# Azure Function 4.x 中实现指数重试的最佳方法是什么? - What is the best way to implement exponential retry in C# Azure Function 4.x with a Service Bus as a trigger? 如何使用 C# 以编程方式与 Azure 服务总线通信 - How to Communicate with Azure Service Bus Programmatically using C# c#使用Azure Service Bus对内部(防火墙后)的服务进行http调用 - c# make http call to service on premise (behind firewall) using Azure Service Bus 如何在 Azure Functions 中实现指数退避? - How to implement exponential backoff in Azure Functions? 调用QueueClient.CreateFromConnectionString时使用C#的Azure服务总线队列:BadImageFormatException - Azure Service bus Queue using C# : BadImageFormatException when calling QueueClient.CreateFromConnectionString 在C#中使用SAS密钥获取Azure Service Bus描述 - Get Azure Service Bus description with SAS key in c# Azure服务总线序列化问题[C#/ JAVA] - Azure Service Bus Serialization Issues [C# / JAVA] C#Azure服务总线队列OnMessage回调 - C# Azure Service Bus Queue OnMessage Callback 如何通过代理连接到 Azure 服务总线主题 - C#? - How to connect to Azure Service Bus Topic through proxy - C#? C# - 天蓝色函数服务总线消息接受一条消息作为 ServiceBusReceivedMessage - C# - azure function service bus message accept a message as ServiceBusReceivedMessage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM