Example POSIX application that retries DNS resolution operation with exponential backoff-and-jitter using the backoffAlgorithm library.
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <time.h>
#define RETRY_MAX_ATTEMPTS ( 5U )
#define RETRY_MAX_BACKOFF_DELAY_MS ( 5000U )
#define RETRY_BACKOFF_BASE_MS ( 500U )
int main()
{
char serverAddress[] = "amazon.com";
uint16_t nextRetryBackoff = 0;
RETRY_BACKOFF_BASE_MS,
RETRY_MAX_BACKOFF_DELAY_MS,
RETRY_MAX_ATTEMPTS );
int32_t dnsStatus = -1;
struct addrinfo hints;
struct addrinfo ** pListHead = NULL;
struct timespec tp;
( void ) memset( &hints, 0, sizeof( hints ) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = ( int32_t ) SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
( void ) clock_gettime( CLOCK_REALTIME, &tp );
srand( tp.tv_sec );
do
{
dnsStatus = getaddrinfo( serverAddress, NULL, &hints, pListHead );
if( dnsStatus != 0 )
{
( void ) usleep( nextRetryBackoff * 1000U );
}
return dnsStatus;
}
void BackoffAlgorithm_InitializeParams(BackoffAlgorithmContext_t *pContext, uint16_t backOffBase, uint16_t maxBackOff, uint32_t maxAttempts)
Initializes the context for using backoff algorithm. The parameters are required for calculating the ...
Definition: backoff_algorithm.c:86
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:40
API for calculating backoff period for retry attempts using exponential backoff with jitter algorithm...
BackoffAlgorithmStatus_t
Status for BackoffAlgorithm_GetNextBackoff.
Definition: backoff_algorithm.h:58
@ BackoffAlgorithmRetriesExhausted
The function exhausted all retry attempts.
Definition: backoff_algorithm.h:60
@ BackoffAlgorithmSuccess
The function successfully calculated the next back-off value.
Definition: backoff_algorithm.h:59
Represents parameters required for calculating the back-off delay for the next retry attempt.
Definition: backoff_algorithm.h:69