Skip to content

Commit 36d0e9b

Browse files
authored
Fix reference example in README (#18)
1 parent a702914 commit 36d0e9b

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ See memory requirements for this library [here](https://docs.aws.amazon.com/embe
2020

2121
## Reference example
2222

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`.
2424

2525
```c
2626
#include "backoff_algorithm.h"
2727
#include <stdlib.h>
2828
#include <string.h>
2929
#include <netdb.h>
30+
#include <unistd.h>
3031
#include <time.h>
3132

3233
/* The maximum number of retries for the example code. */
@@ -44,11 +45,11 @@ int main()
4445
BackoffAlgorithmStatus_t retryStatus = BackoffAlgorithmSuccess;
4546
BackoffAlgorithmContext_t retryParams;
4647
char serverAddress[] = "amazon.com";
47-
uint16_t nextRetryBackOff = 0;
48+
uint16_t nextRetryBackoff = 0;
4849

4950
int32_t dnsStatus = -1;
5051
struct addrinfo hints;
51-
struct addrinfo ** pListHead;
52+
struct addrinfo ** pListHead = NULL;
5253
struct timespec tp;
5354

5455
/* Add hints to retrieve only TCP sockets in getaddrinfo. */
@@ -86,12 +87,19 @@ int main()
8687
{
8788
/* Generate a random number and get back-off value (in milliseconds) for the next retry.
8889
* 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
9091
* and possibility of network collision between devices attempting retries can be avoided.
9192
*
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 );
95103
}
96104
} while( ( dnsStatus != 0 ) && ( retryStatus != BackoffAlgorithmRetriesExhausted ) );
97105

0 commit comments

Comments
 (0)