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 package org.apache.hc.client5.http.impl;
29
30 import java.io.IOException;
31 import java.io.InterruptedIOException;
32 import java.net.ConnectException;
33 import java.net.NoRouteToHostException;
34 import java.net.UnknownHostException;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.Date;
38 import java.util.HashSet;
39 import java.util.Set;
40
41 import javax.net.ssl.SSLException;
42
43 import org.apache.hc.client5.http.HttpRequestRetryStrategy;
44 import org.apache.hc.client5.http.utils.DateUtils;
45 import org.apache.hc.core5.annotation.Contract;
46 import org.apache.hc.core5.annotation.ThreadingBehavior;
47 import org.apache.hc.core5.concurrent.CancellableDependency;
48 import org.apache.hc.core5.http.ConnectionClosedException;
49 import org.apache.hc.core5.http.Header;
50 import org.apache.hc.core5.http.HttpHeaders;
51 import org.apache.hc.core5.http.HttpRequest;
52 import org.apache.hc.core5.http.HttpResponse;
53 import org.apache.hc.core5.http.HttpStatus;
54 import org.apache.hc.core5.http.Method;
55 import org.apache.hc.core5.http.RequestNotExecutedException;
56 import org.apache.hc.core5.http.protocol.HttpContext;
57 import org.apache.hc.core5.util.Args;
58 import org.apache.hc.core5.util.TimeValue;
59
60
61
62
63
64
65 @Contract(threading = ThreadingBehavior.STATELESS)
66 public class DefaultHttpRequestRetryStrategy implements HttpRequestRetryStrategy {
67
68 public static final DefaultHttpRequestRetryStrategytRetryStrategy.html#DefaultHttpRequestRetryStrategy">DefaultHttpRequestRetryStrategy INSTANCE = new DefaultHttpRequestRetryStrategy();
69
70
71
72
73 private final int maxRetries;
74
75
76
77
78 private final TimeValue defaultRetryInterval;
79
80
81
82
83 private final Set<Class<? extends IOException>> nonRetriableIOExceptionClasses;
84
85
86
87
88 private final Set<Integer> retriableCodes;
89
90 protected DefaultHttpRequestRetryStrategy(
91 final int maxRetries,
92 final TimeValue defaultRetryInterval,
93 final Collection<Class<? extends IOException>> clazzes,
94 final Collection<Integer> codes) {
95 Args.notNegative(maxRetries, "maxRetries");
96 Args.notNegative(defaultRetryInterval.getDuration(), "defaultRetryInterval");
97 this.maxRetries = maxRetries;
98 this.defaultRetryInterval = defaultRetryInterval;
99 this.nonRetriableIOExceptionClasses = new HashSet<>(clazzes);
100 this.retriableCodes = new HashSet<>(codes);
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public DefaultHttpRequestRetryStrategy(
127 final int maxRetries,
128 final TimeValue defaultRetryInterval) {
129 this(maxRetries, defaultRetryInterval,
130 Arrays.asList(
131 InterruptedIOException.class,
132 UnknownHostException.class,
133 ConnectException.class,
134 ConnectionClosedException.class,
135 NoRouteToHostException.class,
136 SSLException.class),
137 Arrays.asList(
138 HttpStatus.SC_TOO_MANY_REQUESTS,
139 HttpStatus.SC_SERVICE_UNAVAILABLE));
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 public DefaultHttpRequestRetryStrategy() {
161 this(1, TimeValue.ofSeconds(1L));
162 }
163
164 @Override
165 public boolean retryRequest(
166 final HttpRequest request,
167 final IOException exception,
168 final int execCount,
169 final HttpContext context) {
170 Args.notNull(request, "request");
171 Args.notNull(exception, "exception");
172
173 if (execCount > this.maxRetries) {
174
175 return false;
176 }
177 if (this.nonRetriableIOExceptionClasses.contains(exception.getClass())) {
178 return false;
179 } else {
180 if (exception instanceof RequestNotExecutedException) {
181 return true;
182 }
183 for (final Class<? extends IOException> rejectException : this.nonRetriableIOExceptionClasses) {
184 if (rejectException.isInstance(exception)) {
185 return false;
186 }
187 }
188 }
189 if (request instanceof CancellableDependency && ((CancellableDependency) request).isCancelled()) {
190 return false;
191 }
192
193
194 return handleAsIdempotent(request);
195 }
196
197 @Override
198 public boolean retryRequest(
199 final HttpResponse response,
200 final int execCount,
201 final HttpContext context) {
202 Args.notNull(response, "response");
203
204 return execCount <= this.maxRetries && retriableCodes.contains(response.getCode());
205 }
206
207 @Override
208 public TimeValue getRetryInterval(
209 final HttpResponse response,
210 final int execCount,
211 final HttpContext context) {
212 Args.notNull(response, "response");
213
214 final Header header = response.getFirstHeader(HttpHeaders.RETRY_AFTER);
215 TimeValue retryAfter = null;
216 if (header != null) {
217 final String value = header.getValue();
218 try {
219 retryAfter = TimeValue.ofSeconds(Long.parseLong(value));
220 } catch (final NumberFormatException ignore) {
221 final Date retryAfterDate = DateUtils.parseDate(value);
222 if (retryAfterDate != null) {
223 retryAfter =
224 TimeValue.ofMilliseconds(retryAfterDate.getTime() - System.currentTimeMillis());
225 }
226 }
227
228 if (TimeValue.isPositive(retryAfter)) {
229 return retryAfter;
230 }
231 }
232 return this.defaultRetryInterval;
233 }
234
235 protected boolean handleAsIdempotent(final HttpRequest request) {
236 return Method.isIdempotent(request.getMethod());
237 }
238
239 }