如何對 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 資訊
注意: 不支援將發佈或 PublishBatch API 請求記錄為 CloudTrail 中的事件。

檢查區域配額

即使您使用 PublishBatch API 而非發佈 API 來傳送訊息,區域配額也可能導致限流。例如,如果您的區域配額為每秒 3 萬則訊息,您仍然可以透過下列方式超過速率限制:

  • 您以每秒 3 萬個 API 請求的速率使用「發佈」動作來發佈 3 萬則訊息 (每個 API 請求一則訊息)。
  • 您以每秒 3 千個 API 請求的速率使用 PublishBatch 動作來發佈 3 萬則訊息 (每個批次 API 請求 10 則訊息)。
  • 您以每秒 1 萬個 API 請求的速率使用「發佈」動作來發佈 1 萬則訊息 (每個 API 請求一則訊息)。您也以每秒 2 千個 API 請求的速率使用 PublishBatch 動作來發佈 2 萬則訊息 (每個批次 API 請求 10 則訊息)。並且,在此過程中,您每秒總共建立了 3 萬則發佈的訊息。

每秒訊息數量配額以發佈至 Amazon SNS 區域的訊息數量為基礎,並結合了發佈和 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 個月前