如何解决 Amazon SNS 中的“ThrottlingException”和“Rate exceeded”错误?

2 分钟阅读
0

我想解决 Amazon Simple Notification Service (Amazon SNS) 中的“ThrottlingException”和“Rate exceeded”错误。

概述

Amazon SNS API 调用不能超过各 AWS 账户和区域允许的最大 API 请求速率。如果 API 请求超过此 API 节流限额,则会限制其他 API 调用,并且 Amazon SNS 会返回 ThrottlingException 错误。

Amazon SNS ThrottlingException 错误消息示例:

  • “An error occurred (ThrottlingException) when calling the Subscribe operation: Rate exceeded”
  • “An error occurred (ThrottlingException) when calling the ListOriginationNumbers operation: Rate exceeded”
  • “An error occurred (ThrottlingException) when calling the ListPhoneNumbersOptedOut operation: Rate exceeded”
  • “An error occurred (ThrottlingException) when calling the publish operation: Rate exceeded”
  • “An error occurred (ThrottlingException) when calling the GetSMSAttributes operation: Rate exceeded”

**注意:**对 Amazon SNS API 调用实施节流以提高服务性能。有关 Amazon SNS API 节流限额的更多信息,请参阅 Amazon Simple Notification Service 端点与限额

解决方法

找出受到节流的 API

找出导致错误的 API 操作

如果速率限制超过 API 调用的分配限制,就会收到错误消息。查看错误消息或错误堆栈,找出受到节流的 API 操作。

使用 CloudTrail 日志查看 API 调用的事件

Amazon SNS 已与 AWS CloudTrail 集成。CloudTrail 将 Amazon SNS 的 API 调用捕获为事件。这些事件包括来自 Amazon SNS 控制台的调用和对 Amazon SNS API 操作的代码调用。支持的事件会记录在 CloudTrail 事件中,其他 AWS 服务事件也记录在事件历史记录中。有关 CloudTrail 事件的更多信息,请参阅 CloudTrail 中的 Amazon SNS 信息
**注意:**不支持将 Publish 或 PublishBatch API 请求作为事件记录在 CloudTrail 中。

检查区域限额

即使您使用 PublishBatch API 而不是发布 API 来发送消息,区域限额也可能会导致限制。例如,如果您的区域限额为每秒 30,000 封邮件,您仍然可以通过以下方式超过速率限制:

  • 您以每秒 30,000 个 API 请求的速度使用“Publish”操作发布了 30,000 条消息(每个 API 请求 1 条消息)。
  • 您以每秒 3,000 个 API 请求的速度使用“PublishBatch”操作发布了 30,000 条消息(每批 API 请求 10 条消息)。
  • 您以每秒 10,000 个 API 请求的速度使用“Publish”操作发布了 10,000 条消息(每个 API 请求 1 条消息)。您还以每秒 2,000 个 API 请求的速度使用“PublishBatch”操作发布了 20,000 条消息(每批 API 请求 10 条消息)。这样一来,您每秒总共创建了 30,000 条消息。

每秒消息限额基于发布到某个 Amazon SNS 区域的消息数量,它结合了 Publish 和 PublishBatch API 请求。有关更多信息,请参阅 Amazon Simple Notification Service 端点与限额中的发布 API 节流其他 API 节流

防止出现“ThrottlingException”错误的最佳实践

错开 API 调用的间隔

使用 AWS 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 错误,可以针对造成该错误的 API 申请提高服务限额。在用例描述中,说明节流发生的时间范围以及您申请提高限额的原因。

您也可以参阅服务限额控制面板,了解 SNS 服务的当前限额。
注意: 请确保针对自己的 AWS 区域申请提高服务限额。

**重要事项:**您可以申请提高软限制的服务限额,但仅限 Amazon SNS 限额。您不能增加硬性限制、Amazon SNS 限额。有关更多信息,请参阅服务限额

AWS 官方
AWS 官方已更新 9 个月前