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;
}