//使用逐步增加的方式来延迟重试某个操作InitDelayValue = 100For(Retries = 0; Retries < MAX_RETRIES; Retries = Retries+1)wait for (2^Retries * InitDelayValue) millisecondsStatus = KmsApiRequest()IF Status == SUCCESSBREAK // Succeeded, stop calling the API again.ELSE IF Status = THROTTLED || Status == SERVER_NOT_READYCONTINUE // Failed due to throttling or server busy, try again.ELSEBREAK // another error occurs, stop calling the API again.END IF
# -*- coding: utf-8 -*-import base64import mathimport timefrom tencentcloud.common import credentialfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.common.profile.client_profile import ClientProfilefrom tencentcloud.common.profile.http_profile import HttpProfilefrom tencentcloud.kms.v20190118 import kms_client, modelsdef KmsInit(region="ap-guangzhou", secretId="", secretKey=""):try:credProfile = credential.Credential(secretId, secretKey)client = kms_client.KmsClient(credProfile, region)return clientexcept TencentCloudSDKException as err:print(err)return Nonedef BackoffFunction(RetryCount):InitDelayValue = 100DelayTime = math.pow(2, RetryCount) * InitDelayValuereturn DelayTimeif __name__ == '__main__':# 用户自定义参数secretId = "replace-with-real-secretId"secretKey = "replace-with-real-secretKey"region = "ap-guangzhou"keyId = "replace-with-realkeyid"plaintext = "abcdefg123456789abcdefg123456789abcdefg"Retries = 0MaxRetries = 10client = KmsInit(region, secretId, secretKey)req = models.EncryptRequest()req.KeyId = keyIdreq.Plaintext = base64.b64encode(plaintext)while Retries < MaxRetries:try:Retries += 1rsp = client.Encrypt(req) # 调用加密接口print 'plaintext: ',plaintext,'CiphertextBlob: ',rsp.CiphertextBlobbreakexcept TencentCloudSDKException as err:if err.code == 'InternalError' or err.code == 'RequestLimitExceeded':if Retries == MaxRetries:breaktime.sleep(BackoffFunction(Retries + 1))continueelse:print(err)breakexcept Exception as err:print(err)break
文档反馈