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 package org.apache.hc.client5.http.impl.classic;
28
29 import java.io.IOException;
30 import java.io.InterruptedIOException;
31
32 import org.apache.hc.client5.http.HttpRequestRetryStrategy;
33 import org.apache.hc.client5.http.HttpRoute;
34 import org.apache.hc.client5.http.classic.ExecChain;
35 import org.apache.hc.client5.http.classic.ExecChain.Scope;
36 import org.apache.hc.client5.http.classic.ExecChainHandler;
37 import org.apache.hc.client5.http.config.RequestConfig;
38 import org.apache.hc.client5.http.protocol.HttpClientContext;
39 import org.apache.hc.core5.annotation.Contract;
40 import org.apache.hc.core5.annotation.Internal;
41 import org.apache.hc.core5.annotation.ThreadingBehavior;
42 import org.apache.hc.core5.http.ClassicHttpRequest;
43 import org.apache.hc.core5.http.ClassicHttpResponse;
44 import org.apache.hc.core5.http.HttpEntity;
45 import org.apache.hc.core5.http.HttpException;
46 import org.apache.hc.core5.http.NoHttpResponseException;
47 import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
48 import org.apache.hc.core5.util.Args;
49 import org.apache.hc.core5.util.TimeValue;
50 import org.apache.hc.core5.util.Timeout;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 @Contract(threading = ThreadingBehavior.STATELESS)
68 @Internal
69 public class HttpRequestRetryExec implements ExecChainHandler {
70
71 private static final Logger LOG = LoggerFactory.getLogger(HttpRequestRetryExec.class);
72
73 private final HttpRequestRetryStrategy retryStrategy;
74
75 public HttpRequestRetryExec(
76 final HttpRequestRetryStrategy retryStrategy) {
77 Args.notNull(retryStrategy, "retryStrategy");
78 this.retryStrategy = retryStrategy;
79 }
80
81 @Override
82 public ClassicHttpResponse execute(
83 final ClassicHttpRequest request,
84 final Scope scope,
85 final ExecChain chain) throws IOException, HttpException {
86 Args.notNull(request, "request");
87 Args.notNull(scope, "scope");
88 final String exchangeId = scope.exchangeId;
89 final HttpRoute route = scope.route;
90 final HttpClientContext context = scope.clientContext;
91 ClassicHttpRequest currentRequest = request;
92
93 for (int execCount = 1;; execCount++) {
94 final ClassicHttpResponse response;
95 try {
96 response = chain.proceed(currentRequest, scope);
97 } catch (final IOException ex) {
98 if (scope.execRuntime.isExecutionAborted()) {
99 throw new RequestFailedException("Request aborted");
100 }
101 final HttpEntity requestEntity = request.getEntity();
102 if (requestEntity != null && !requestEntity.isRepeatable()) {
103 if (LOG.isDebugEnabled()) {
104 LOG.debug("{} cannot retry non-repeatable request", exchangeId);
105 }
106 throw ex;
107 }
108 if (retryStrategy.retryRequest(request, ex, execCount, context)) {
109 if (LOG.isDebugEnabled()) {
110 LOG.debug("{} {}", exchangeId, ex.getMessage(), ex);
111 }
112 if (LOG.isInfoEnabled()) {
113 LOG.info("Recoverable I/O exception ({}) caught when processing request to {}",
114 ex.getClass().getName(), route);
115 }
116 currentRequest = ClassicRequestBuilder.copy(scope.originalRequest).build();
117 continue;
118 } else {
119 if (ex instanceof NoHttpResponseException) {
120 final NoHttpResponseException updatedex = new NoHttpResponseException(
121 route.getTargetHost().toHostString() + " failed to respond");
122 updatedex.setStackTrace(ex.getStackTrace());
123 throw updatedex;
124 }
125 throw ex;
126 }
127 }
128
129 try {
130 final HttpEntity entity = request.getEntity();
131 if (entity != null && !entity.isRepeatable()) {
132 if (LOG.isDebugEnabled()) {
133 LOG.debug("{} cannot retry non-repeatable request", exchangeId);
134 }
135 return response;
136 }
137 if (retryStrategy.retryRequest(response, execCount, context)) {
138 final TimeValue nextInterval = retryStrategy.getRetryInterval(response, execCount, context);
139
140 if (TimeValue.isPositive(nextInterval)) {
141 final RequestConfig requestConfig = context.getRequestConfig();
142 final Timeout responseTimeout = requestConfig.getResponseTimeout();
143 if (responseTimeout != null && nextInterval.compareTo(responseTimeout) > 0) {
144 return response;
145 }
146 }
147 response.close();
148 if (TimeValue.isPositive(nextInterval)) {
149 try {
150 if (LOG.isDebugEnabled()) {
151 LOG.debug("{} wait for {}", exchangeId, nextInterval);
152 }
153 nextInterval.sleep();
154 } catch (final InterruptedException e) {
155 Thread.currentThread().interrupt();
156 throw new InterruptedIOException();
157 }
158 }
159 currentRequest = ClassicRequestBuilder.copy(scope.originalRequest).build();
160 } else {
161 return response;
162 }
163 } catch (final RuntimeException ex) {
164 response.close();
165 throw ex;
166 }
167 }
168 }
169
170 }