dongzhao5970 2019-06-13 05:40
浏览 52
已采纳

lambda处理程序回复后如何等待goroutine完成

I'm writing a slack bot with Go and Aws Lambda. Slack requires for the bot to reply within 3 seconds. However, sometimes I can't make it reply that fast, cuz it's "talking" to other serverless applications for requesting some data or dispatching tasks. I have never worked with goroutines before, but I was hoping that I could implement something like this:

  1. Lambda receives a request
  2. The bot creates a goroutine that will process this request and act accordingly on it
  3. The handler doesn't wait for all these actions to complete but replies right away with 200.
  4. Lambda continues to run until goroutine is finished.

I'm not sure if that's even possible.

I've read about sync.WaitGroup, but I'm not sure how to incorporate it together with main function. Should I use it inside the handler? But I need to return response and that's not a function that I can wrap into a goroutine.

Ideally, I would like for handler to reply right away and then process goroutine in the background.

  • 写回答

1条回答 默认 最新

  • dongpo8702 2019-06-13 09:15
    关注

    Don't try to do anything in your lambda handler after the request finishes.

    A more reliable approach:

    1. Accept the call and record whatever input data is needed.
    2. Put the data in SQS
    3. Respond with HTTP 200
    4. Another (SQS triggered) function does the processing and if needed, calls Slack back on recorded response_url
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址