我想对 Amazon Simple Notification Service (Amazon SNS) 中的 "ThrottlingException Rate exceeded" 错误进行故障排除。
简短描述
如果 Amazon SNS API 请求超过 API 节流配额,则 Amazon SNS 会返回与以下内容类似的 "ThrottlingException Rate exceeded" 错误:
"An error occurred (ThrottlingException) when calling the API operation: Rate exceeded"
**注意:**Amazon SNS 会对 API 调用进行节流以提高服务性能。有关 Amazon SNS API 节流配额的更多信息,请参阅 Amazon Simple Notification Service 端点和配额。
解决方法
找出受到节流的 API
如果速率限制超过 API 调用的分配限制,就会收到错误消息。查看错误消息或错误堆栈,找出受到节流的 API 操作。
使用 AWS CloudTrail 日志查看 API 调用的事件。支持的事件会记录在 CloudTrail 事件中,其他 AWS 服务事件也记录在事件历史记录中。有关详细信息,请参阅使用 AWS CloudTrail 记录 AWS SNS API 调用。
您还可以查看 Amazon SNS 的 Amazon CloudWatch 指标。使用 Amazon SNS 使用情况指标来确定已达到速率限制的 SNS API 调用。
查看您的 Amazon SNS API 节流配额
Amazon SNS API 节流配额因 AWS 区域而异。每秒消息配额基于发布到某个 SNS 区域的消息数量,以及 Publish 和 PublishBatch API 请求。如果超出您所在区域的每秒消息配额,则可能会收到节流错误。有关更多信息,请参阅 Amazon SNS API 节流。
**注意:**启用 CloudTrail 日志后,SNS Publish 和 PublishBatch API 请求会记录到 CloudTrail。但是,CloudTrail 不会记录 SNS 资源类型 AWS::SNS::PhoneNumber。
使用指数回退和重试
为避免节流错误,最佳做法是在进行 API 调用时使用指数回退和重试来错开时间间隔。确保在一段时间内分配您的 API 调用。随机安排操作,使其随时间推移而分散。有关详细信息,请参阅指数回退和抖动以及重试行为。
使用以下高级示例伪代码进行指数回退和重试:
MakeSDKRequest() { attempts = 0
loop {
GetSendToken()
response = SNSAPIRequest()
RequestBookkeeping(response)
if not Retryable(response)
return response
attempts += 1
if attempts >= MAX_ATTEMPTS:
return response
if not HasRetryQuota(response)
return response
delay = ExponentialBackoff(attempts)
sleep(delay)
}
}
申请增加配额
如果您已实施指数回退和重试,但仍然收到 "ThrottlingException Rate exceeded" 错误,请提交 SNS 配额增加请求。
**注意:**您可以申请增加软限制配额,但无法提高硬配额上限。有关更多信息,请参阅服务配额。