0

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?

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. Slack is giving timeout after three seconds

failed with the error "operation_timeout"

Update

This is not related to a Lambda timeout. 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

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

1 Answers1

0

You're probably running into a Lambda timeout. AWS Lambda runs your code in response to an event and for a maximum of 900 seconds / 15 minutes. This limit can be configured as described in the docs and is 3 seconds by default.

Solution: Increase the Lambda functions timeout value.


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:

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). 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. You could try that next.

If that doesn't work, you need a more efficient implementation of the isPrime algorithm.

Maurice
  • 11,482
  • 2
  • 25
  • 45
  • No not lambda timeout –  Feb 10 '21 at 16:51
  • Please check this question https://stackoverflow.com/questions/34896954/how-to-avoid-slack-command-timeout-error –  Feb 10 '21 at 16:52
  • So you already know why you get the timeout error? That might be information worth including in the question :) – Maurice Feb 10 '21 at 16:54
  • After reading your solution , i have tried increasing the lambda function values. still got the error. Will add this in my question. –  Feb 10 '21 at 16:56
  • I've also updated my answer to incorporate the new information. – Maurice Feb 10 '21 at 17:02
  • can we sent a random response within 3000 ms and wait for it ? –  Feb 10 '21 at 17:21
  • I'm not sure if I understand what you're saying. If the question is, can we return anything within 3000ms for testing, then yes. – Maurice Feb 10 '21 at 17:26
  • As slack is waiting for response within 3000 ms. if we can send initial response witihin 3000 ms and try sending another response after processing the data –  Feb 10 '21 at 17:29
  • That's not how Lambda works, once you send the response, the function ends. I don't know what your use case is, but within 3000ms you can calculate a few prime numbers with an efficient implementation. – Maurice Feb 10 '21 at 17:31