Skip to content

Commit 5548cf6

Browse files
committed
remove c rand from the standard retry strategy
1 parent b2341a3 commit 5548cf6

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

src/aws-cpp-sdk-core/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ file(GLOB UTILS_BASE64_HEADERS "include/aws/core/utils/base64/*.h")
6363
file(GLOB UTILS_CHECKSUM_HEADERS "include/aws/core/utils/checksum/*.h")
6464
file(GLOB UTILS_CRYPTO_HEADERS "include/aws/core/utils/crypto/*.h")
6565
file(GLOB UTILS_JSON_HEADERS "include/aws/core/utils/json/*.h")
66+
file(GLOB UTILS_LOCAL_HEADERS "include/aws/core/utils/local/*.h")
6667
file(GLOB UTILS_CBOR_HEADERS "include/aws/core/utils/cbor/*.h")
6768
file(GLOB UTILS_THREADING_HEADERS "include/aws/core/utils/threading/*.h")
6869
file(GLOB UTILS_XML_HEADERS "include/aws/core/utils/xml/*.h")
@@ -119,6 +120,7 @@ file(GLOB UTILS_BASE64_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/base64/*
119120
file(GLOB UTILS_CHECKSUM_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/checksum/*.cpp")
120121
file(GLOB UTILS_CRYPTO_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/crypto/*.cpp")
121122
file(GLOB UTILS_JSON_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/json/*.cpp")
123+
file(GLOB UTILS_LOCAL_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/local/*.cpp")
122124
file(GLOB UTILS_CBOR_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/cbor/*.cpp")
123125
file(GLOB UTILS_THREADING_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/threading/*.cpp")
124126
file(GLOB UTILS_XML_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/xml/*.cpp")
@@ -274,6 +276,7 @@ file(GLOB AWS_NATIVE_SDK_COMMON_HEADERS
274276
${UTILS_CHECKSUM_HEADERS}
275277
${UTILS_CRYPTO_HEADERS}
276278
${UTILS_JSON_HEADERS}
279+
${UTILS_LOCAL_HEADERS}
277280
${UTILS_CBOR_HEADERS}
278281
${UTILS_THREADING_HEADERS}
279282
${UTILS_RETRY_HEADERS}
@@ -356,6 +359,7 @@ file(GLOB AWS_NATIVE_SDK_NON_UNITY_SRC
356359
${MONITORING_SOURCE}
357360
${UTILS_CRYPTO_FACTORY_SOURCE}
358361
${UTILS_JSON_SOURCE}
362+
${UTILS_LOCAL_SOURCE}
359363
${UTILS_CBOR_SOURCE}
360364
${UTILS_EVENT_SOURCE}
361365
${UTILS_SOURCE}
@@ -417,6 +421,7 @@ if(MSVC)
417421
source_group("Header Files\\aws\\core\\utils\\event" FILES ${UTILS_EVENT_HEADERS})
418422
source_group("Header Files\\aws\\core\\utils\\exceptions" FILES ${UTILS_EXCEPTIONS_HEADERS})
419423
source_group("Header Files\\aws\\core\\utils\\json" FILES ${UTILS_JSON_HEADERS})
424+
source_group("Header Files\\aws\\core\\utils\\local" FILES ${UTILS_LOCAL_HEADERS})
420425
source_group("Header Files\\aws\\core\\utils\\cbor" FILES ${UTILS_CBOR_HEADERS})
421426
source_group("Header Files\\aws\\core\\utils\\threading" FILES ${UTILS_THREADING_HEADERS})
422427
source_group("Header Files\\aws\\core\\utils\\xml" FILES ${UTILS_XML_HEADERS})
@@ -487,6 +492,7 @@ if(MSVC)
487492
source_group("Source Files\\utils\\event" FILES ${UTILS_EVENT_SOURCE})
488493
source_group("Source Files\\utils\\exceptions" FILES ${UTILS_EXCEPTIONS_SOURCE})
489494
source_group("Source Files\\utils\\json" FILES ${UTILS_JSON_SOURCE})
495+
source_group("Source Files\\utils\\local" FILES ${UTILS_LOCAL_SOURCE})
490496
source_group("Source Files\\utils\\cbor" FILES ${UTILS_CBOR_SOURCE})
491497
source_group("Source Files\\utils\\threading" FILES ${UTILS_THREADING_SOURCE})
492498
source_group("Source Files\\utils\\xml" FILES ${UTILS_XML_SOURCE})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
#pragma once
7+
#include <aws/core/Core_EXPORTS.h>
8+
9+
#include <random>
10+
11+
namespace Aws {
12+
namespace Utils {
13+
14+
#ifdef UINT64_MAX
15+
using MTEngine = std::mt19937_64;
16+
using RandGenType = uint64_t;
17+
#else
18+
using MTEngine = std::mt19937;
19+
using RandGenType = unsigned int;
20+
#endif
21+
22+
AWS_CORE_LOCAL MTEngine::result_type GetRandomValue();
23+
24+
} // namespace Utils
25+
} // namespace Aws

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <aws/core/client/AWSError.h>
99
#include <aws/core/client/CoreErrors.h>
1010
#include <aws/core/utils/Outcome.h>
11+
#include <aws/core/utils/local/Random.h>
1112

1213
using namespace Aws::Utils::Threading;
1314

@@ -19,19 +20,11 @@ namespace Aws
1920
static const int RETRY_COST = 5;
2021
static const int TIMEOUT_RETRY_COST = 10;
2122

22-
StandardRetryStrategy::StandardRetryStrategy(long maxAttempts) :
23-
m_retryQuotaContainer(Aws::MakeShared<DefaultRetryQuotaContainer>("StandardRetryStrategy")),
24-
m_maxAttempts(maxAttempts)
25-
{
26-
srand((unsigned int)time(NULL));
27-
}
23+
StandardRetryStrategy::StandardRetryStrategy(long maxAttempts)
24+
: m_retryQuotaContainer(Aws::MakeShared<DefaultRetryQuotaContainer>("StandardRetryStrategy")), m_maxAttempts(maxAttempts) {}
2825

29-
StandardRetryStrategy::StandardRetryStrategy(std::shared_ptr<RetryQuotaContainer> retryQuotaContainer, long maxAttempts) :
30-
m_retryQuotaContainer(retryQuotaContainer),
31-
m_maxAttempts(maxAttempts)
32-
{
33-
srand((unsigned int)time(NULL));
34-
}
26+
StandardRetryStrategy::StandardRetryStrategy(std::shared_ptr<RetryQuotaContainer> retryQuotaContainer, long maxAttempts)
27+
: m_retryQuotaContainer(retryQuotaContainer), m_maxAttempts(maxAttempts) {}
3528

3629
void StandardRetryStrategy::RequestBookkeeping(const HttpResponseOutcome& httpResponseOutcome)
3730
{
@@ -64,7 +57,7 @@ namespace Aws
6457
{
6558
AWS_UNREFERENCED_PARAM(error);
6659
// Maximum left shift factor is capped by ceil(log2(max_delay)), to avoid wrap-around and overflow into negative values:
67-
return (std::min)(rand() % 1000 * (1 << (std::min)(attemptedRetries, 15L)), 20000);
60+
return (std::min)(static_cast<long>(Aws::Utils::GetRandomValue() % 1000L) * (1L << (std::min)(attemptedRetries, 15L)), 20000L);
6861
}
6962

7063
DefaultRetryQuotaContainer::DefaultRetryQuotaContainer() : m_retryQuota(INITIAL_RETRY_TOKENS)

src/aws-cpp-sdk-core/source/utils/UUID.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include <aws/core/utils/StringUtils.h>
99
#include <aws/core/utils/crypto/Factories.h>
1010
#include <aws/core/utils/crypto/SecureRandom.h>
11+
#include <aws/core/utils/local/Random.h>
1112
#include <iomanip>
12-
#include <random>
1313
#include <chrono>
1414
#include <thread>
1515
#include <mutex>
@@ -99,25 +99,12 @@ namespace Aws
9999
using RandGenType = unsigned int;
100100
#endif
101101

102-
static size_t GetCurrentThreadRandomSeed()
103-
{
104-
static size_t processRandomSeed = std::random_device{}();
105-
static MTEngine threadRandomSeedGen(processRandomSeed);
106-
// Threads can be re-used (esp. on OS X), generate a true random per-thread random seed
107-
static std::mutex threadRandomSeedGenMtx;
108-
std::unique_lock<std::mutex> lock(threadRandomSeedGenMtx);
109-
return static_cast<size_t>(std::hash<std::thread::id>{}(std::this_thread::get_id()) ^ threadRandomSeedGen());
110-
}
111-
112102
Aws::Utils::UUID UUID::PseudoRandomUUID()
113103
{
114-
static const thread_local size_t threadSeed = GetCurrentThreadRandomSeed();
115-
static thread_local MTEngine gen(threadSeed);
116-
117104
unsigned char randomBytes[UUID_BINARY_SIZE] = {0};
118105

119106
for (size_t i = 0; i < UUID_BINARY_SIZE / sizeof(RandGenType); i++) {
120-
reinterpret_cast<RandGenType*>(randomBytes)[i] = gen();
107+
reinterpret_cast<RandGenType*>(randomBytes)[i] = GetRandomValue();
121108
}
122109

123110
//Set version bits to 0100
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
#include <aws/core/utils/local/Random.h>
7+
8+
#include <thread>
9+
10+
Aws::Utils::MTEngine::result_type Aws::Utils::GetRandomValue() {
11+
static size_t const processRandomSeed = std::random_device{}();
12+
static MTEngine threadRandomSeedGen(processRandomSeed);
13+
// Threads can be re-used (esp. on OS X), generate a true random per-thread random seed
14+
static std::mutex threadRandomSeedGenMtx;
15+
thread_local MTEngine gen([]() -> size_t {
16+
std::unique_lock<std::mutex> const lock(threadRandomSeedGenMtx);
17+
return std::hash<std::thread::id>{}(std::this_thread::get_id()) ^ threadRandomSeedGen();
18+
}());
19+
20+
return gen();
21+
}

0 commit comments

Comments
 (0)