[英]how to limit the number of lambda function calls per minute?
Im using a AWS Lambda function receving many unexpected signals from several channels and sending request to another API.我正在使用 AWS Lambda function 从多个通道接收到许多意外信号并将请求发送到另一个 API。
the problem is the another API is limiting the number of requests per minute.(if the numer exceed the limit, IP will be banned)问题是另一个API限制了每分钟请求的数量。(如果数量超过限制,IP将被禁止)
So I want to limit the number of requests per minute sent by the lambda function.所以我想限制lambda function每分钟发送的请求数。
[What I have tried]: [我尝试过的]:
At first, I tried to use 'Concurrency limit', which turns out that it cannot limit the number of call 'per minute'.起初,我尝试使用“并发限制”,结果发现它不能限制“每分钟”的调用次数。
Secondly, I tried to get the current number of requests per minute from the another API before actual requests and if the number of requests exceed the limit, then sleep 1minute and send actual requests.其次,我尝试在实际请求之前从另一个 API 获取当前每分钟的请求数,如果请求数超过限制,则休眠 1 分钟并发送实际请求。
However, getting the current number of requests per minute requires 1 request quarter also.但是,获取当前每分钟的请求数也需要 1 个请求季度。 So, if too many signals call the lambda function in one minute, it can cause IP ban as well.
所以,如果一分钟内有太多信号呼叫lambda function,也会导致IP被ban。
Please help me to solve this problem, I will really appreciate your help.请帮助我解决这个问题,我将非常感谢你的帮助。 Thanks!
谢谢!
Many people ask this question, but there is no simple answer.很多人问这个问题,但没有简单的答案。
It requires rethinking your architecture to determine how to receive, queue and make API calls.它需要重新考虑您的体系结构以确定如何接收、排队和拨打 API 呼叫。 For example, if the limit has been exceeded, what should happen to new requests coming in -- should they be rejected , should they simply wait (not a good idea with AWS Lambda functions since they accumulate cost with no benefit), or should the messages go into a queue for later handling?
例如,如果超过了限制,新请求进来应该怎么办——他们应该被拒绝,他们应该只是等待(AWS Lambda 函数不是一个好主意,因为它们会累积成本而没有好处),或者应该将消息 go 放入队列中以供以后处理?
Can the processes calling your function gracefully handle a "Too many requests" error?调用您的 function 的进程能否妥善处理“请求过多”错误? Can they asynchronously make a request and later poll for a response?
他们可以异步发出请求然后轮询响应吗? Figuring out this process is actually harder and more important that implementing the 'per minute' limit.
弄清楚这个过程实际上比实施“每分钟”限制更难也更重要。
Reducing the concurrency limit is a good start.降低并发限制是一个好的开始。 If possible, reduce it to 1 concurrent Lambda function so that you don't need to deal with parallel functions.
如果可能,将它减少到 1 个并发 Lambda function这样就不需要处理并行函数了。 Then, the function could simply maintain a global variable containing the times of the previous calls.
然后,function 可以简单地维护一个包含先前调用时间的全局变量。 When invoked, the function can check the list of times to count how many were made in the last period.
调用时,function 可以检查时间列表以计算上一周期的次数。 If there are too many, then either wait or reject the request.
如果太多,则等待或拒绝请求。 This is made much more complex if the concurrency is higher than 1 since this information will need to be shared.
如果并发性高于 1,这会变得更加复杂,因为需要共享此信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.