This repository has been archived by the owner on Jan 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 546
/
DefaultRetryPolicy.java
71 lines (56 loc) · 2.19 KB
/
DefaultRetryPolicy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.octo.android.robospice.retry;
import com.octo.android.robospice.persistence.exception.SpiceException;
/**
* Default {@link RetryPolicy} implementation. Proposes an exponential back off
* algorithm. When {@link #getRetryCount()} returns 0, the request is not
* retried anymore and will fail. Between each retry attempt, the request
* processor will sleep for {@link #getDelayBeforeRetry()} milliseconds.
* @author SNI
*/
public class DefaultRetryPolicy implements RetryPolicy {
/** The default number of retry attempts. */
public static final int DEFAULT_RETRY_COUNT = 3;
/** The default delay before retry a request (in ms). */
public static final long DEFAULT_DELAY_BEFORE_RETRY = 2500;
/** The default backoff multiplier. */
public static final float DEFAULT_BACKOFF_MULT = 1f;
/** The number of retry attempts. */
private int retryCount = DEFAULT_RETRY_COUNT;
/**
* The delay to wait before next retry attempt. Will be multiplied by
* {@link #backOffMultiplier} between every retry attempt.
*/
private long delayBeforeRetry = DEFAULT_DELAY_BEFORE_RETRY;
/**
* The backoff multiplier. Will be multiplied by {@link #delayBeforeRetry}
* between every retry attempt.
*/
private float backOffMultiplier = DEFAULT_BACKOFF_MULT;
// ----------------------------------
// CONSTRUCTORS
// ----------------------------------
public DefaultRetryPolicy(int retryCount, long delayBeforeRetry, float backOffMultiplier) {
this.retryCount = retryCount;
this.delayBeforeRetry = delayBeforeRetry;
this.backOffMultiplier = backOffMultiplier;
}
public DefaultRetryPolicy() {
this(DEFAULT_RETRY_COUNT, DEFAULT_DELAY_BEFORE_RETRY, DEFAULT_BACKOFF_MULT);
}
// ----------------------------------
// PUBLIC API
// ----------------------------------
@Override
public int getRetryCount() {
return retryCount;
}
@Override
public void retry(SpiceException e) {
retryCount--;
delayBeforeRetry = (long) (delayBeforeRetry * backOffMultiplier);
}
@Override
public long getDelayBeforeRetry() {
return delayBeforeRetry;
}
}