|
11 | 11 | import me.tongfei.progressbar.DelegatingProgressBarConsumer; |
12 | 12 | import me.tongfei.progressbar.ProgressBar; |
13 | 13 | import me.tongfei.progressbar.ProgressBarBuilder; |
| 14 | +import org.apache.hc.client5.http.HttpRequestRetryStrategy; |
14 | 15 | import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; |
| 16 | +import org.apache.hc.client5.http.config.ConnectionConfig; |
15 | 17 | import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
16 | 18 | import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; |
17 | 19 | import org.apache.hc.client5.http.impl.classic.HttpClients; |
| 20 | +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; |
18 | 21 | import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; |
| 22 | +import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder; |
19 | 23 | import org.apache.hc.client5.http.ssl.NoopHostnameVerifier; |
20 | | -import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; |
| 24 | +import org.apache.hc.client5.http.ssl.TlsSocketStrategy; |
| 25 | +import org.apache.hc.core5.http.HttpRequest; |
| 26 | +import org.apache.hc.core5.http.HttpResponse; |
| 27 | +import org.apache.hc.core5.http.HttpStatus; |
21 | 28 | import org.apache.hc.core5.http.Method; |
22 | 29 | import org.apache.hc.core5.http.ParseException; |
| 30 | +import org.apache.hc.core5.http.io.SocketConfig; |
23 | 31 | import org.apache.hc.core5.http.io.entity.EntityUtils; |
| 32 | +import org.apache.hc.core5.http.protocol.HttpContext; |
| 33 | +import org.apache.hc.core5.http.ssl.TLS; |
24 | 34 | import org.apache.hc.core5.net.URIBuilder; |
| 35 | +import org.apache.hc.core5.pool.PoolConcurrencyPolicy; |
| 36 | +import org.apache.hc.core5.pool.PoolReusePolicy; |
| 37 | +import org.apache.hc.core5.util.TimeValue; |
| 38 | +import org.apache.hc.core5.util.Timeout; |
25 | 39 | import org.awaitility.core.ConditionTimeoutException; |
26 | 40 | import org.awaitility.pollinterval.FixedPollInterval; |
27 | 41 | import org.awaitility.pollinterval.PollInterval; |
@@ -59,9 +73,26 @@ protected static HttpClientBuilder defaultHttpClientBuilder() { |
59 | 73 | try { |
60 | 74 | SSLContext ctx = SSLContext.getInstance("TLS"); |
61 | 75 | ctx.init(new KeyManager[0], new TrustManager[]{new DefaultTrustManager()}, new SecureRandom()); |
| 76 | + PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create() |
| 77 | + .setTlsSocketStrategy((TlsSocketStrategy) ClientTlsStrategyBuilder.create() |
| 78 | + .setSslContext(ctx) |
| 79 | + .setHostnameVerifier(new NoopHostnameVerifier()) |
| 80 | + .setTlsVersions(TLS.V_1_3) |
| 81 | + .build()) |
| 82 | + .setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(Timeout.ofSeconds(601)).build()) |
| 83 | + .setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT) |
| 84 | + .setConnPoolPolicy(PoolReusePolicy.FIFO) |
| 85 | + .setDefaultConnectionConfig(ConnectionConfig.custom() |
| 86 | + .setSocketTimeout(Timeout.ofSeconds(601)) |
| 87 | + .setConnectTimeout(Timeout.ofSeconds(601)) |
| 88 | + .setTimeToLive(TimeValue.ofSeconds(601)) |
| 89 | + .build()) |
| 90 | + .setMaxConnPerRoute(10) |
| 91 | + .build(); |
| 92 | + |
62 | 93 | return HttpClients.custom() |
63 | | - .setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create() |
64 | | - .setSSLSocketFactory(new SSLConnectionSocketFactory(ctx, new NoopHostnameVerifier())).build()); |
| 94 | + .setConnectionManager(connectionManager) |
| 95 | + .setRetryStrategy(new CustomRequestRetryStrategy()); |
65 | 96 | } catch (NoSuchAlgorithmException | KeyManagementException e) { |
66 | 97 | throw new RuntimeException(e); |
67 | 98 | } |
@@ -232,6 +263,39 @@ private static String prettyPrint(String content) { |
232 | 263 | } |
233 | 264 | } |
234 | 265 |
|
| 266 | + protected static class CustomRequestRetryStrategy implements HttpRequestRetryStrategy { |
| 267 | + private static final int MAX_RETRIES = 5; |
| 268 | + |
| 269 | + @Override |
| 270 | + public boolean retryRequest(HttpRequest httpRequest, IOException e, int count, HttpContext httpContext) { |
| 271 | + if (count < MAX_RETRIES) { |
| 272 | + LOG.error("Got request error {}. Retry {}...", e.getMessage(), count); |
| 273 | + return true; |
| 274 | + } |
| 275 | + return false; |
| 276 | + } |
| 277 | + |
| 278 | + @Override |
| 279 | + public boolean retryRequest(HttpResponse response, int count, HttpContext httpContext) { |
| 280 | + if (count <= MAX_RETRIES && (response.getCode() == HttpStatus.SC_GATEWAY_TIMEOUT |
| 281 | + || response.getCode() == HttpStatus.SC_SERVICE_UNAVAILABLE)) { |
| 282 | + LOG.warn("Got {} {}. Retry {}...", response.getCode(), response.getReasonPhrase(), count); |
| 283 | + return true; |
| 284 | + } |
| 285 | + return false; |
| 286 | + } |
| 287 | + |
| 288 | + @Override |
| 289 | + public TimeValue getRetryInterval(HttpResponse response, int count, HttpContext context) { |
| 290 | + return TimeValue.ofSeconds(10); |
| 291 | + } |
| 292 | + |
| 293 | + @Override |
| 294 | + public TimeValue getRetryInterval(HttpRequest request, IOException e, int count, HttpContext context) { |
| 295 | + return TimeValue.ofSeconds(5); |
| 296 | + } |
| 297 | + } |
| 298 | + |
235 | 299 | private static class DefaultTrustManager implements X509TrustManager { |
236 | 300 |
|
237 | 301 | @Override |
|
0 commit comments