Paw*_*aga 5 .net c# azure azure-functions serverless
How to implement exponential backoff in Azure Functions?
I have a function that depends on external API. I would like to handle the unavailability of this service using the retry policy. This function is triggered when a new message appears in the queue and in this case, this policy is turned on by default:
For most triggers, there is no built-in retry when errors occur during function execution. The two triggers that have retry support are Azure Queue storage and Azure Blob storage. By default, these triggers are retried up to five times. After the fifth retry, both triggers write a message to a special poison queue.
Unfortunately, the retry starts immediately after the exception (TimeSpan.Zero), and this is pointless in this case, because the service is most likely still unavailable. Is there a way to dynamically modify the time the message is again available in the queue?
我知道我可以设置visibilityTimeout(host.json 参考),但它是为所有队列设置的,这不是我想在这里实现的。
我找到了一种解决方法,但这远非理想的解决方案。如果出现异常,我们可以再次将消息添加到队列中并为此消息设置可见性超时:
[FunctionName("Test")]
public static async Task Run([QueueTrigger("queue-test")]string myQueueItem, TraceWriter log,
ExecutionContext context, [Queue("queue-test")] CloudQueue outputQueue)
{
if (true)
{
log.Error("Error message");
await outputQueue.AddMessageAsync(new CloudQueueMessage(myQueueItem), TimeSpan.FromDays(7),
TimeSpan.FromMinutes(1), // <-- visibilityTimeout
null, null).ConfigureAwait(false);
return;
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个解决方案很弱,因为它没有上下文(我不知道它是哪种尝试,因此我不能限制调用次数和修改时间(指数退避))。
内部重试策略也不受欢迎,因为它会大大增加成本(定价模型)。
小智 0
要考虑的一种选择是让您的函数调用一个逻辑应用程序,该应用程序将延迟设置为您所需的时间,然后在延迟后再次调用该函数。您还可以使用一些持久存储来向逻辑应用添加其他重试逻辑(例如尝试次数)来记录您的尝试。仅当存在连接问题时,您才会调用逻辑应用程序。
或者,您可以将流程起点转移到逻辑应用程序,因为它也可以触发(认为绑定)队列消息。在任何一种情况下,逻辑应用都添加了暂停和重新调用功能和/或流程的功能。
| 归档时间: |
|
| 查看次数: |
1805 次 |
| 最近记录: |