我有一个由队列消息触发的Azure功能。此函数向第三方API发出请求。不幸的是,这个API有限制 - 每秒10个事务,但我可能在服务总线队列中每秒有10个以上的消息。如何限制Azure函数的调用次数以满足第三方API限制?
答案 0 :(得分:5)
不幸的是,没有内置选项。
限制并发执行的唯一可靠方法是运行固定的应用服务计划(而非消费计划),只有1个实例一直在运行。您将需要为此实例付费。
然后在library(ggplot2)
mydata <- read.csv(
text = "s no,type,state
t1,type1,A
t2,type2,C
t3,type3,A
t4,type1,B
t5,type3,B
t6,type3,B
t7,type3,C
t8,type2,A
t9,type2,C
t10,type2,B")
ggplot(mydata, aes(x = state, fill = type)) +
geom_bar()
文件中设置选项:
host.json
最后,确保您的函数需要一秒钟才能执行(或其他最短持续时间,并相应地调整并发调用)。
正如@SeanFeldman建议的那样,请参阅this answer中的其他一些想法。它是关于存储队列的,但也适用于服务总线。
答案 1 :(得分:0)
您可以尝试编写一些自定义逻辑,即在Azure函数中实现自己的内存中队列,以排队请求并限制对第三方API的调用。无论如何,在第三方API调用成功之前,您不需要确认队列中的消息。这样也保持了可靠性。
答案 2 :(得分:0)
维护系统完整性的最佳方法是限制服务总线消息的消耗。您可以控制QueueClient处理邮件的方式,请参阅:https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues#4-receive-messages-from-the-queue
查看&#34; Max Concurrent来电&#34;
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);
}
答案 3 :(得分:0)
您想要删除在第二个时间间隔内收到的N-10条消息,还是想要处理与API限制相关的每条消息?对于后者,您可以将函数处理的消息添加到另一个队列,您可以通过另一个函数(定时器触发器)每秒读取一批10条消息