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.async;
28
29 import java.net.InetSocketAddress;
30 import java.util.concurrent.Future;
31
32 import org.apache.hc.client5.http.config.ConnectionConfig;
33 import org.apache.hc.core5.concurrent.CallbackContribution;
34 import org.apache.hc.core5.concurrent.FutureCallback;
35 import org.apache.hc.core5.function.Resolver;
36 import org.apache.hc.core5.http.HttpHost;
37 import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
38 import org.apache.hc.core5.http2.nio.pool.H2ConnPool;
39 import org.apache.hc.core5.io.CloseMode;
40 import org.apache.hc.core5.io.ModalCloseable;
41 import org.apache.hc.core5.reactor.ConnectionInitiator;
42 import org.apache.hc.core5.reactor.IOSession;
43 import org.apache.hc.core5.util.TimeValue;
44 import org.apache.hc.core5.util.Timeout;
45
46 class InternalH2ConnPool implements ModalCloseable {
47
48 private final H2ConnPool connPool;
49
50 private volatile Resolver<HttpHost, ConnectionConfig> connectionConfigResolver;
51
52 InternalH2ConnPool(final ConnectionInitiator connectionInitiator,
53 final Resolver<HttpHost, InetSocketAddress> addressResolver,
54 final TlsStrategy tlsStrategy) {
55 this.connPool = new H2ConnPool(connectionInitiator, addressResolver, tlsStrategy);
56 }
57
58 @Override
59 public void close(final CloseMode closeMode) {
60 connPool.close(closeMode);
61 }
62
63 @Override
64 public void close() {
65 connPool.close();
66 }
67
68 private ConnectionConfig resolveConnectionConfig(final HttpHost httpHost) {
69 final Resolver<HttpHost, ConnectionConfig> resolver = this.connectionConfigResolver;
70 final ConnectionConfig connectionConfig = resolver != null ? resolver.resolve(httpHost) : null;
71 return connectionConfig != null ? connectionConfig : ConnectionConfig.DEFAULT;
72 }
73
74 public Future<IOSession> getSession(
75 final HttpHost endpoint,
76 final Timeout connectTimeout,
77 final FutureCallback<IOSession> callback) {
78 final ConnectionConfig connectionConfig = resolveConnectionConfig(endpoint);
79 return connPool.getSession(
80 endpoint,
81 connectTimeout != null ? connectTimeout : connectionConfig.getConnectTimeout(),
82 new CallbackContribution<IOSession>(callback) {
83
84 @Override
85 public void completed(final IOSession ioSession) {
86 final Timeout socketTimeout = connectionConfig.getSocketTimeout();
87 if (socketTimeout != null) {
88 ioSession.setSocketTimeout(socketTimeout);
89 }
90 callback.completed(ioSession);
91 }
92
93 });
94 }
95
96 public void closeIdle(final TimeValue idleTime) {
97 connPool.closeIdle(idleTime);
98 }
99
100 public void setConnectionConfigResolver(final Resolver<HttpHost, ConnectionConfig> connectionConfigResolver) {
101 this.connectionConfigResolver = connectionConfigResolver;
102 }
103
104 }