|
22 | 22 | import io.split.integrations.IntegrationsConfig; |
23 | 23 | import org.apache.hc.client5.http.auth.AuthScope; |
24 | 24 | import org.apache.hc.client5.http.auth.Credentials; |
25 | | -import org.apache.hc.client5.http.auth.CredentialsProvider; |
26 | 25 | import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; |
27 | 26 | import org.apache.hc.client5.http.config.RequestConfig; |
28 | 27 | import org.apache.hc.client5.http.cookie.StandardCookieSpec; |
|
33 | 32 | import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; |
34 | 33 | import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; |
35 | 34 | import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner; |
36 | | -import org.apache.hc.client5.http.socket.ConnectionSocketFactory; |
37 | | -import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory; |
38 | 35 | import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; |
39 | 36 | import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder; |
40 | | -import org.apache.hc.core5.http.config.Registry; |
41 | | -import org.apache.hc.core5.http.config.RegistryBuilder; |
42 | 37 | import org.apache.hc.core5.http.io.SocketConfig; |
43 | 38 | import org.apache.hc.core5.http.ssl.TLS; |
44 | 39 | import org.apache.hc.core5.ssl.SSLContexts; |
45 | 40 | import org.apache.hc.core5.util.Timeout; |
46 | 41 | import org.slf4j.Logger; |
47 | 42 | import org.slf4j.LoggerFactory; |
48 | 43 |
|
49 | | -import javax.net.ssl.SSLContext; |
50 | 44 | import java.io.IOException; |
51 | 45 | import java.net.URI; |
52 | 46 | import java.net.URISyntaxException; |
53 | | -import java.security.KeyManagementException; |
54 | | -import java.security.NoSuchAlgorithmException; |
55 | 47 | import java.util.ArrayList; |
56 | 48 | import java.util.List; |
57 | 49 | import java.util.Random; |
|
60 | 52 |
|
61 | 53 | public class SplitFactoryImpl implements SplitFactory { |
62 | 54 | private static final Logger _log = LoggerFactory.getLogger(SplitFactory.class); |
| 55 | + private final static long SSE_CONNECT_TIMEOUT = 30000; |
| 56 | + private final static long SSE_SOCKET_TIMEOUT = 70000; |
63 | 57 |
|
64 | 58 | private static final Multiset<String> USED_API_TOKENS = ConcurrentHashMultiset.create(); |
65 | 59 | private static Random RANDOM = new Random(); |
@@ -100,23 +94,60 @@ private static CloseableHttpClient buildHttpClient(String apiToken, SplitClientC |
100 | 94 |
|
101 | 95 | // Set up proxy is it exists |
102 | 96 | if (config.proxy() != null) { |
103 | | - _log.info("Initializing Split SDK with proxy settings"); |
104 | | - DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(config.proxy()); |
105 | | - httpClientbuilder.setRoutePlanner(routePlanner); |
106 | | - |
107 | | - if (config.proxyUsername() != null && config.proxyPassword() != null) { |
108 | | - _log.debug("Proxy setup using credentials"); |
109 | | - BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); |
110 | | - AuthScope siteScope = new AuthScope(config.proxy().getHostName(), config.proxy().getPort()); |
111 | | - Credentials siteCreds = new UsernamePasswordCredentials(config.proxyUsername(), config.proxyPassword().toCharArray()); |
112 | | - credsProvider.setCredentials(siteScope, siteCreds); |
113 | | - httpClientbuilder.setDefaultCredentialsProvider(credsProvider); |
114 | | - } |
| 97 | + httpClientbuilder = setupProxy(httpClientbuilder, config); |
115 | 98 | } |
116 | 99 |
|
117 | 100 | return httpClientbuilder.build(); |
118 | 101 | } |
119 | 102 |
|
| 103 | + private static CloseableHttpClient buildSSEdHttpClient(SplitClientConfig config) { |
| 104 | + RequestConfig requestConfig = RequestConfig.custom() |
| 105 | + .setConnectTimeout(Timeout.of(SSE_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)) |
| 106 | + .build(); |
| 107 | + |
| 108 | + SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create() |
| 109 | + .setSslContext(SSLContexts.createSystemDefault()) |
| 110 | + .setTlsVersions(TLS.V_1_1, TLS.V_1_2) |
| 111 | + .build(); |
| 112 | + |
| 113 | + PoolingHttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create() |
| 114 | + .setSSLSocketFactory(sslSocketFactory) |
| 115 | + .setDefaultSocketConfig(SocketConfig.custom() |
| 116 | + .setSoTimeout(Timeout.ofMilliseconds(SSE_SOCKET_TIMEOUT)) |
| 117 | + .build()) |
| 118 | + .build(); |
| 119 | + cm.setMaxTotal(1); |
| 120 | + cm.setDefaultMaxPerRoute(1); |
| 121 | + |
| 122 | + HttpClientBuilder httpClientbuilder = HttpClients.custom() |
| 123 | + .setConnectionManager(cm) |
| 124 | + .setDefaultRequestConfig(requestConfig); |
| 125 | + |
| 126 | + // Set up proxy is it exists |
| 127 | + if (config.proxy() != null) { |
| 128 | + httpClientbuilder = setupProxy(httpClientbuilder, config); |
| 129 | + } |
| 130 | + |
| 131 | + return httpClientbuilder.build(); |
| 132 | + } |
| 133 | + |
| 134 | + private static HttpClientBuilder setupProxy(HttpClientBuilder httpClientbuilder, SplitClientConfig config) { |
| 135 | + _log.info("Initializing Split SDK with proxy settings"); |
| 136 | + DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(config.proxy()); |
| 137 | + httpClientbuilder.setRoutePlanner(routePlanner); |
| 138 | + |
| 139 | + if (config.proxyUsername() != null && config.proxyPassword() != null) { |
| 140 | + _log.debug("Proxy setup using credentials"); |
| 141 | + BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); |
| 142 | + AuthScope siteScope = new AuthScope(config.proxy().getHostName(), config.proxy().getPort()); |
| 143 | + Credentials siteCreds = new UsernamePasswordCredentials(config.proxyUsername(), config.proxyPassword().toCharArray()); |
| 144 | + credsProvider.setCredentials(siteScope, siteCreds); |
| 145 | + httpClientbuilder.setDefaultCredentialsProvider(credsProvider); |
| 146 | + } |
| 147 | + |
| 148 | + return httpClientbuilder; |
| 149 | + } |
| 150 | + |
120 | 151 | public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyntaxException { |
121 | 152 | _apiToken = apiToken; |
122 | 153 |
|
@@ -190,7 +221,7 @@ public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyn |
190 | 221 | final EventClient eventClient = EventClientImpl.create(httpclient, eventsRootTarget, config.eventsQueueSize(), config.eventFlushIntervalInMillis(), config.waitBeforeShutdown()); |
191 | 222 |
|
192 | 223 | // SyncManager |
193 | | - final SyncManager syncManager = SyncManagerImp.build(config.streamingEnabled(), splitFetcherProvider, segmentFetcher, config.authServiceURL(), httpclient, config.streamingServiceURL(), config.authRetryBackoffBase()); |
| 224 | + final SyncManager syncManager = SyncManagerImp.build(config.streamingEnabled(), splitFetcherProvider, segmentFetcher, config.authServiceURL(), httpclient, config.streamingServiceURL(), config.authRetryBackoffBase(), buildSSEdHttpClient(config)); |
194 | 225 | syncManager.start(); |
195 | 226 |
|
196 | 227 | destroyer = new Runnable() { |
|
0 commit comments