这是Python代码:https://bpa.st/RRQQW
我试图将秘密的创建限制为每秒 50 个,但它似乎没有按预期工作。我研究了 Python 并发性,但不确定它如何适用于我的情况
当处理 AWS Secrets Manager 并需要创建大量密钥,同时遵守每秒 50 个请求的 API 速率限制时,您可以使用 Python 的并发.futures 模块并仔细计时来实现此目的。
伪代码:
FUNCTION create_secret(secret_manager, secret_name, secret_value):
TRY:
Create secret using secret_manager.create_secret()
RETURN success message
CATCH ClientError as e:
IF error is 'ResourceExistsException':
RETURN "Secret already exists" message
ELSE:
RETURN error message
FUNCTION lambda_handler(event, context):
Initialize secret_manager client
Load secrets_data from JSON file
SET total_secrets = number of secrets in secrets_data
SET secrets_created = 0
SET rate_limit = 50 // AWS API rate limit
SET batch_size = 50 // Number of secrets to process in each batch
FUNCTION process_batch(batch):
Initialize empty results list
CREATE ThreadPoolExecutor with max_workers = rate_limit
FOR EACH secret_name, secret_value IN batch:
Submit create_secret task to executor
FOR EACH completed future:
Append result to results list
RETURN results
SET start_time = current time
FOR i = 0 TO total_secrets STEP batch_size:
Create batch of secrets from secrets_data
CALL process_batch(batch)
Update secrets_created count
Print progress
Calculate elapsed_time
IF elapsed_time < 1 second:
Sleep for remaining time to complete 1 second
Reset start_time
RETURN success response with secrets created count