backoffAlgorithm  v1.0.0
Algorithmic library for calculating retry intervals using exponential backoff and jitter.
backoff_algorithm.h
Go to the documentation of this file.
1 /*
2  * backoffAlgorithm v1.0.0
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
33 #ifndef BACKOFF_ALGORITHM_H_
34 #define BACKOFF_ALGORITHM_H_
35 
36 /* Standard include. */
37 #include <stdint.h>
38 
43 #define BACKOFF_ALGORITHM_RETRY_FOREVER 0
44 
49 typedef enum BackoffAlgorithmStatus
50 {
54 
60 typedef struct BackoffAlgorithmContext
61 {
65  uint16_t maxBackoffDelay;
66 
71  uint32_t attemptsDone;
72 
76  uint16_t nextJitterMax;
77 
81  uint32_t maxRetryAttempts;
83 
98 /* @[define_backoffalgorithm_initializeparams] */
100  uint16_t backOffBase,
101  uint16_t maxBackOff,
102  uint32_t maxAttempts );
103 /* @[define_backoffalgorithm_initializeparams] */
104 
127 /* @[define_backoffalgorithm_getnextbackoff] */
129  uint32_t randomValue,
130  uint16_t * pNextBackOff );
131 /* @[define_backoffalgorithm_getnextbackoff] */
132 
133 #endif /* ifndef BACKOFF_ALGORITHM_H_ */
BackoffAlgorithmContext_t::maxRetryAttempts
uint32_t maxRetryAttempts
The maximum number of retry attempts.
Definition: backoff_algorithm.h:81
BackoffAlgorithmContext_t::maxBackoffDelay
uint16_t maxBackoffDelay
The maximum backoff delay (in milliseconds) between consecutive retry attempts.
Definition: backoff_algorithm.h:65
BackoffAlgorithmContext_t
Represents parameters required for calculating the back-off delay for the next retry attempt.
Definition: backoff_algorithm.h:61
BackoffAlgorithmStatus_t
BackoffAlgorithmStatus_t
Status for BackoffAlgorithm_GetNextBackoff.
Definition: backoff_algorithm.h:50
BackoffAlgorithm_InitializeParams
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:84
BackoffAlgorithmRetriesExhausted
@ BackoffAlgorithmRetriesExhausted
The function exhausted all retry attempts.
Definition: backoff_algorithm.h:52
BackoffAlgorithmContext_t::nextJitterMax
uint16_t nextJitterMax
The maximum backoff value (in milliseconds) for the next retry attempt.
Definition: backoff_algorithm.h:76
BackoffAlgorithmSuccess
@ BackoffAlgorithmSuccess
The function successfully calculated the next back-off value.
Definition: backoff_algorithm.h:51
BackoffAlgorithmContext_t::attemptsDone
uint32_t attemptsDone
The total number of retry attempts completed. This value is incremented on every call to BackoffAlgor...
Definition: backoff_algorithm.h:71
BackoffAlgorithm_GetNextBackoff
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