sh Bash中的指数退避

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh Bash中的指数退避相关的知识,希望对你有一定的参考价值。

expbackoff() {
    # Exponential backoff: retries a command upon failure, scaling up the delay between retries.
    # Example: "expbackoff my_command --with --some --args --maybe"
    local MAX_RETRIES=${EXPBACKOFF_MAX_RETRIES:-8} # Max number of retries
    local BASE=${EXPBACKOFF_BASE:-1} # Base value for backoff calculation
    local MAX=${EXPBACKOFF_MAX:-300} # Max value for backoff calculation
    local FAILURES=0
    while ! "$@"; do
        FAILURES=$(( $FAILURES + 1 ))
        if (( $FAILURES > $MAX_RETRIES )); then
            echo "$@" >&2
            echo " * Failed, max retries exceeded" >&2
            return 1
        else
            local SECONDS=$(( $BASE * 2 ** ($FAILURES - 1) ))
            if (( $SECONDS > $MAX )); then
                SECONDS=$MAX
            fi
            echo "$@" >&2
            echo " * $FAILURES failure(s), retrying in $SECONDS second(s)" >&2
            sleep $SECONDS
            echo
        fi
    done
}

something_that_succeeds() { echo "I'm a winner!"; }
something_that_fails() { echo "I'm a loser :("; return 1; }

EXPBACKOFF_MAX_RETRIES=3 # Override default value - speeds up testing
expbackoff something_that_succeeds --calling it with -args
echo # Clear up the display
expbackoff something_that_fails --calling it with -args
echo
echo $? # Should be 1, indicating overall failure of `something_that_fails`

以上是关于sh Bash中的指数退避的主要内容,如果未能解决你的问题,请参考以下文章

python中的指数退避实现

RxJava 中的指数退避

AWS 中的错误重试和指数退避 Error Retries and Exponential Backoff in AWS

AWS 中的错误重试和指数退避

算法6指数退避算法

二进制指数退避算法