Skip to content

Commit 5719404

Browse files
committed
remove c rand from the standard retry strategy
1 parent 9204e23 commit 5719404

File tree

5 files changed

+58
-44
lines changed

5 files changed

+58
-44
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
AWS_CORE_LOCAL std::mt19937::result_type GetRandomValue();
15+
16+
} // namespace Utils
17+
} // namespace Aws

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55

6-
#include <aws/core/client/RetryStrategy.h>
7-
86
#include <aws/core/client/AWSError.h>
97
#include <aws/core/client/CoreErrors.h>
8+
#include <aws/core/client/RetryStrategy.h>
109
#include <aws/core/utils/Outcome.h>
10+
#include <aws/core/utils/local/Random.h>
1111

1212
using namespace Aws::Utils::Threading;
1313

@@ -19,19 +19,11 @@ namespace Aws
1919
static const int RETRY_COST = 5;
2020
static const int TIMEOUT_RETRY_COST = 10;
2121

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

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-
}
25+
StandardRetryStrategy::StandardRetryStrategy(std::shared_ptr<RetryQuotaContainer> retryQuotaContainer, long maxAttempts)
26+
: m_retryQuotaContainer(retryQuotaContainer), m_maxAttempts(maxAttempts) {}
3527

3628
void StandardRetryStrategy::RequestBookkeeping(const HttpResponseOutcome& httpResponseOutcome)
3729
{
@@ -64,7 +56,7 @@ namespace Aws
6456
{
6557
AWS_UNREFERENCED_PARAM(error);
6658
// 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);
59+
return std::min(static_cast<int>(Aws::Utils::GetRandomValue() % 1000) * (1 << std::min(attemptedRetries, 15L)), 20000);
6860
}
6961

7062
DefaultRetryQuotaContainer::DefaultRetryQuotaContainer() : m_retryQuota(INITIAL_RETRY_TOKENS)

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

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55

6-
#include <aws/core/utils/UUID.h>
76
#include <aws/core/utils/HashingUtils.h>
87
#include <aws/core/utils/StringUtils.h>
8+
#include <aws/core/utils/UUID.h>
99
#include <aws/core/utils/crypto/Factories.h>
1010
#include <aws/core/utils/crypto/SecureRandom.h>
11-
#include <iomanip>
12-
#include <random>
13-
#include <chrono>
14-
#include <thread>
15-
#include <mutex>
11+
#include <aws/core/utils/local/Random.h>
1612

1713
namespace Aws
1814
{
@@ -91,33 +87,12 @@ namespace Aws
9187
return Aws::Utils::UUID(randomBytes);
9288
}
9389

94-
#ifdef UINT64_MAX
95-
using MTEngine = std::mt19937_64;
96-
using RandGenType = uint64_t;
97-
#else
98-
using MTEngine = std::mt19937;
99-
using RandGenType = unsigned int;
100-
#endif
101-
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-
11290
Aws::Utils::UUID UUID::PseudoRandomUUID()
11391
{
114-
static const thread_local size_t threadSeed = GetCurrentThreadRandomSeed();
115-
static thread_local MTEngine gen(threadSeed);
116-
11792
unsigned char randomBytes[UUID_BINARY_SIZE] = {0};
11893

119-
for (size_t i = 0; i < UUID_BINARY_SIZE / sizeof(RandGenType); i++) {
120-
reinterpret_cast<RandGenType*>(randomBytes)[i] = gen();
94+
for (size_t i = 0; i < UUID_BINARY_SIZE / sizeof(decltype(Aws::Utils::GetRandomValue())); i++) {
95+
reinterpret_cast<decltype(Aws::Utils::GetRandomValue())*>(randomBytes)[i] = GetRandomValue();
12196
}
12297

12398
//Set version bits to 0100
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 <cstddef>
9+
#include <mutex>
10+
#include <random>
11+
#include <thread>
12+
13+
std::mt19937::result_type Aws::Utils::GetRandomValue() {
14+
static size_t const processRandomSeed = std::random_device{}();
15+
static std::mt19937 threadRandomSeedGen(processRandomSeed);
16+
// Threads can be re-used (esp. on OS X), generate a true random per-thread random seed
17+
static std::mutex threadRandomSeedGenMtx;
18+
thread_local std::mt19937 gen([]() -> size_t {
19+
std::unique_lock<std::mutex> const lock(threadRandomSeedGenMtx);
20+
return static_cast<size_t>(std::hash<std::thread::id>{}(std::this_thread::get_id()) ^ threadRandomSeedGen());
21+
}());
22+
23+
return gen();
24+
}

0 commit comments

Comments
 (0)