Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/aws-cpp-sdk-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ file(GLOB UTILS_BASE64_HEADERS "include/aws/core/utils/base64/*.h")
file(GLOB UTILS_CHECKSUM_HEADERS "include/aws/core/utils/checksum/*.h")
file(GLOB UTILS_CRYPTO_HEADERS "include/aws/core/utils/crypto/*.h")
file(GLOB UTILS_JSON_HEADERS "include/aws/core/utils/json/*.h")
file(GLOB UTILS_LOCAL_HEADERS "include/aws/core/utils/local/*.h")
file(GLOB UTILS_CBOR_HEADERS "include/aws/core/utils/cbor/*.h")
file(GLOB UTILS_THREADING_HEADERS "include/aws/core/utils/threading/*.h")
file(GLOB UTILS_XML_HEADERS "include/aws/core/utils/xml/*.h")
Expand Down Expand Up @@ -119,6 +120,7 @@ file(GLOB UTILS_BASE64_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/base64/*
file(GLOB UTILS_CHECKSUM_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/checksum/*.cpp")
file(GLOB UTILS_CRYPTO_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/crypto/*.cpp")
file(GLOB UTILS_JSON_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/json/*.cpp")
file(GLOB UTILS_LOCAL_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/local/*.cpp")
file(GLOB UTILS_CBOR_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/cbor/*.cpp")
file(GLOB UTILS_THREADING_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/threading/*.cpp")
file(GLOB UTILS_XML_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/xml/*.cpp")
Expand Down Expand Up @@ -274,6 +276,7 @@ file(GLOB AWS_NATIVE_SDK_COMMON_HEADERS
${UTILS_CHECKSUM_HEADERS}
${UTILS_CRYPTO_HEADERS}
${UTILS_JSON_HEADERS}
${UTILS_LOCAL_HEADERS}
${UTILS_CBOR_HEADERS}
${UTILS_THREADING_HEADERS}
${UTILS_RETRY_HEADERS}
Expand Down Expand Up @@ -356,6 +359,7 @@ file(GLOB AWS_NATIVE_SDK_NON_UNITY_SRC
${MONITORING_SOURCE}
${UTILS_CRYPTO_FACTORY_SOURCE}
${UTILS_JSON_SOURCE}
${UTILS_LOCAL_SOURCE}
${UTILS_CBOR_SOURCE}
${UTILS_EVENT_SOURCE}
${UTILS_SOURCE}
Expand Down Expand Up @@ -417,6 +421,7 @@ if(MSVC)
source_group("Header Files\\aws\\core\\utils\\event" FILES ${UTILS_EVENT_HEADERS})
source_group("Header Files\\aws\\core\\utils\\exceptions" FILES ${UTILS_EXCEPTIONS_HEADERS})
source_group("Header Files\\aws\\core\\utils\\json" FILES ${UTILS_JSON_HEADERS})
source_group("Header Files\\aws\\core\\utils\\local" FILES ${UTILS_LOCAL_HEADERS})
source_group("Header Files\\aws\\core\\utils\\cbor" FILES ${UTILS_CBOR_HEADERS})
source_group("Header Files\\aws\\core\\utils\\threading" FILES ${UTILS_THREADING_HEADERS})
source_group("Header Files\\aws\\core\\utils\\xml" FILES ${UTILS_XML_HEADERS})
Expand Down Expand Up @@ -487,6 +492,7 @@ if(MSVC)
source_group("Source Files\\utils\\event" FILES ${UTILS_EVENT_SOURCE})
source_group("Source Files\\utils\\exceptions" FILES ${UTILS_EXCEPTIONS_SOURCE})
source_group("Source Files\\utils\\json" FILES ${UTILS_JSON_SOURCE})
source_group("Source Files\\utils\\local" FILES ${UTILS_LOCAL_SOURCE})
source_group("Source Files\\utils\\cbor" FILES ${UTILS_CBOR_SOURCE})
source_group("Source Files\\utils\\threading" FILES ${UTILS_THREADING_SOURCE})
source_group("Source Files\\utils\\xml" FILES ${UTILS_XML_SOURCE})
Expand Down
17 changes: 17 additions & 0 deletions src/aws-cpp-sdk-core/include/aws/core/utils/local/Random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#pragma once
#include <aws/core/Core_EXPORTS.h>

#include <random>

namespace Aws {
namespace Utils {

AWS_CORE_LOCAL std::mt19937::result_type GetRandomValue();

} // namespace Utils
} // namespace Aws
22 changes: 7 additions & 15 deletions src/aws-cpp-sdk-core/source/client/RetryStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* 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/client/RetryStrategy.h>
#include <aws/core/utils/Outcome.h>
#include <aws/core/utils/local/Random.h>

using namespace Aws::Utils::Threading;

Expand All @@ -19,19 +19,11 @@ namespace Aws
static const int RETRY_COST = 5;
static const int TIMEOUT_RETRY_COST = 10;

StandardRetryStrategy::StandardRetryStrategy(long maxAttempts) :
m_retryQuotaContainer(Aws::MakeShared<DefaultRetryQuotaContainer>("StandardRetryStrategy")),
m_maxAttempts(maxAttempts)
{
srand((unsigned int)time(NULL));
}
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)
{
srand((unsigned int)time(NULL));
}
StandardRetryStrategy::StandardRetryStrategy(std::shared_ptr<RetryQuotaContainer> retryQuotaContainer, long maxAttempts)
: m_retryQuotaContainer(retryQuotaContainer), m_maxAttempts(maxAttempts) {}

void StandardRetryStrategy::RequestBookkeeping(const HttpResponseOutcome& httpResponseOutcome)
{
Expand Down Expand Up @@ -64,7 +56,7 @@ namespace Aws
{
AWS_UNREFERENCED_PARAM(error);
// Maximum left shift factor is capped by ceil(log2(max_delay)), to avoid wrap-around and overflow into negative values:
return (std::min)(rand() % 1000 * (1 << (std::min)(attemptedRetries, 15L)), 20000);
return std::min(static_cast<int>(Aws::Utils::GetRandomValue()) % 1000 * (1 << std::min(attemptedRetries, 15L)), 20000);
}

DefaultRetryQuotaContainer::DefaultRetryQuotaContainer() : m_retryQuota(INITIAL_RETRY_TOKENS)
Expand Down
33 changes: 4 additions & 29 deletions src/aws-cpp-sdk-core/source/utils/UUID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/core/utils/UUID.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/core/utils/StringUtils.h>
#include <aws/core/utils/UUID.h>
#include <aws/core/utils/crypto/Factories.h>
#include <aws/core/utils/crypto/SecureRandom.h>
#include <iomanip>
#include <random>
#include <chrono>
#include <thread>
#include <mutex>
#include <aws/core/utils/local/Random.h>

namespace Aws
{
Expand Down Expand Up @@ -91,33 +87,12 @@ namespace Aws
return Aws::Utils::UUID(randomBytes);
}

#ifdef UINT64_MAX
using MTEngine = std::mt19937_64;
using RandGenType = uint64_t;
#else
using MTEngine = std::mt19937;
using RandGenType = unsigned int;
#endif

static size_t GetCurrentThreadRandomSeed()
{
static size_t processRandomSeed = std::random_device{}();
static MTEngine threadRandomSeedGen(processRandomSeed);
// Threads can be re-used (esp. on OS X), generate a true random per-thread random seed
static std::mutex threadRandomSeedGenMtx;
std::unique_lock<std::mutex> lock(threadRandomSeedGenMtx);
return static_cast<size_t>(std::hash<std::thread::id>{}(std::this_thread::get_id()) ^ threadRandomSeedGen());
}

Aws::Utils::UUID UUID::PseudoRandomUUID()
{
static const thread_local size_t threadSeed = GetCurrentThreadRandomSeed();
static thread_local MTEngine gen(threadSeed);

unsigned char randomBytes[UUID_BINARY_SIZE] = {0};

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

//Set version bits to 0100
Expand Down
24 changes: 24 additions & 0 deletions src/aws-cpp-sdk-core/source/utils/local/Random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/core/utils/local/Random.h>

#include <cstddef>
#include <mutex>
#include <random>
#include <thread>

std::mt19937::result_type Aws::Utils::GetRandomValue() {
static size_t const processRandomSeed = std::random_device{}();
static std::mt19937 threadRandomSeedGen(processRandomSeed);
// Threads can be re-used (esp. on OS X), generate a true random per-thread random seed
static std::mutex threadRandomSeedGenMtx;
thread_local std::mt19937 gen([]() -> size_t {
std::unique_lock<std::mutex> const lock(threadRandomSeedGenMtx);
return static_cast<size_t>(std::hash<std::thread::id>{}(std::this_thread::get_id()) ^ threadRandomSeedGen());
}());

return gen();
}
Loading