diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/client/RetryStrategy.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/client/RetryStrategy.cpp')
-rw-r--r-- | contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/client/RetryStrategy.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/client/RetryStrategy.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/client/RetryStrategy.cpp new file mode 100644 index 0000000000..b439b7ca99 --- /dev/null +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/client/RetryStrategy.cpp @@ -0,0 +1,102 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include <aws/core/client/RetryStrategy.h> + +#include <aws/core/client/AWSError.h> +#include <aws/core/client/CoreErrors.h> +#include <aws/core/utils/Outcome.h> + +using namespace Aws::Utils::Threading; + +namespace Aws +{ + namespace Client + { + static const int INITIAL_RETRY_TOKENS = 500; + static const int RETRY_COST = 5; + static const int NO_RETRY_INCREMENT = 1; + static const int TIMEOUT_RETRY_COST = 10; + + StandardRetryStrategy::StandardRetryStrategy(long maxAttempts) : + m_retryQuotaContainer(Aws::MakeShared<DefaultRetryQuotaContainer>("StandardRetryStrategy")), + m_maxAttempts(maxAttempts) + {} + + StandardRetryStrategy::StandardRetryStrategy(std::shared_ptr<RetryQuotaContainer> retryQuotaContainer, long maxAttempts) : + m_retryQuotaContainer(retryQuotaContainer), + m_maxAttempts(maxAttempts) + {} + + void StandardRetryStrategy::RequestBookkeeping(const HttpResponseOutcome& httpResponseOutcome) + { + if (httpResponseOutcome.IsSuccess()) + { + m_retryQuotaContainer->ReleaseRetryQuota(NO_RETRY_INCREMENT); + } + } + + void StandardRetryStrategy::RequestBookkeeping(const HttpResponseOutcome& httpResponseOutcome, const AWSError<CoreErrors>& lastError) + { + if (httpResponseOutcome.IsSuccess()) + { + m_retryQuotaContainer->ReleaseRetryQuota(lastError); + } + } + + bool StandardRetryStrategy::ShouldRetry(const AWSError<CoreErrors>& error, long attemptedRetries) const + { + if (!error.ShouldRetry()) + return false; + + if (attemptedRetries + 1 >= m_maxAttempts) + return false; + + return m_retryQuotaContainer->AcquireRetryQuota(error); + } + + long StandardRetryStrategy::CalculateDelayBeforeNextRetry(const AWSError<CoreErrors>& error, long attemptedRetries) const + { + AWS_UNREFERENCED_PARAM(error); + return (std::min)(rand() % 1000 * (1 << attemptedRetries), 20000); + } + + DefaultRetryQuotaContainer::DefaultRetryQuotaContainer() : m_retryQuota(INITIAL_RETRY_TOKENS) + {} + + bool DefaultRetryQuotaContainer::AcquireRetryQuota(int capacityAmount) + { + WriterLockGuard guard(m_retryQuotaLock); + + if (capacityAmount > m_retryQuota) + { + return false; + } + else + { + m_retryQuota -= capacityAmount; + return true; + } + } + + bool DefaultRetryQuotaContainer::AcquireRetryQuota(const AWSError<CoreErrors>& error) + { + int capacityAmount = error.GetErrorType() == CoreErrors::REQUEST_TIMEOUT ? TIMEOUT_RETRY_COST : RETRY_COST; + return AcquireRetryQuota(capacityAmount); + } + + void DefaultRetryQuotaContainer::ReleaseRetryQuota(int capacityAmount) + { + WriterLockGuard guard(m_retryQuotaLock); + m_retryQuota = (std::min)(m_retryQuota + capacityAmount, INITIAL_RETRY_TOKENS); + } + + void DefaultRetryQuotaContainer::ReleaseRetryQuota(const AWSError<CoreErrors>& error) + { + int capacityAmount = error.GetErrorType() == CoreErrors::REQUEST_TIMEOUT ? TIMEOUT_RETRY_COST : RETRY_COST; + ReleaseRetryQuota(capacityAmount); + } + } +}
\ No newline at end of file |