Skip to content

Commit dee37e6

Browse files
committed
updated testing and namespace
1 parent 3ca5166 commit dee37e6

3 files changed

Lines changed: 222 additions & 533 deletions

File tree

src/aws-cpp-sdk-core/source/client/RetryStrategy.cpp

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,87 +14,96 @@
1414
#include <aws/core/utils/logging/LogMacros.h>
1515

1616
using namespace Aws::Utils::Threading;
17+
using namespace Aws::Client;
1718

1819
static const char RETRY_STRATEGY_TAG[] = "StandardRetryStrategy";
20+
static const int INITIAL_RETRY_TOKENS = 500;
21+
static const int RETRY_COST = 5;
22+
static const int TIMEOUT_RETRY_COST = 10;
1923

2024
namespace Aws
2125
{
2226
namespace Client
2327
{
24-
static const int INITIAL_RETRY_TOKENS = 500;
25-
static const int RETRY_COST = 5;
26-
static const int TIMEOUT_RETRY_COST = 10;
27-
28-
static bool IsNewRetriesEnabled()
29-
{
30-
return Aws::Utils::StringUtils::ToLower(Aws::Environment::GetEnv("AWS_NEW_RETRIES_2026").c_str()) == "true";
31-
}
32-
33-
struct StandardRetryStrategy::RetryImpl
28+
class StandardRetryStrategy::RetryImpl
3429
{
30+
public:
3531
virtual ~RetryImpl() = default;
3632
virtual long CalculateDelay(const AWSError<CoreErrors>& error, long attemptedRetries) const = 0;
3733
};
34+
}
35+
}
36+
37+
namespace {
38+
bool IsNewRetriesEnabled()
39+
{
40+
return Aws::Utils::StringUtils::ToLower(Aws::Environment::GetEnv("AWS_NEW_RETRIES_2026").c_str()) == "true";
41+
}
3842

39-
namespace {
40-
struct LegacyRetryImpl : StandardRetryStrategy::RetryImpl
43+
class LegacyRetryImpl : public StandardRetryStrategy::RetryImpl
44+
{
45+
public:
46+
long CalculateDelay(const AWSError<CoreErrors>& error, long attemptedRetries) const override
4147
{
42-
long CalculateDelay(const AWSError<CoreErrors>& error, long attemptedRetries) const override
43-
{
44-
AWS_UNREFERENCED_PARAM(error);
45-
// Maximum left shift factor is capped by ceil(log2(max_delay)), to avoid wrap-around and overflow into negative values:
46-
return std::min(static_cast<int>(Aws::Utils::GetRandomValue() % 1000) * (1 << std::min(attemptedRetries, 15L)), 20000);
47-
}
48-
};
48+
AWS_UNREFERENCED_PARAM(error);
49+
// Maximum left shift factor is capped by ceil(log2(max_delay)), to avoid wrap-around and overflow into negative values:
50+
return std::min(static_cast<int>(Aws::Utils::GetRandomValue() % 1000) * (1 << std::min(attemptedRetries, 15L)), 20000);
51+
}
52+
};
4953

50-
struct NewRetriesImpl : StandardRetryStrategy::RetryImpl
54+
class NewRetriesImpl : public StandardRetryStrategy::RetryImpl
55+
{
56+
public:
57+
long CalculateDelay(const AWSError<CoreErrors>& error, long attemptedRetries) const override
5158
{
52-
long CalculateDelay(const AWSError<CoreErrors>& error, long attemptedRetries) const override
53-
{
54-
double x = error.ShouldThrottle() ? 1.0 : 0.05;
55-
double exponentialPart = x * static_cast<double>(1L << (std::min)(attemptedRetries, 30L));
56-
double cappedPart = (std::min)(exponentialPart, 20.0);
59+
double x = error.ShouldThrottle() ? 1.0 : 0.05;
60+
double exponentialPart = x * static_cast<double>(1L << (std::min)(attemptedRetries, 30L));
61+
double cappedPart = (std::min)(exponentialPart, 20.0);
5762

58-
double b = static_cast<double>(Aws::Utils::GetRandomValue() % 10000) / 10000.0;
59-
double t_i = b * cappedPart;
63+
double b = static_cast<double>(Aws::Utils::GetRandomValue() % 10000) / 10000.0;
64+
double t_i = b * cappedPart;
6065

61-
const auto& headers = error.GetResponseHeaders();
62-
auto it = headers.find("x-amz-retry-after");
63-
if (it != headers.end())
66+
const auto& headers = error.GetResponseHeaders();
67+
auto it = headers.find("x-amz-retry-after");
68+
if (it != headers.end())
69+
{
70+
long long headerMs = Aws::Utils::StringUtils::ConvertToInt64(it->second.c_str());
71+
if (headerMs < 0)
6472
{
65-
long long headerMs = Aws::Utils::StringUtils::ConvertToInt64(it->second.c_str());
66-
if (headerMs < 0)
67-
{
68-
AWS_LOGSTREAM_DEBUG(RETRY_STRATEGY_TAG, "Ignoring invalid x-amz-retry-after value: " << it->second);
69-
}
70-
double headerSec = static_cast<double>(headerMs) / 1000.0;
71-
double clamped = (std::max)(t_i, (std::min)(headerSec, 5.0 + t_i));
72-
return static_cast<long>(clamped * 1000.0);
73+
AWS_LOGSTREAM_DEBUG(RETRY_STRATEGY_TAG, "Ignoring invalid x-amz-retry-after value: " << it->second);
7374
}
74-
75-
return static_cast<long>(t_i * 1000.0);
75+
double headerSec = static_cast<double>(headerMs) / 1000.0;
76+
double clamped = (std::max)(t_i, (std::min)(headerSec, 5.0 + t_i));
77+
return static_cast<long>(clamped * 1000.0);
7678
}
77-
};
78-
} // anonymous namespace
7979

80-
static Aws::UniquePtr<StandardRetryStrategy::RetryImpl> CreateRetryImpl()
80+
return static_cast<long>(t_i * 1000.0);
81+
}
82+
};
83+
84+
Aws::UniquePtr<StandardRetryStrategy::RetryImpl> CreateRetryImpl()
85+
{
86+
if (IsNewRetriesEnabled())
8187
{
82-
if (IsNewRetriesEnabled())
83-
{
84-
return Aws::MakeUnique<NewRetriesImpl>("StandardRetryStrategy");
85-
}
86-
return Aws::MakeUnique<LegacyRetryImpl>("StandardRetryStrategy");
88+
return Aws::MakeUnique<NewRetriesImpl>("StandardRetryStrategy");
8789
}
90+
return Aws::MakeUnique<LegacyRetryImpl>("StandardRetryStrategy");
91+
}
8892

89-
static std::shared_ptr<RetryQuotaContainer> CreateQuotaContainer()
93+
std::shared_ptr<RetryQuotaContainer> CreateQuotaContainer()
94+
{
95+
if (IsNewRetriesEnabled())
9096
{
91-
if (IsNewRetriesEnabled())
92-
{
93-
return Aws::MakeShared<ThrottleBasedRetryQuotaContainer>("StandardRetryStrategy");
94-
}
95-
return Aws::MakeShared<DefaultRetryQuotaContainer>("StandardRetryStrategy");
97+
return Aws::MakeShared<ThrottleBasedRetryQuotaContainer>("StandardRetryStrategy");
9698
}
99+
return Aws::MakeShared<DefaultRetryQuotaContainer>("StandardRetryStrategy");
100+
}
101+
} // anonymous namespace
97102

103+
namespace Aws
104+
{
105+
namespace Client
106+
{
98107
StandardRetryStrategy::StandardRetryStrategy(long maxAttempts)
99108
: m_retryQuotaContainer(CreateQuotaContainer()), m_maxAttempts(maxAttempts),
100109
m_impl(CreateRetryImpl()) {}

0 commit comments

Comments
 (0)