This repository contains the backoffAlgorithm library, a utility library to calculate backoff period using an exponential backoff with jitter algorithm for retrying network operations (like failed network connection with server). This library uses the "Full Jitter" strategy for the exponential backoff with jitter algorithm. More information about the algorithm can be seen in the Exponential Backoff and Jitter AWS blog.
The backoffAlgorithm library is distributed under the MIT Open Source License.
Exponential backoff with jitter is typically used when retrying a failed network connection or operation request with the server. An exponential backoff with jitter helps to mitigate failed network operations with servers, that are caused due to network congestion or high request load on the server, by spreading out retry requests across multiple devices attempting network operations. Besides, in an environment with poor connectivity, a client can get disconnected at any time. A backoff strategy helps the client to conserve battery by not repeatedly attempting reconnections when they are unlikely to succeed.
The example below shows how to use the backoffAlgorithm library to retry a DNS resolution query for amazon.com
.
#include "backoff_algorithm.h"
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <time.h>
/* The maximum number of retries for the example code. */
#define RETRY_MAX_ATTEMPTS ( 5U )
/* The maximum back-off delay (in milliseconds) for between retries in the example. */
#define RETRY_MAX_BACKOFF_DELAY_MS ( 5000U )
/* The base back-off delay (in milliseconds) for retry configuration in the example. */
#define RETRY_BACKOFF_BASE_MS ( 500U )
int main()
{
/* Variables used in this example. */
BackoffAlgorithmStatus_t retryStatus = BackoffAlgorithmSuccess;
BackoffAlgorithmContext_t retryParams;
char serverAddress[] = "amazon.com";
uint16_t nextRetryBackOff = 0;
int32_t dnsStatus = -1;
struct addrinfo hints;
struct addrinfo ** pListHead;
struct timespec tp;
/* Add hints to retrieve only TCP sockets in getaddrinfo. */
( void ) memset( &hints, 0, sizeof( hints ) );
/* Address family of either IPv4 or IPv6. */
hints.ai_family = AF_UNSPEC;
/* TCP Socket. */
hints.ai_socktype = ( int32_t ) SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
/* Initialize reconnect attempts and interval. */
BackoffAlgorithm_InitializeParams( &retryParams,
RETRY_BACKOFF_BASE_MS,
RETRY_MAX_BACKOFF_DELAY_MS,
RETRY_MAX_ATTEMPTS );
/* Seed the pseudo random number generator used in this example (with call to
* rand() function provided by ISO C standard library) for use in backoff period
* calculation when retrying failed DNS resolution. */
/* Get current time to seed pseudo random number generator. */
( void ) clock_gettime( CLOCK_REALTIME, &tp );
/* Seed pseudo random number generator with seconds. */
srand( tp.tv_sec );
do
{
/* Perform a DNS lookup on the given host name. */
dnsStatus = getaddrinfo( serverAddress, NULL, &hints, pListHead );
/* Retry if DNS resolution query failed. */
if( dnsStatus != 0 )
{
/* Generate a random number and get back-off value (in milliseconds) for the next retry.
* Note: It is recommended to use a random number generator that is seeded with
* device-specific entropy source so that backoff calculation in devices is different
* and possibility of network collision between devices attempting retries can be avoided.
*
* For the simplicity of the code example, the pseudo random number generator, rand() function
* is used. */
retryStatus = BackoffAlgorithm_GetNextBackoff( &retryParams, rand(), &nextRetryBackOff );
}
} while( ( dnsStatus != 0 ) && ( retryStatus != BackoffAlgorithmRetriesExhausted ) );
return dnsStatus;
}
A compiler that supports C89 or later such as gcc is required to build the library.
For instance, if the example above is copied to a file named example.c
, gcc can be used like so:
gcc -I source/include example.c source/backoff_algorithm.c -o example
./example
gcc can also produce an output file to be linked:
gcc -I source/include -c source/backoff_algorithm.c
By default, the submodules in this repository are configured with update=none
in .gitmodules, to avoid increasing clone time and disk space usage of other repositories (like amazon-freertos that submodules this repository).
To build unit tests, the submodule dependency of Unity is required. Use the following command to clone the submodule:
git submodule update --checkout --init --recursive --test/unit-test/Unity
Go to the root directory of this repository. (Make sure that the Unity submodule is cloned as described above.)
Create build directory: mkdir build && cd build
Run cmake while inside build directory: cmake -S ../test
Run this command to build the library and unit tests: make all
The generated test executables will be present in build/bin/tests
folder.
Run ctest
to execute all tests and view the test run summary.
See CONTRIBUTING.md for information on contributing.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。