BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff()

in source/backoff_algorithm.c [38:80]


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;
}