Created
August 2, 2023 17:44
-
-
Save NorseGaud/c0615901889dc4d2ca431ff68933da2c to your computer and use it in GitHub Desktop.
BASH script: exponential backoff with limit of 60s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -E # required to get exit code from while () | |
BASE=${BACKOFF_BASE:-1} | |
MAX=${BACKOFF_MAX:-60} | |
FAILURES=0 | |
while ( | |
# Code here | |
) 2>&1; RC=$?; [[ $RC -ne 0 ]]; do | |
FAILURES=$(( $FAILURES + 1 )) | |
SECONDS=$(( ($BASE * 2) ** ($FAILURES - 1) )) | |
if [[ $SECONDS -ge $MAX ]]; then | |
SECONDS=$MAX | |
FAILURES=$(( $FAILURES - 1 )) # make sure SECONDS doesn't grow too large | |
fi | |
print_warning "$FAILURES failure(s), retrying in $SECONDS second(s)" >&2 | |
sleep $SECONDS | |
echo | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment