backoffAlgorithm  v1.1.0
Algorithmic library for calculating retry intervals using exponential backoff and jitter.

Simple exponential backoff and jitter function that provides the delay value for the next retry attempt. After a failure of an operation that needs to be retried, the application should use this function to obtain the backoff delay value for the next retry, and then wait for the backoff time period before retrying the operation.

[in,out]pRetryContextStructure containing parameters for the next backoff value calculation.
[in]randomValueThe random value to use for calculation of the backoff period. The random value should be in the range of [0, UINT32_MAX].
[out]pNextBackOffThis will be populated with the backoff value (in milliseconds) for the next retry attempt. The value does not exceed the maximum backoff delay configured in the context.
For generating a random number, it is recommended to use a Random Number Generator that is seeded with a device-specific entropy source so that possibility of collisions between multiple devices retrying the network operations can be mitigated.
BackoffAlgorithmSuccess after a successful sleep; BackoffAlgorithmRetriesExhausted when all attempts are exhausted.

From the Code Example for backoffAlgorithm API, following is the part relevant to the BackoffAlgorithm_GetNextBackoff API.

/* 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 );
/* 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 across devices is different
* and possibility of network collision between devices attempting retries can be avoided.
* For the simplicity of this code example, the pseudo random number generator, rand()
* function is used. */
retryStatus = BackoffAlgorithm_GetNextBackoff( &retryParams, rand(), &nextRetryBackoff );
/* Wait for the calculated backoff period before the next retry attempt of querying DNS.
* As usleep() takes nanoseconds as the parameter, we multiply the backoff period by 1000. */
( void ) usleep( nextRetryBackoff * 1000U );
} while( ( dnsStatus != 0 ) && ( retryStatus != BackoffAlgorithmRetriesExhausted ) );
@ BackoffAlgorithmRetriesExhausted
The function exhausted all retry attempts.
Definition: backoff_algorithm.h:58
BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff(BackoffAlgorithmContext_t *pRetryContext, uint32_t randomValue, uint16_t *pNextBackOff)
Simple exponential backoff and jitter function that provides the delay value for the next retry attem...
Definition: backoff_algorithm.c:38