简体   繁体   English

从 Slack 调用 Lambda function 时超时

[英]Timeout when calling Lambda function from Slack

I have this code associated with slash command followed this article and slash commands are working.我在这篇文章之后有与斜杠命令相关联的代码,并且斜杠命令正在工作。

If there a delay of more than 3 seconds there is a timeout error, how can I avoid this?如果延迟超过 3 秒出现超时错误,我该如何避免这种情况?

import json
from urllib import parse as urlparse
import base64
from functools import lru_cache
import math

@lru_cache(maxsize=60)
def isPrime(i):
    if (i in [2,3]):  # shortcut low primes
        return True
    else:
        if (i % 2 == 0 or i % 3 == 0):  # special since we go 3-> sqrt
            return False
        sqrt = int(math.sqrt(i) // 1)
        for s in range(3,sqrt+1,2):  # check odd vals, or all prior primes + new primes
            if (i % s == 0):
                return False
        return True

commands = {'isprime':isPrime,'prime':isPrime }   # map of command aliases

def lambda_handler(event, context):
    msg_map = dict(urlparse.parse_qsl(base64.b64decode(str(event['body'])).decode('ascii')))  # data comes b64 and also urlencoded name=value& pairs
    command = msg_map.get('command','err')  # will be /command name
    params = msg_map.get('text','err').split(" ")  # params ['isPrime','50']
    subcommand = params[0].lower()
    if (len(params) < 2):
        response = f'available subcommands: {list(commands.keys())} + 1 parameter'
    elif (subcommand in commands.keys()):
        response = f'{subcommand} needs an numeric param' if len(params) < 2 else f'{subcommand} = {commands[subcommand](int(float(params[1])))}'
    else:
        response = f'illegal sub command >{subcommand}<, commands available {list(commands.keys())}'

    # logging
    print (str(command) + ' ' + str(params) +' -> '+ response + ',original: '+ str(msg_map))

    return  {
        "response_type": "in_channel",
        "text": command + ' ' + " ".join(params),
        "attachments": [
            {
                "text": response
            }
        ]
    }

How to add 5 mins wait for the response.如何添加 5 分钟等待响应。 Slack is giving timeout after three seconds Slack 在三秒后超时

failed with the error "operation_timeout"

Update更新

This is not related to a Lambda timeout.这与 Lambda 超时无关。 I've increased it and upon further research it seems that there is a hard limit of 3000 ms for the response time imposed by slack as described in the answer to this question我已经增加了它,经过进一步研究,似乎有一个 3000 毫秒的硬限制,因为松弛强加的响应时间在这个问题的答案中有所描述

You're probably running into a Lambda timeout.您可能遇到了 Lambda 超时。 AWS Lambda runs your code in response to an event and for a maximum of 900 seconds / 15 minutes. AWS Lambda 运行您的代码以响应事件,最长运行时间为 900 秒/15 分钟。 This limit can be configured as described in the docs and is 3 seconds by default .此限制可以按照文档中的说明进行配置,默认为 3 秒

Solution: Increase the Lambda functions timeout value.解决方案:增加 Lambda 函数超时值。


Update更新

Since you have been able to eliminate the Lambda timeout being an issue and pointed to this question which says there is a hard limit of 3000ms for responses, we've got a different problem:由于您已经能够消除 Lambda 超时是一个问题,并指出这个问题说响应的硬限制为 3000 毫秒,所以我们遇到了一个不同的问题:

The code is to slow.代码很慢。

Checking for prime numbers is a compute intensive process and if it needs to be completed within a short period of time, you can throw money at the problem (CPU horsepower).检查素数是一个计算密集型过程,如果需要在短时间内完成,您可以花钱解决问题(CPU 马力)。 In Lambda this means increasing the memory capacity of the function, because in Lambda CPU, RAM and Networking performance scale with the allocated memory. In Lambda this means increasing the memory capacity of the function, because in Lambda CPU, RAM and Networking performance scale with the allocated memory.

A lambda function with a Memory Setting of 1280 MB should give you roughly a whole vCPU, which is sort of the upper limit for your single threaded implementation.一个 lambda function 和一个 Memory 设置为1280 MB应该给你大约一个完整的 vCPU,这是你的单线程实现的上限。 You could try that next.你可以试试下一个。

If that doesn't work, you need a more efficient implementation of the isPrime algorithm.如果这不起作用,您需要更有效地实现isPrime算法。

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

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