TooManyRequestsException:超出速率。在调用 codnito-idp admin_get_user 函数时
TooManyRequestsException: Rate exceeded. while calling codnito-idp admin_get_user function
我在 Python Lambda 函数中使用 AWS sdk 来获取 Cognito 用户的属性。喜欢这个代码:
client = boto3.client('cognito-idp')
try:
response = client.admin_get_user(
UserPoolId=pool_id,
Username=email
)
if response is not None and 'Username' in response and 'UserAttributes' in response:
res = response['UserAttributes']
return res
else:
return None
except ClientError as e:
if e.response['Error']['Code'] == 'UserNotFoundException':
print("USER NOT FOUND")
return None
raise e
return None
当我尝试使用 AdminGetUser API 获取多个用户(大约 50 个用户)时,出现 TooManyRequestsException 错误。
根据 AWS Doc,每秒从 Cognito 获取用户信息的默认限制是多少?我看到这个页面 Quotas in Amazon Cognito 但我看不到这个默认限制是什么。
处理此问题的最佳方法是什么?
现在我打算等待 1 秒,然后我将尝试从同一用户获取用户的属性,同时我会遇到相同的异常。
谢谢!
根据您提到的配额页面,限制是 5/秒。
Soft Limits in Amazon Cognito User Pools APIs
...
Admin APIs not listed above. 5
您需要限制您的请求,但最好的方法取决于您解决方案的其他方面。
举几个例子:
如果您的 lambda 函数被调用 50 次,每个用户调用一次,您可以为您的 lambda 设置最大并发数。 (您可以找到更多信息 here and here )。
如果单个 lambda 调用一次迭代所有 50 个项目,那么您可以在每次迭代中添加一个 time.sleep
调用,或者使用一些速率限制库(这个似乎可以完成任务:https://pypi.org/project/ratelimit/).在这种情况下,除了这种方法之外,您可能希望将 lambda 的并发性限制为 1 。
我在 Python Lambda 函数中使用 AWS sdk 来获取 Cognito 用户的属性。喜欢这个代码:
client = boto3.client('cognito-idp')
try:
response = client.admin_get_user(
UserPoolId=pool_id,
Username=email
)
if response is not None and 'Username' in response and 'UserAttributes' in response:
res = response['UserAttributes']
return res
else:
return None
except ClientError as e:
if e.response['Error']['Code'] == 'UserNotFoundException':
print("USER NOT FOUND")
return None
raise e
return None
当我尝试使用 AdminGetUser API 获取多个用户(大约 50 个用户)时,出现 TooManyRequestsException 错误。
根据 AWS Doc,每秒从 Cognito 获取用户信息的默认限制是多少?我看到这个页面 Quotas in Amazon Cognito 但我看不到这个默认限制是什么。
处理此问题的最佳方法是什么?
现在我打算等待 1 秒,然后我将尝试从同一用户获取用户的属性,同时我会遇到相同的异常。
谢谢!
根据您提到的配额页面,限制是 5/秒。
Soft Limits in Amazon Cognito User Pools APIs
...
Admin APIs not listed above. 5
您需要限制您的请求,但最好的方法取决于您解决方案的其他方面。
举几个例子:
如果您的 lambda 函数被调用 50 次,每个用户调用一次,您可以为您的 lambda 设置最大并发数。 (您可以找到更多信息 here and here )。
如果单个 lambda 调用一次迭代所有 50 个项目,那么您可以在每次迭代中添加一个 time.sleep
调用,或者使用一些速率限制库(这个似乎可以完成任务:https://pypi.org/project/ratelimit/).在这种情况下,除了这种方法之外,您可能希望将 lambda 的并发性限制为 1 。