简体   繁体   English

使用AWS Lambda进行长时间轮询

[英]Long polling with AWS Lambda

I'm creating a monitoring system for an external API on which I need to invoke a function if the response is different from the initial request. 我正在为外部API创建一个监视系统,如果响应与初始请求不同,则需要在该系统上调用一个函数。

Currently without any long polling around my functions I've got the following: 目前,我没有对我的功能进行任何长时间的调查,但是我得到了以下内容:

  let gameAPI = new game(
      [
        "email@email.com",
        "password",
        "secret",
        "secret"
      ]
  );


  let lastModified;

  gameAPI.login().then(() => {

      gameAPI.getProfileStats(process.argv[2], process.argv[3], process.argv[4]).then(stats => {
          //first run grab this only
          if(lastModified < stats.data.lastModified) {
              //we found new data, stop polling & update mySQL database.
          } else {
              //continue to run every 1 minute...
          }
      });

  });

Would AWS Lambda be a good solution for this theory? AWS Lambda将是该理论的一个很好的解决方案吗? if so, how would I go about doing it since Lambda charges for computer power usage? 如果是这样,自Lambda收取计算机电源使用费后,我将如何处理?

No definitely not a good idea, in that case you are not letting the Lambda function exit and you are continuously billed. 绝对不是一个好主意,在这种情况下,您不会让Lambda函数退出,并且会不断向您收费。 And on top of that Lambda has maximum execution time of 15min so you will need to somehow trigger it every so often. 最重要的是,Lambda的执行时间最长为15分钟,因此您需要经常触发它。

One viable approach would be still to use Lambda, but instead of continue to run, exit on the else statement, and use CloudWatch trigger to invoke your function every minute, in that way you can run the check every minute, but only charges for the duration your checking function executes. 一种可行的方法仍然是使用Lambda,而不是继续运行,而是在else语句上退出,并使用CloudWatch触发器每分钟调用一次函数,这样您就可以每分钟运行一次检查,但仅对检查功能执行的持续时间。

Edit 编辑

If there are many users to monitor, then depends on the actual number, a simple solution would be have an EC2 with a long living process, and you store Last Checked timestamp in DB. 如果要监视的用户很多,则取决于实际数量,一个简单的解决方案是使EC2具有较长的生存期,然后将Last Checked时间戳存储在DB中。 For each period (say 1min), select the DB for items last updated > 1min ago, and store in a queue within that local process. 对于每个时间段(例如1分钟),请选择上一次更新> 1分钟前的项目的数据库,并将其存储在该本地进程的队列中。 And in the process, at a certain rate (say 1/sec), do your fetch and check logic and update data and Last Checked in DB. 然后,在此过程中,以一定的速率(例如1 /秒)执行获取和检查逻辑,并更新数据和数据库中的“最后检查”。

If you are expecting large volume, you can use SQS + Lambda w/CloudWatch + ELB, with Last Checked timestamp: 如果您希望获得大批量交易,则可以将SQS + Lambda w / CloudWatch + ELB与最近检查的时间戳一起使用:

  1. Every minute, CloudWatch invoke Lambda and select records that requires update and put the IDs into a SQS queue. 每分钟,CloudWatch都会调用Lambda并选择需要更新的记录,并将ID放入SQS队列。
  2. Use ELB worker environment and consume the queue to do the update. 使用ELB工作程序环境并使用队列进行更新。

That way you can have virtually infinitely rate. 这样,您几乎可以获得无限的评价。 Or if you like slightly less complexity you can use simple EC2 instance and manually manage the instances and scale. 或者,如果您希望降低复杂度,则可以使用简单的EC2实例并手动管理实例并进行扩展。

Will be good to have "GAME API" pushing stats to a SQS queue. 拥有“ GAME API”将统计信息推送到SQS队列会很好。

Then consumers will long poll the queue and save the results to wherever you want. 然后,消费者将长期轮询队列并将结果保存到所需的任何位置。 This has several benefits. 这有几个好处。

  1. You get long polling feature out of the box from SQS (No need to implement it) 您可以通过SQS开箱即用地获得长轮询功能(无需实施)
  2. By enabling long polling you reduce the number of requests to your game API thus you also reduce the costs 通过启用长时间轮询,您可以减少对游戏API的请求数量,从而也可以降低成本
  3. You can scale up and down your consumers whenever you want, you'll not run in risk of duplicates because SQS allows setting a visibility timeout for messages 您可以随时按需扩大或缩小消费者,因为SQS允许设置消息的可见性超时,所以您不会冒重复的风险

Saving data into relations DB such as MySQL in your consumer code may lead to unexpected issues such as table locking when you have more than 1 consumer polling the same queue. 在您的使用者代码中将数据保存到关系数据库(如MySQL)中时,可能会导致意外问题,例如,当您有多个使用者在轮询同一队列时,表锁定。 Therefore it is a good Idea to use NoSQL DB for such solutions because they are flexible in terms of scalability. 因此,将NoSQL DB用于此类解决方案是一个好主意,因为它们在可伸缩性方面很灵活。

For example, Dynamo DB has the ability to specify WCU (Write Capacity Units) for tables, thus you save costs and have control over what you provision. 例如,Dynamo DB可以为表指定WCU(写入容量单位),因此可以节省成本并可以控制所提供的内容。

Finally, SQS can be integrated with Lambda function (Async Lambda call), as a consumer, this ensures that you run the function only when it is needed because SQS triggers the function when there is a record to save. 最后,作为使用者,SQS可以与Lambda函数(异步Lambda调用)集成,从而确保仅在需要时才运行该函数,因为SQS在有记录要保存时会触发该函数。

So the answer to your question is yes it is a good idea to use Lambda, but you should use it with SQS queue. 因此,您的问题的答案是肯定的。使用Lambda是一个好主意,但您应该将其与SQS队列一起使用。

I hope this helps. 我希望这有帮助。

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

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