View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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 }