What does backoffMultiplier mean in DefaultRetryPolicy?
Asked Answered
C

2

16

I have created new Job retry policy : new DefaultRetryPolicy(5000, 2, 2);

So, What does backoffMultiplier mean in DefaultRetryPolicy?

Contusion answered 5/4, 2017 at 7:32 Comment(0)
C
16

RetryPolicy deals with these three parameters

Timeout - Specifies Socket Timeout in millis per every retry attempt.

Number Of Retries - Number of times retry is attempted.

BackOff Multiplier - A multiplier which is used to determine exponential time set to socket for every retry attempt.

For above example

Timeout - 5000 secs, Num of Attempt - 2, Back Off Multiplier - 2

Attempt 1:

time = time + (time * Back Off Multiplier );

time = 5000 + 10000 = 15000

Socket Timeout = time;

Request dispatched with Socket Timeout of 15 Secs

Attempt 2:

time = time + (time * Back Off Multiplier );

time = 15000 + 30000 = 45000

Socket Timeout = time;

Request dispatched with Socket Timeout of 45 Secs

So at the end of Attempt 2 if still Socket Timeout happenes Volley would throw a TimeoutError in your UI Error response handler.

Contusion answered 5/4, 2017 at 7:32 Comment(3)
"Timeout - 5000 secs" Here 5000 is milliseconds, right?Diminutive
Yes, Timeout is 5 secondsContusion
Answer is copy/paste from here beginners-java.blogspot.fi/2015/03/…Pelvic
P
21

The marked answer is copied off answer from this blog, without credits :(

Here is an explanation of how code calculates it... You can look at the DefaultRetryPolicy.java it is open source after all...

When it is initialized, it uses user given values:

    /**
     * Constructs a new retry policy.
     *
     * @param initialTimeoutMs The initial timeout for the policy.
     * @param maxNumRetries The maximum number of retries.
     * @param backoffMultiplier Backoff multiplier for the policy.
     */
    public DefaultRetryPolicy(int initialTimeoutMs, int maxNumRetries, float backoffMultiplier) {
        mCurrentTimeoutMs = initialTimeoutMs;
        mMaxNumRetries = maxNumRetries;
        mBackoffMultiplier = backoffMultiplier;
}

Therefore when code checks current timeout, it receive the initialTimeoutMs value for the first request (not the retry request, original request).

    /** Returns the current timeout. */
    @Override
    public int getCurrentTimeout() {
        return mCurrentTimeoutMs;
    }

If a retry() is called then it re-calculates the timeout (for the retrt request 2nd, 3rd etc.):

/**
 * Prepares for the next retry by applying a backoff to the timeout.
 *
 * @param error The error code of the last attempt.
 */
@Override
public void retry(VolleyError error) throws VolleyError {
    mCurrentRetryCount++;
    mCurrentTimeoutMs += (int) (mCurrentTimeoutMs * mBackoffMultiplier);
    if (!hasAttemptRemaining()) {
        throw error;
    }
}

So the back off multiplier is only added if 1st request fails, in the 2nd request (retry request) and so on...

With initialTimeoutMs = 5000ms, maxNumRetries = 2, backoffMultiplier = 2

Timeout for 1st request 5s Timeout for 2nd request(retry) 5+10 = 15s Timeout for 3rd request(retry) 15+30 = 45s

So there would be 1 normal request + 2 retries. Totaling 3 requests, because of the code

/** Returns true if this policy has attempts remaining, false otherwise. */
protected boolean hasAttemptRemaining() {
    return mCurrentRetryCount <= mMaxNumRetries;
}

After 1st request mCurrentRetryCount is 0, after 2nd request 1, and after 3rd request 2. Only on 4th request it would return false Because mCurrentRetryCount is increased only from the retry() method.

Pelvic answered 14/4, 2018 at 16:22 Comment(0)
C
16

RetryPolicy deals with these three parameters

Timeout - Specifies Socket Timeout in millis per every retry attempt.

Number Of Retries - Number of times retry is attempted.

BackOff Multiplier - A multiplier which is used to determine exponential time set to socket for every retry attempt.

For above example

Timeout - 5000 secs, Num of Attempt - 2, Back Off Multiplier - 2

Attempt 1:

time = time + (time * Back Off Multiplier );

time = 5000 + 10000 = 15000

Socket Timeout = time;

Request dispatched with Socket Timeout of 15 Secs

Attempt 2:

time = time + (time * Back Off Multiplier );

time = 15000 + 30000 = 45000

Socket Timeout = time;

Request dispatched with Socket Timeout of 45 Secs

So at the end of Attempt 2 if still Socket Timeout happenes Volley would throw a TimeoutError in your UI Error response handler.

Contusion answered 5/4, 2017 at 7:32 Comment(3)
"Timeout - 5000 secs" Here 5000 is milliseconds, right?Diminutive
Yes, Timeout is 5 secondsContusion
Answer is copy/paste from here beginners-java.blogspot.fi/2015/03/…Pelvic

© 2022 - 2024 — McMap. All rights reserved.