|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making polaris-java available. |
| 3 | + * |
| 4 | + * Copyright (C) 2021 Tencent. All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the BSD 3-Clause License (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://opensource.org/licenses/BSD-3-Clause |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software distributed |
| 13 | + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 14 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.ecwid.consul.transport; |
| 19 | + |
| 20 | +import org.apache.http.client.HttpClient; |
| 21 | +import org.apache.http.client.config.RequestConfig; |
| 22 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 23 | +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
| 24 | + |
| 25 | +/** |
| 26 | + * Transport for TTL. |
| 27 | + * |
| 28 | + * @author Haotian Zhang |
| 29 | + */ |
| 30 | +public class TtlHttpTransport extends AbstractHttpTransport { |
| 31 | + |
| 32 | + private final HttpClient httpClient; |
| 33 | + |
| 34 | + static final int DEFAULT_TTL_READ_TIMEOUT = 5 * 1000; // 5s |
| 35 | + static final int DEFAULT_CONNECTION_TIMEOUT = 5 * 1000; // 5s |
| 36 | + |
| 37 | + public TtlHttpTransport() { |
| 38 | + this(DEFAULT_TTL_READ_TIMEOUT); |
| 39 | + } |
| 40 | + |
| 41 | + public TtlHttpTransport(int ttlReadTimeout) { |
| 42 | + if (ttlReadTimeout == 0) { |
| 43 | + ttlReadTimeout = DEFAULT_TTL_READ_TIMEOUT; |
| 44 | + } |
| 45 | + PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); |
| 46 | + connectionManager.setMaxTotal(DEFAULT_MAX_CONNECTIONS); |
| 47 | + connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE_CONNECTIONS); |
| 48 | + |
| 49 | + RequestConfig requestConfig = RequestConfig.custom(). |
| 50 | + setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT). |
| 51 | + setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT). |
| 52 | + setSocketTimeout(ttlReadTimeout). |
| 53 | + build(); |
| 54 | + |
| 55 | + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(). |
| 56 | + setConnectionManager(connectionManager). |
| 57 | + setDefaultRequestConfig(requestConfig). |
| 58 | + useSystemProperties(); |
| 59 | + |
| 60 | + this.httpClient = httpClientBuilder.build(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public HttpClient getHttpClient() { |
| 65 | + return httpClient; |
| 66 | + } |
| 67 | +} |
0 commit comments