一些背景信息:
我正在使用CodePipeline和两个lambda函数开发用于更新和测试全局重定向的测试套件:包装器函数和测试器函数。
包装器循环遍历域名列表。对于每个域名,它获取关联的S3文件,然后创建一个长列表对象(文件中每行一个对象)。例如,一个对象示例:
redirect = {
'staging': None,
'prod': None,
'country': 'US',
'language': 'EN',
'status_code': None,
'shortlink': None,
'expected': None
}
这个包装器接受对象列表并调用第二个 lambda 函数(测试 lambda)来发送每个对象的一系列 httpx 请求等等。
最重要的是异步处理 - 否则,lambda 将在 15 分钟时限内超时。
实际问题:
这个包装器函数只能触发测试程序10次。否则,当结果返回时,会出现以下错误:
Connection pool is full, discarding connection: lambda.us-east-1.amazonaws.com
我已经将ThreadPoolExecutor的max_workers增加,并为boto3设置了max_workers。 我与AWS支持人员交谈过,我的实际Lambda设置有利于触发我的测试Lambda的多次调用。 然而,我仍然会遇到连接池错误。
相关代码:
from botocore.client import Config
from boto3.session import Session
from concurrent.futures import ThreadPoolExecutor, as_completed
import boto3
max_pool_connections = 30
config = Config(
max_pool_connections=max_pool_connections,
read_timeout = 900
)
def handler(event, context):
try:
events = ... # generated via a bunch of nonsense
with ThreadPoolExecutor(max_pool_connections) as executor:
futures = []
for event in events:
print(event)
future = executor.submit(lambda_client.invoke, FunctionName = "site-tester", InvocationType = "RequestResponse", Payload = json.dumps(event))
futures.append(future)
for index, future in enumerate(as_completed(futures, timeout=None), start=1):
...
这显然不是完整的代码,因为完整的代码是一团混乱。我暂时没有时间编写完整的测试功能,但如果有人有任何初始想法或故障排除提示,将不胜感激。
我想要指出的是,对于那10个被调用并返回的事件,一切都按预期工作。但它只限于10次,而我绝对需要更多次数。
再次强调最重要的是它是异步的-否则,Lambda函数将在15分钟时超时。
并发设置
包装器函数:保留并发设置为30
测试函数:保留并发设置为30