| 21481 |
rajender |
1 |
/*
|
|
|
2 |
* Copyright (C) 2011 The Android Open Source Project
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package com.android.volley;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Default retry policy for requests.
|
|
|
21 |
*/
|
|
|
22 |
public class DefaultRetryPolicy implements RetryPolicy {
|
|
|
23 |
/** The current timeout in milliseconds. */
|
|
|
24 |
private int mCurrentTimeoutMs;
|
|
|
25 |
|
|
|
26 |
/** The current retry count. */
|
|
|
27 |
private int mCurrentRetryCount;
|
|
|
28 |
|
|
|
29 |
/** The maximum number of attempts. */
|
|
|
30 |
private final int mMaxNumRetries;
|
|
|
31 |
|
|
|
32 |
/** The backoff multiplier for the policy. */
|
|
|
33 |
private final float mBackoffMultiplier;
|
|
|
34 |
|
|
|
35 |
/** The default socket timeout in milliseconds */
|
|
|
36 |
public static final int DEFAULT_TIMEOUT_MS = 2500;
|
|
|
37 |
|
|
|
38 |
/** The default number of retries */
|
|
|
39 |
public static final int DEFAULT_MAX_RETRIES = 1;
|
|
|
40 |
|
|
|
41 |
/** The default backoff multiplier */
|
|
|
42 |
public static final float DEFAULT_BACKOFF_MULT = 1f;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Constructs a new retry policy using the default timeouts.
|
|
|
46 |
*/
|
|
|
47 |
public DefaultRetryPolicy() {
|
|
|
48 |
this(DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Constructs a new retry policy.
|
|
|
53 |
* @param initialTimeoutMs The initial timeout for the policy.
|
|
|
54 |
* @param maxNumRetries The maximum number of retries.
|
|
|
55 |
* @param backoffMultiplier Backoff multiplier for the policy.
|
|
|
56 |
*/
|
|
|
57 |
public DefaultRetryPolicy(int initialTimeoutMs, int maxNumRetries, float backoffMultiplier) {
|
|
|
58 |
mCurrentTimeoutMs = initialTimeoutMs;
|
|
|
59 |
mMaxNumRetries = maxNumRetries;
|
|
|
60 |
mBackoffMultiplier = backoffMultiplier;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Returns the current timeout.
|
|
|
65 |
*/
|
|
|
66 |
@Override
|
|
|
67 |
public int getCurrentTimeout() {
|
|
|
68 |
return mCurrentTimeoutMs;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Returns the current retry count.
|
|
|
73 |
*/
|
|
|
74 |
@Override
|
|
|
75 |
public int getCurrentRetryCount() {
|
|
|
76 |
return mCurrentRetryCount;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Returns the backoff multiplier for the policy.
|
|
|
81 |
*/
|
|
|
82 |
public float getBackoffMultiplier() {
|
|
|
83 |
return mBackoffMultiplier;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Prepares for the next retry by applying a backoff to the timeout.
|
|
|
88 |
* @param error The error code of the last attempt.
|
|
|
89 |
*/
|
|
|
90 |
@Override
|
|
|
91 |
public void retry(VolleyError error) throws VolleyError {
|
|
|
92 |
mCurrentRetryCount++;
|
|
|
93 |
mCurrentTimeoutMs += (mCurrentTimeoutMs * mBackoffMultiplier);
|
|
|
94 |
if (!hasAttemptRemaining()) {
|
|
|
95 |
throw error;
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Returns true if this policy has attempts remaining, false otherwise.
|
|
|
101 |
*/
|
|
|
102 |
protected boolean hasAttemptRemaining() {
|
|
|
103 |
return mCurrentRetryCount <= mMaxNumRetries;
|
|
|
104 |
}
|
|
|
105 |
}
|