| 1 | /* | 
|---|---|
| 2 | * IXExponentialBackoff.cpp | 
| 3 | * Author: Benjamin Sergeant | 
| 4 | * Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved. | 
| 5 | */ | 
| 6 | |
| 7 | #include "IXExponentialBackoff.h" | 
| 8 | |
| 9 | #include <cmath> | 
| 10 | |
| 11 | namespace ix | 
| 12 | { | 
| 13 | uint32_t calculateRetryWaitMilliseconds(uint32_t retryCount, | 
| 14 | uint32_t maxWaitBetweenReconnectionRetries, | 
| 15 | uint32_t minWaitBetweenReconnectionRetries) | 
| 16 | { | 
| 17 | // It's easy with a power function to go beyond 2^32, and then | 
| 18 | // have unexpected results, so prepare for that | 
| 19 | const uint32_t maxRetryCountWithoutOverflow = 26; | 
| 20 | |
| 21 | uint32_t waitTime = 0; | 
| 22 | if (retryCount < maxRetryCountWithoutOverflow) | 
| 23 | { | 
| 24 | waitTime = std::pow(2, retryCount) * 100; | 
| 25 | } | 
| 26 | |
| 27 | if (waitTime < minWaitBetweenReconnectionRetries) | 
| 28 | { | 
| 29 | waitTime = minWaitBetweenReconnectionRetries; | 
| 30 | } | 
| 31 | |
| 32 | if (waitTime > maxWaitBetweenReconnectionRetries) | 
| 33 | { | 
| 34 | waitTime = maxWaitBetweenReconnectionRetries; | 
| 35 | } | 
| 36 | |
| 37 | if (retryCount >= maxRetryCountWithoutOverflow) | 
| 38 | { | 
| 39 | waitTime = maxWaitBetweenReconnectionRetries; | 
| 40 | } | 
| 41 | |
| 42 | return waitTime; | 
| 43 | } | 
| 44 | } // namespace ix | 
| 45 |