Skip to content

Commit

Permalink
Replace retry-utils with submodule to FreeRTOS/backoffAlgorithm (#1401)
Browse files Browse the repository at this point in the history
Use the new location of "retry utils" library which is the new `FreeRTOS/backoffAlgorithm` repository. 
Changes include:
* Adding the submodule
* Changing calling code of demos and tests to use the renamed API 
* Updating build files of demos/tests to use the submodule files
  • Loading branch information
aggarw13 authored Nov 24, 2020
1 parent ad28ed3 commit 7b4f726
Show file tree
Hide file tree
Showing 40 changed files with 283 additions and 977 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@
path = libraries/standard/corePKCS11
branch = main
url = https://github.com/FreeRTOS/corePKCS11.git
[submodule "libraries/standard/backoffAlgorithm"]
path = libraries/standard/backoffAlgorithm
branch = main
url = https://github.com/FreeRTOS/backoffAlgorithm.git
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ if(${BUILD_TESTS})
# Create a list for each unit test target.
set(utest_targets
openssl_utest sockets_utest
plaintext_utest clock_utest
retry_utils_utest)
plaintext_utest clock_utest)

# Add a target for running coverage on tests.
add_custom_target(coverage
Expand Down
8 changes: 6 additions & 2 deletions demos/defender/defender_demo_json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ set( DEMO_NAME "defender_demo" )
# Include MQTT library's source and header path variables.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreMQTT/mqttFilePaths.cmake )

# Include backoffAlgorithm library file path configuration.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake )

# Include JSON library's source and header path variables.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreJSON/jsonFilePaths.cmake )

Expand All @@ -19,6 +22,7 @@ add_executable( ${DEMO_NAME}
"report_builder.c"
${MQTT_SOURCES}
${MQTT_SERIALIZER_SOURCES}
${BACKOFF_ALGORITHM_SOURCES}
${JSON_SOURCES}
${DEFENDER_SOURCES} )

Expand All @@ -27,12 +31,12 @@ check_aws_credentials( ${DEMO_NAME} )

target_link_libraries( ${DEMO_NAME} PRIVATE
clock_posix
openssl_posix
retry_utils )
openssl_posix )

target_include_directories( ${DEMO_NAME} PUBLIC
${LOGGING_INCLUDE_DIRS}
${MQTT_INCLUDE_PUBLIC_DIRS}
${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS}
${JSON_INCLUDE_PUBLIC_DIRS}
${DEFENDER_INCLUDE_PUBLIC_DIRS}
${CMAKE_CURRENT_LIST_DIR} )
Expand Down
30 changes: 15 additions & 15 deletions demos/defender/defender_demo_json/mqtt_operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
/* OpenSSL sockets transport implementation. */
#include "openssl_posix.h"

/* Reconnect parameters. */
#include "retry_utils.h"
/*Include backoff algorithm header for retry logic.*/
#include "backoff_algorithm.h"

/* Clock for timer. */
#include "clock.h"
Expand Down Expand Up @@ -253,7 +253,7 @@ static MQTTPublishCallback_t appPublishCallback = NULL;
/**
* @brief The random number generator to use for exponential backoff with
* jitter retry logic.
* This function is an implementation the #RetryUtils_RNG_t interface type
* This function is an implementation the #BackoffAlgorithm_RNG_t interface type
* of the retry utils library API.
*
* @return The generated random number. This function ALWAYS succeeds
Expand Down Expand Up @@ -341,9 +341,9 @@ static int32_t generateRandomNumber()
static bool connectToBrokerWithBackoffRetries( NetworkContext_t * pNetworkContext )
{
bool returnStatus = false;
RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess;
BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess;
OpensslStatus_t opensslStatus = OPENSSL_SUCCESS;
RetryUtilsContext_t reconnectParams;
BackoffAlgorithmContext_t reconnectParams;
ServerInfo_t serverInfo;
OpensslCredentials_t opensslCredentials;
uint16_t nextRetryBackOff = 0U;
Expand Down Expand Up @@ -381,11 +381,11 @@ static bool connectToBrokerWithBackoffRetries( NetworkContext_t * pNetworkContex
srand( tp.tv_nsec );

/* Initialize reconnect attempts and interval */
RetryUtils_InitializeParams( &reconnectParams,
CONNECTION_RETRY_BACKOFF_BASE_MS,
CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS,
CONNECTION_RETRY_MAX_ATTEMPTS,
generateRandomNumber );
BackoffAlgorithm_InitializeParams( &reconnectParams,
CONNECTION_RETRY_BACKOFF_BASE_MS,
CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS,
CONNECTION_RETRY_MAX_ATTEMPTS,
generateRandomNumber );

