簡體   English   中英

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

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

我有一個由隊列消息觸發的Azure函數。 該函數向第三方API發出請求。 不幸的是,此API有限制-每秒10個事務,但是我可能每秒在服務總線隊列中有10條以上的消息。 如何限制Azure函數的調用次數以滿足第三方API的限制?

不幸的是,沒有內置選項。

限制並發執行的唯一可靠方法是在固定的App Service Plan(而不是Consumption Plan)上運行,並且始終僅運行一個實例。 您必須為此實例付費。

然后在host.json文件中設置選項:

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

最后,請確保您的函數需要花費一秒鍾的時間執行(或其他最短的持續時間,並相應地調整並發調用)。

正如@SeanFeldman所建議的那樣,請在此答案中查看其他一些想法。 它與存儲隊列有關,但也適用於服務總線。

您可以嘗試編寫一些自定義邏輯,即在Azure函數中實現自己的內存隊列,以將請求排隊並限制對第三方API的調用。 無論如何,直到對第三方API的調用成功,您無需確認隊列中的消息。 這樣也保持了可靠性。

維護系統完整性的最佳方法是限制服務總線消息的消耗。 您可以控制QueueClient處理消息的方式,請參閱: https ://docs.microsoft.com/zh-cn/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues#4- 從隊列接收消息

查看“最大同時通話次數”

 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條消息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM