-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbackoff_algorithm.c
More file actions
103 lines (87 loc) · 4.11 KB
/
Copy pathbackoff_algorithm.c
File metadata and controls
103 lines (87 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* backoffAlgorithm v1.4.1
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file backoff_algorithm.c
* @brief Implementation of the backoff algorithm API for a "Full Jitter" exponential backoff
* with jitter strategy.
*/
/* Standard includes. */
#include <assert.h>
#include <stddef.h>
/* Include API header. */
#include "backoff_algorithm.h"
/*-----------------------------------------------------------*/
BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff( BackoffAlgorithmContext_t * pRetryContext,
uint32_t randomValue,
uint16_t * pNextBackOff )
{
BackoffAlgorithmStatus_t status = BackoffAlgorithmSuccess;
assert( pRetryContext != NULL );
assert( pNextBackOff != NULL );
/* If maxRetryAttempts state of the context is set to the maximum, retry forever. */
if( ( pRetryContext->maxRetryAttempts == BACKOFF_ALGORITHM_RETRY_FOREVER ) ||
( pRetryContext->attemptsDone < pRetryContext->maxRetryAttempts ) )
{
/* The next backoff value is a random value between 0 and the maximum jitter value
* for the retry attempt. */
/* Choose a random value for back-off time between 0 and the max jitter value. */
*pNextBackOff = ( uint16_t ) ( randomValue % ( pRetryContext->nextJitterMax + ( uint32_t ) 1U ) );
/* Increment the retry attempt. */
pRetryContext->attemptsDone++;
/* Double the max jitter value for the next retry attempt, only
* if the new value will be less than the max backoff time value. */
if( pRetryContext->nextJitterMax < ( pRetryContext->maxBackoffDelay / 2U ) )
{
pRetryContext->nextJitterMax += pRetryContext->nextJitterMax;
}
else
{
pRetryContext->nextJitterMax = pRetryContext->maxBackoffDelay;
}
}
else
{
/* When max retry attempts are exhausted, let application know by
* returning BackoffAlgorithmRetriesExhausted. Application may choose to
* restart the retry process after calling BackoffAlgorithm_InitializeParams(). */
status = BackoffAlgorithmRetriesExhausted;
}
return status;
}
/*-----------------------------------------------------------*/
void BackoffAlgorithm_InitializeParams( BackoffAlgorithmContext_t * pContext,
uint16_t backOffBase,
uint16_t maxBackOff,
uint32_t maxAttempts )
{
assert( pContext != NULL );
/* Initialize the context with parameters used in calculating the backoff
* value for the next retry attempt. */
pContext->nextJitterMax = backOffBase;
pContext->maxBackoffDelay = maxBackOff;
pContext->maxRetryAttempts = maxAttempts;
/* The total number of retry attempts is zero at initialization. */
pContext->attemptsDone = 0;
}
/*-----------------------------------------------------------*/