/* Attempt to connect to MQTT broker. If connection fails, retry after
* a timeout. Timeout value will exponentially increase until maximum
Expand Down Expand Up @@ -413,20 +413,20 @@ static bool connectToBrokerWithBackoffRetries( NetworkContext_t * pNetworkContex
else
{
/* Get back-off value (in milliseconds) for the next connection retry. */
retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff );
assert( retryUtilsStatus != RetryUtilsRngFailure );
backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff );
assert( backoffAlgStatus != BackoffAlgorithmRngFailure );

if( retryUtilsStatus == RetryUtilsRetriesExhausted )
if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted )
{
LogError( ( "Connection to the broker failed, all attempts exhausted." ) );
}
else if( retryUtilsStatus == RetryUtilsSuccess )
else if( backoffAlgStatus == BackoffAlgorithmSuccess )
{
LogWarn( ( "Connection to the broker failed. Retrying connection after backoff." ) );
( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND );
}
}
} while( ( opensslStatus != OPENSSL_SUCCESS ) && ( retryUtilsStatus == RetryUtilsSuccess ) );
} while( ( opensslStatus != OPENSSL_SUCCESS ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) );

return returnStatus;
}
Expand Down
28 changes: 14 additions & 14 deletions demos/http/common/src/http_demo_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
/* Demo utils header. */
#include "http_demo_utils.h"

/* Retry utilities. */
#include "retry_utils.h"
/*Include backoff algorithm header for retry logic.*/
#include "backoff_algorithm.h"

/* Third party parser utilities. */
#include "http_parser.h"
Expand Down Expand Up @@ -67,7 +67,7 @@
/**
* @brief The random number generator to use for exponential backoff with
* jitter retry logic.
* This function is an implementation the #RetryUtils_RNG_t interface type
* This function is an implementation the #BackoffAlgorithm_RNG_t interface type
* of the retry utils library API.
*
* @return The generated random number. This function ALWAYS succeeds
Expand All @@ -89,9 +89,9 @@ int32_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction,
{
int32_t returnStatus = EXIT_FAILURE;
/* Status returned by the retry utilities. */
RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess;
BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess;
/* Struct containing the next backoff time. */
RetryUtilsContext_t reconnectParams;
BackoffAlgorithmContext_t reconnectParams;
uint16_t nextRetryBackOff = 0U;
struct timespec tp;

Expand All @@ -106,11 +106,11 @@ int32_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction,
srand( tp.tv_nsec );

/* Initialize reconnect attempts and interval */
RetryUtils_InitializeParams( &reconnectParams,
CONNECTION_RETRY_BACKOFF_BASE_MS,
CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS,
CONNECTION_RETRY_MAX_ATTEMPTS,
generateRandomNumber );
BackoffAlgorithm_InitializeParams( &reconnectParams,
CONNECTION_RETRY_BACKOFF_BASE_MS,
CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS,
CONNECTION_RETRY_MAX_ATTEMPTS,
generateRandomNumber );

/* Attempt to connect to HTTP server. If connection fails, retry after
* a timeout. Timeout value will exponentially increase until maximum
Expand All @@ -122,10 +122,10 @@ int32_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction,
if( returnStatus != EXIT_SUCCESS )
{
/* Get back-off value (in milliseconds) for the next connection retry. */
retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff );
assert( retryUtilsStatus != RetryUtilsRngFailure );
backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff );
assert( backoffAlgStatus != BackoffAlgorithmRngFailure );

if( retryUtilsStatus == RetryUtilsSuccess )
if( backoffAlgStatus == BackoffAlgorithmSuccess )
{
LogWarn( ( "Connection to the HTTP server failed. Retrying connection after backoff." ) );
( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND );
Expand All @@ -135,7 +135,7 @@ int32_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction,
LogError( ( "Connection to the HTTP server failed, all attempts exhausted." ) );
}
}
} while( ( returnStatus == EXIT_FAILURE ) && ( retryUtilsStatus == RetryUtilsSuccess ) );
} while( ( returnStatus == EXIT_FAILURE ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) );

if( returnStatus == EXIT_FAILURE )
{
Expand Down
10 changes: 7 additions & 3 deletions demos/http/http_demo_basic_tls/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ set( DEMO_NAME "http_demo_basic_tls" )
# Include HTTP library's source and header path variables.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake )

# Include backoffAlgorithm library file path configuration.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake )

# Demo target.
add_executable(
${DEMO_NAME}
"${DEMO_NAME}.c"
"../common/src/http_demo_utils.c"
"${DEMOS_DIR}/http/common/src/http_demo_utils.c"
${HTTP_SOURCES}
${HTTP_THIRD_PARTY_SOURCES}
${BACKOFF_ALGORITHM_SOURCES}
)

target_link_libraries(
${DEMO_NAME}
PRIVATE
openssl_posix
retry_utils
)

target_include_directories(
${DEMO_NAME}
PUBLIC
"../common/include"
"${DEMOS_DIR}/http/common/include"
${HTTP_INCLUDE_PUBLIC_DIRS}
${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS}
${CMAKE_CURRENT_LIST_DIR}
${LOGGING_INCLUDE_DIRS}
)
Expand Down
4 changes: 2 additions & 2 deletions demos/http/http_demo_basic_tls/http_demo_basic_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
/* OpenSSL transport header. */
#include "openssl_posix.h"

/* Retry utilities. */
#include "retry_utils.h"
/*Include backoff algorithm header for retry logic.*/
#include "backoff_algorithm.h"

/* Check that hostname of the server is defined. */
#ifndef SERVER_HOST
Expand Down
10 changes: 7 additions & 3 deletions demos/http/http_demo_mutual_auth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ set( DEMO_NAME "http_demo_mutual_auth" )
# Include HTTP library's source and header path variables.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake )

# Include backoffAlgorithm library file path configuration.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake )

# Demo target.
add_executable(
${DEMO_NAME}
"${DEMO_NAME}.c"
"../common/src/http_demo_utils.c"
"${DEMOS_DIR}/http/common/src/http_demo_utils.c"
${HTTP_SOURCES}
${HTTP_THIRD_PARTY_SOURCES}
${BACKOFF_ALGORITHM_SOURCES}
)

# Add to default target if all required macros needed to run this demo are defined
Expand All @@ -21,14 +25,14 @@ target_link_libraries(
${DEMO_NAME}
PRIVATE
openssl_posix
retry_utils
)

target_include_directories(
${DEMO_NAME}
PUBLIC
"../common/include"
"${DEMOS_DIR}/http/common/include"
${HTTP_INCLUDE_PUBLIC_DIRS}
${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS}
${CMAKE_CURRENT_LIST_DIR}
${LOGGING_INCLUDE_DIRS}
)
Expand Down
10 changes: 7 additions & 3 deletions demos/http/http_demo_plaintext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ set( DEMO_NAME "http_demo_plaintext" )
# Include HTTP library's source and header path variables.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake )

# Include backoffAlgorithm library file path configuration.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake )

# Demo target.
add_executable(
${DEMO_NAME}
"${DEMO_NAME}.c"
"../common/src/http_demo_utils.c"
"${DEMOS_DIR}/http/common/src/http_demo_utils.c"
${HTTP_SOURCES}
${HTTP_THIRD_PARTY_SOURCES}
${BACKOFF_ALGORITHM_SOURCES}
)

target_link_libraries(
${DEMO_NAME}
PRIVATE
plaintext_posix
retry_utils
)

target_include_directories(
${DEMO_NAME}
PUBLIC
"../common/include"
"${DEMOS_DIR}/http/common/include"
${HTTP_INCLUDE_PUBLIC_DIRS}
${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS}
${CMAKE_CURRENT_LIST_DIR}
${LOGGING_INCLUDE_DIRS}
)
Expand Down
10 changes: 7 additions & 3 deletions demos/http/http_demo_s3_download/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ set( DEMO_NAME "http_demo_s3_download" )
# Include HTTP library's source and header path variables.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake )

# Include backoffAlgorithm library file path configuration.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake )

# Demo target.
add_executable(
${DEMO_NAME}
"${DEMO_NAME}.c"
"../common/src/http_demo_utils.c"
"${DEMOS_DIR}/http/common/src/http_demo_utils.c"
${HTTP_SOURCES}
${HTTP_THIRD_PARTY_SOURCES}
${BACKOFF_ALGORITHM_SOURCES}
)

# Add to default target if all required macros needed to run this demo are defined
Expand All @@ -19,14 +23,14 @@ target_link_libraries(
${DEMO_NAME}
PRIVATE
openssl_posix
retry_utils
)

target_include_directories(
${DEMO_NAME}
PUBLIC
"../common/include"
"${DEMOS_DIR}/http/common/include"
${HTTP_INCLUDE_PUBLIC_DIRS}
${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS}
${HTTP_INCLUDE_THIRD_PARTY_DIRS}
${HTTP_INCLUDE_PRIVATE_DIRS}
${CMAKE_CURRENT_LIST_DIR}
Expand Down
4 changes: 2 additions & 2 deletions demos/http/http_demo_s3_download/http_demo_s3_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
/* OpenSSL transport header. */
#include "openssl_posix.h"

/* Retry utilities. */
#include "retry_utils.h"
/*Include backoff algorithm header for retry logic.*/
#include "backoff_algorithm.h"

/* Check that TLS port of the server is defined. */
#ifndef HTTPS_PORT
Expand Down
10 changes: 7 additions & 3 deletions demos/http/http_demo_s3_download_multithreaded/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ set( DEMO_NAME "http_demo_s3_download_multithreaded" )
# Include HTTP library's source and header path variables.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake )

# Include backoffAlgorithm library file path configuration.
include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake )

# Demo target.
add_executable(
${DEMO_NAME}
"${DEMO_NAME}.c"
"../common/src/http_demo_utils.c"
"${DEMOS_DIR}/http/common/src/http_demo_utils.c"
${HTTP_SOURCES}
${HTTP_THIRD_PARTY_SOURCES}
${BACKOFF_ALGORITHM_SOURCES}
)

# Add to default target if all required macros needed to run this demo are defined
Expand All @@ -19,15 +23,15 @@ target_link_libraries(
${DEMO_NAME}
PRIVATE
openssl_posix
retry_utils
rt
)

target_include_directories(
${DEMO_NAME}
PUBLIC
"../common/include"
"${DEMOS_DIR}/http/common/include"
${HTTP_INCLUDE_PUBLIC_DIRS}
${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS}
${HTTP_INCLUDE_THIRD_PARTY_DIRS}
${HTTP_INCLUDE_PRIVATE_DIRS}
${CMAKE_CURRENT_LIST_DIR}
Expand Down
Loading

0 comments on commit 7b4f726

Please sign in to comment.