stoty commented on a change in pull request #149:
URL: https://github.com/apache/calcite-avatica/pull/149#discussion_r664705830



##########
File path: 
core/src/main/java/org/apache/calcite/avatica/remote/CommonsHttpClientPoolCache.java
##########
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.remote;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+import 
org.apache.calcite.avatica.remote.HostnameVerificationConfigurable.HostnameVerification;
+
+import org.apache.http.config.Registry;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.ssl.SSLContextBuilder;
+import org.apache.http.ssl.SSLContexts;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.concurrent.ConcurrentHashMap;
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.SSLContext;
+
+/**
+ * Creates and returns a PoolingHttpClientConnectionManager object.
+ * If a pool exists for a given set of keystore, trustore, and 
hostanmeVerification
+ * parameters, then the existing pool is returned.
+ *
+ */
+public class CommonsHttpClientPoolCache {
+
+  // Some basic exposed configurations
+  private static final String MAX_POOLED_CONNECTION_PER_ROUTE_KEY =
+      "avatica.pooled.connections.per.route";
+  private static final String MAX_POOLED_CONNECTION_PER_ROUTE_DEFAULT = "25";
+  private static final String MAX_POOLED_CONNECTIONS_KEY = 
"avatica.pooled.connections.max";
+  private static final String MAX_POOLED_CONNECTIONS_DEFAULT = "100";
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(CommonsHttpClientPoolCache.class);
+
+  private CommonsHttpClientPoolCache() {
+    //do not instantiate
+  }
+
+  private static final ConcurrentHashMap<String, 
PoolingHttpClientConnectionManager> CACHED_POOLS =
+      new ConcurrentHashMap<>();
+
+  public static PoolingHttpClientConnectionManager getPool(ConnectionConfig 
config) {
+    String sslDisc = extractSSLParameters(config);
+
+    PoolingHttpClientConnectionManager pool = CACHED_POOLS.get(sslDisc);
+    if (pool != null) {
+      //Debug
+      System.out.println("Reusing existing pool for sslDisc:" + sslDisc);
+      return pool;
+    }
+
+    synchronized (CommonsHttpClientPoolCache.class) {
+      pool = CACHED_POOLS.get(sslDisc);
+      if (pool != null) {
+        //Debug
+        System.out.println("Reusing2 existing pool for sslDisc:" + sslDisc);
+        return pool;
+      }
+
+      Registry<ConnectionSocketFactory> csfr = createCSFRegistry(config);
+      pool = new PoolingHttpClientConnectionManager(csfr);
+      final String maxCnxns =
+          System.getProperty(MAX_POOLED_CONNECTIONS_KEY, 
MAX_POOLED_CONNECTIONS_DEFAULT);
+      pool.setMaxTotal(Integer.parseInt(maxCnxns));
+      // Increase default max connection per route to 25
+      final String maxCnxnsPerRoute = 
System.getProperty(MAX_POOLED_CONNECTION_PER_ROUTE_KEY,
+          MAX_POOLED_CONNECTION_PER_ROUTE_DEFAULT);
+      pool.setDefaultMaxPerRoute(Integer.parseInt(maxCnxnsPerRoute));
+      //Debug
+      System.out.println("Created new pool for sslDisc:" + sslDisc);

Review comment:
       I meant to delete those, but forgot.
   But perhaps having some debug log statements won't hurt.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to