|
13 | 13 | import org.apache.http.protocol.HttpCoreContext; |
14 | 14 | import org.apache.http.util.Args; |
15 | 15 |
|
| 16 | +import com.microsoft.graph.httpcore.middlewareoption.MiddlewareType; |
| 17 | +import com.microsoft.graph.httpcore.middlewareoption.RetryOptions; |
| 18 | + |
16 | 19 | public class RetryHandler implements ServiceUnavailableRetryStrategy{ |
17 | 20 |
|
18 | 21 | /** |
19 | 22 | * Maximum number of allowed retries if the server responds with a HTTP code |
20 | 23 | * in our retry code list. Default value is 1. |
21 | 24 | */ |
22 | | - private final int maxRetries; |
| 25 | + private final int maxRetries = 2; |
23 | 26 |
|
24 | 27 | /** |
25 | 28 | * Retry interval between subsequent requests, in milliseconds. Default |
26 | 29 | * value is 1 second. |
27 | 30 | */ |
28 | | - private long retryInterval; |
| 31 | + private long retryInterval = 1000; |
29 | 32 | private final int DELAY_MILLISECONDS = 1000; |
30 | 33 | private final String RETRY_AFTER = "Retry-After"; |
31 | 34 | private final String TRANSFER_ENCODING = "Transfer-Encoding"; |
32 | 35 |
|
33 | 36 | private final int MSClientErrorCodeTooManyRequests = 429; |
34 | 37 | private final int MSClientErrorCodeServiceUnavailable = 503; |
35 | 38 | private final int MSClientErrorCodeGatewayTimeout = 504; |
| 39 | + private final RetryOptions mRetryOption; |
36 | 40 |
|
37 | | - public RetryHandler(final int maxRetries, final int retryInterval) { |
| 41 | + public RetryHandler(RetryOptions option) { |
38 | 42 | super(); |
39 | | - Args.positive(maxRetries, "Max retries"); |
40 | | - Args.positive(retryInterval, "Retry interval"); |
41 | | - this.maxRetries = maxRetries; |
42 | | - this.retryInterval = retryInterval; |
| 43 | + this.mRetryOption = option; |
43 | 44 | } |
44 | 45 |
|
45 | 46 | public RetryHandler() { |
46 | | - this(2, 1000); |
| 47 | + this(null); |
47 | 48 | } |
48 | 49 |
|
49 | 50 | @Override |
50 | 51 | public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) { |
| 52 | + |
| 53 | + RetryOptions retryOption = (RetryOptions)context.getAttribute(MiddlewareType.RETRY.toString()); |
| 54 | + if(retryOption != null) { |
| 55 | + return retryOption.shouldRetry().shouldRetry(response, executionCount, context); |
| 56 | + } |
| 57 | + if(mRetryOption != null) { |
| 58 | + return mRetryOption.shouldRetry().shouldRetry(response, executionCount, context); |
| 59 | + } |
| 60 | + |
51 | 61 | boolean shouldRetry = false; |
52 | 62 | int statusCode = response.getStatusLine().getStatusCode(); |
53 | 63 | shouldRetry = (executionCount < maxRetries) && checkStatus(statusCode) && isBuffered(response, context); |
|
0 commit comments