backoffAlgorithm v1.3.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.3.0
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
35#ifndef BACKOFF_ALGORITHM_H_
36#define BACKOFF_ALGORITHM_H_
37
38/* Standard include. */
39#include <stdint.h>
40
41/* *INDENT-OFF* */
42#ifdef __cplusplus
43 extern "C" {
44#endif
45/* *INDENT-ON* */
46
51#define BACKOFF_ALGORITHM_RETRY_FOREVER ( UINT32_MAX )
52
57typedef enum BackoffAlgorithmStatus
58{
62
68typedef struct BackoffAlgorithmContext
69{
74
79 uint32_t attemptsDone;
80
84 uint16_t nextJitterMax;
85
91
106/* @[define_backoffalgorithm_initializeparams] */
108 uint16_t backOffBase,
109 uint16_t maxBackOff,
110 uint32_t maxAttempts );
111/* @[define_backoffalgorithm_initializeparams] */
112
135/* @[define_backoffalgorithm_getnextbackoff] */
137 uint32_t randomValue,
138 uint16_t * pNextBackOff );
139/* @[define_backoffalgorithm_getnextbackoff] */
140
141/* *INDENT-OFF* */
142#ifdef __cplusplus
143 }
144#endif
145/* *INDENT-ON* */
146
147#endif /* ifndef BACKOFF_ALGORITHM_H_ */
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
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
uint32_t maxRetryAttempts
The maximum number of retry attempts.
Definition: backoff_algorithm.h:89
uint16_t maxBackoffDelay
The maximum backoff delay (in milliseconds) between consecutive retry attempts.
Definition: backoff_algorithm.h:73
uint16_t nextJitterMax
The maximum backoff value (in milliseconds) for the next retry attempt.
Definition: backoff_algorithm.h:84
uint32_t attemptsDone
The total number of retry attempts completed. This value is incremented on every call to BackoffAlgor...
Definition: backoff_algorithm.h:79