Azure 函数:限制每秒调用次数
Azure function: limit the number of calls per second
我有一个由队列消息触发的 Azure 函数。此函数向第三方 API 发出请求。不幸的是,这个 API 有限制 - 每秒 10 个事务,但我可能在服务总线队列中每秒有超过 10 条消息。如何限制 Azure 函数的调用次数以满足第三方 API 限制?
您可以尝试编写一些自定义逻辑,即在 Azure 函数中实现您自己的 in-memory 队列来排队请求并限制对第三方的调用 API。无论如何,在调用第三方 API 成功之前,您不需要确认队列中的消息。这样也可以保持可靠性。
遗憾的是没有 built-in 选项。
限制并发执行的唯一可靠方法是 运行 固定应用服务计划(而非消费计划),始终只有 1 个实例 运行ning。您必须为此实例付费。
然后在host.json
文件中设置选项:
"serviceBus": {
// The maximum number of concurrent calls to the callback the message
// pump should initiate. The default is 16.
"maxConcurrentCalls": 10
}
最后,确保您的函数需要一秒钟的时间来执行(或其他最短持续时间,并相应地调整并发调用)。
正如@SeanFeldman 所建议的,请参阅 中的一些其他想法。它是关于存储队列的,但也适用于服务总线。
保持系统完整性的最佳方法是限制服务总线消息的消耗。您可以控制 QueueClient 如何处理消息,请参阅:https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues#4-receive-messages-from-the-queue
查看 "Max Concurrent calls"
static void RegisterOnMessageHandlerAndReceiveMessages()
{
// Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc.
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
// Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity.
// Set it according to how many messages the application wants to process in parallel.
MaxConcurrentCalls = 1,
// Indicates whether the message pump should automatically complete the messages after returning from user callback.
// False below indicates the complete operation is handled by the user callback as in ProcessMessagesAsync().
AutoComplete = false
};
// Register the function that processes messages.
queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}
您是要删除在第二个时间间隔内收到的 N-10 条消息,还是要根据 API 限制来处理每条消息?对于后者,您可以将您的函数处理的消息添加到另一个队列,您可以每秒通过另一个函数(计时器触发器)从中读取一批 10 条消息
我有一个由队列消息触发的 Azure 函数。此函数向第三方 API 发出请求。不幸的是,这个 API 有限制 - 每秒 10 个事务,但我可能在服务总线队列中每秒有超过 10 条消息。如何限制 Azure 函数的调用次数以满足第三方 API 限制?
您可以尝试编写一些自定义逻辑,即在 Azure 函数中实现您自己的 in-memory 队列来排队请求并限制对第三方的调用 API。无论如何,在调用第三方 API 成功之前,您不需要确认队列中的消息。这样也可以保持可靠性。
遗憾的是没有 built-in 选项。
限制并发执行的唯一可靠方法是 运行 固定应用服务计划(而非消费计划),始终只有 1 个实例 运行ning。您必须为此实例付费。
然后在host.json
文件中设置选项:
"serviceBus": {
// The maximum number of concurrent calls to the callback the message
// pump should initiate. The default is 16.
"maxConcurrentCalls": 10
}
最后,确保您的函数需要一秒钟的时间来执行(或其他最短持续时间,并相应地调整并发调用)。
正如@SeanFeldman 所建议的,请参阅
保持系统完整性的最佳方法是限制服务总线消息的消耗。您可以控制 QueueClient 如何处理消息,请参阅:https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues#4-receive-messages-from-the-queue
查看 "Max Concurrent calls"
static void RegisterOnMessageHandlerAndReceiveMessages()
{
// Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc.
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
// Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity.
// Set it according to how many messages the application wants to process in parallel.
MaxConcurrentCalls = 1,
// Indicates whether the message pump should automatically complete the messages after returning from user callback.
// False below indicates the complete operation is handled by the user callback as in ProcessMessagesAsync().
AutoComplete = false
};
// Register the function that processes messages.
queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}
您是要删除在第二个时间间隔内收到的 N-10 条消息,还是要根据 API 限制来处理每条消息?对于后者,您可以将您的函数处理的消息添加到另一个队列,您可以每秒通过另一个函数(计时器触发器)从中读取一批 10 条消息