简体   繁体   English

Azure功能:限制每秒呼叫次数

[英]Azure function: limit the number of calls per second

I have an Azure function triggered by queue messages. 我有一个由队列消息触发的Azure函数。 This function makes a request to third-party API. 该函数向第三方API发出请求。 Unfortunately this API has limit - 10 transactions per second, but I might have more than 10 messages per second in service bus queue. 不幸的是,此API有限制-每秒10个事务,但是我可能每秒在服务总线队列中有10条以上的消息。 How can I limit the number of calls of Azure function to satisfy third-party API limitations? 如何限制Azure函数的调用次数以满足第三方API的限制?

Unfortunately there is no built-in option for this. 不幸的是,没有内置选项。

The only reliable way to limit concurrent executions would be to run on a fixed App Service Plan (not Consumption Plan) with just 1 instance running all the time. 限制并发执行的唯一可靠方法是在固定的App Service Plan(而不是Consumption Plan)上运行,并且始终仅运行一个实例。 You will have to pay for this instance. 您必须为此实例付费。

Then set the option in host.json file: 然后在host.json文件中设置选项:

"serviceBus": {
    // The maximum number of concurrent calls to the callback the message
    // pump should initiate. The default is 16.
    "maxConcurrentCalls": 10
}

Finally, make sure your function takes a second to execute (or other minimal duration, and adjust concurrent calls accordingly). 最后,请确保您的函数需要花费一秒钟的时间执行(或其他最短的持续时间,并相应地调整并发调用)。

As @SeanFeldman suggested, see some other ideas in this answer . 正如@SeanFeldman所建议的那样,请在此答案中查看其他一些想法。 It's about Storage Queues, but applies to Service Bus too. 它与存储队列有关,但也适用于服务总线。

You can try writing some custom logic ie implement your own in-memory queue in Azure function to queue up requests and limit the calls to third party API. 您可以尝试编写一些自定义逻辑,即在Azure函数中实现自己的内存队列,以将请求排队并限制对第三方API的调用。 Anyway until the call to third party API succeeds, you dont need to acknowledge the messages in the queue. 无论如何,直到对第三方API的调用成功,您无需确认队列中的消息。 In this way reliability is also maintained. 这样也保持了可靠性。

The best way to maintain integrity of the system is to throttle the consumption of the Service Bus messages. 维护系统完整性的最佳方法是限制服务总线消息的消耗。 You can control how your QueueClient processes the messages, see: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues#4-receive-messages-from-the-queue 您可以控制QueueClient处理消息的方式,请参阅: https ://docs.microsoft.com/zh-cn/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues#4- 从队列接收消息

Check out the "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);
}

Do you want to get rid of N-10 messages you receive in a second interval or do you want to treat every message in respect to the API throttling limit? 您是要摆脱第二个间隔中收到的N-10条消息,还是要处理与API节流限制有关的每条消息? For the latter, you can add the messages processed by your function to another queue from which you can read a batch of 10 messages via another function (timer trigger) every second 对于后者,您可以将函数处理的消息添加到另一个队列,您可以每秒通过另一个函数(计时器触发器)从中读取一批10条消息

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM