@@ -20,13 +20,14 @@ See memory requirements for this library [here](https://docs.aws.amazon.com/embe
20
20
21
21
## Reference example
22
22
23
- The example below shows how to use the backoffAlgorithm library to retry a DNS resolution query for ` amazon.com ` .
23
+ The example below shows how to use the backoffAlgorithm library on a POSIX platform to retry a DNS resolution query for ` amazon.com ` .
24
24
25
25
``` c
26
26
#include " backoff_algorithm.h"
27
27
#include < stdlib.h>
28
28
#include < string.h>
29
29
#include < netdb.h>
30
+ #include < unistd.h>
30
31
#include < time.h>
31
32
32
33
/* The maximum number of retries for the example code. */
@@ -44,11 +45,11 @@ int main()
44
45
BackoffAlgorithmStatus_t retryStatus = BackoffAlgorithmSuccess;
45
46
BackoffAlgorithmContext_t retryParams;
46
47
char serverAddress[] = "amazon.com";
47
- uint16_t nextRetryBackOff = 0;
48
+ uint16_t nextRetryBackoff = 0;
48
49
49
50
int32_t dnsStatus = -1;
50
51
struct addrinfo hints;
51
- struct addrinfo ** pListHead;
52
+ struct addrinfo ** pListHead = NULL ;
52
53
struct timespec tp;
53
54
54
55
/* Add hints to retrieve only TCP sockets in getaddrinfo. */
@@ -86,12 +87,19 @@ int main()
86
87
{
87
88
/* Generate a random number and get back-off value (in milliseconds) for the next retry.
88
89
* Note: It is recommended to use a random number generator that is seeded with
89
- * device-specific entropy source so that backoff calculation in devices is different
90
+ * device-specific entropy source so that backoff calculation across devices is different
90
91
* and possibility of network collision between devices attempting retries can be avoided.
91
92
*
92
- * For the simplicity of the code example, the pseudo random number generator, rand() function
93
- * is used. */
94
- retryStatus = BackoffAlgorithm_GetNextBackoff( &retryParams, rand(), &nextRetryBackOff );
93
+ * For the simplicity of this code example, the pseudo random number generator, rand()
94
+ * function is used. */
95
+ retryStatus = BackoffAlgorithm_GetNextBackoff( &retryParams, rand(), &nextRetryBackoff );
96
+
97
+ /* Wait for the calculated backoff period before the next retry attempt of querying DNS.
98
+ * As usleep() takes nanoseconds as the parameter, we multiply the backoff period by 1000. */
99
+ ( void ) usleep( nextRetryBackoff * 1000U );
100
+
101
+ /* Retry the DNS lookup for the host name. */
102
+ dnsStatus = getaddrinfo( serverAddress, NULL, &hints, pListHead );
95
103
}
96
104
} while( ( dnsStatus != 0 ) && ( retryStatus != BackoffAlgorithmRetriesExhausted ) );
97
105
0 commit comments