我想要疑難排解 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 呼叫的事件。受支援的事件會與其他 AWS 服務事件一起記錄在 CloudTrail 事件中,作為事件歷史記錄的一部分。如需更多資訊,請參閱使用 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 配額提高申請。
**注意:**您可以請求提高軟性限制,但無法提高硬性配額限制。如需更多資訊,請參閱服務配額。