Browse Source

Lazy connections.

Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
Santiago Pericas-Geertsen 10 months ago
parent
commit
cc9ef0e197

+ 23 - 12
frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/models/PgClientConnectionPool.java

@@ -1,6 +1,8 @@
 
 
 package io.helidon.benchmark.nima.models;
 package io.helidon.benchmark.nima.models;
 
 
+import java.util.concurrent.locks.ReentrantLock;
+
 import io.vertx.core.Vertx;
 import io.vertx.core.Vertx;
 import io.vertx.pgclient.PgConnectOptions;
 import io.vertx.pgclient.PgConnectOptions;
 import io.vertx.pgclient.PgConnection;
 import io.vertx.pgclient.PgConnection;
@@ -14,6 +16,7 @@ class PgClientConnectionPool implements AutoCloseable {
     private final Vertx vertx;
     private final Vertx vertx;
     private final PgConnectOptions options;
     private final PgConnectOptions options;
     private final PgClientConnection[] connections;
     private final PgClientConnection[] connections;
+    private final ReentrantLock lock = new ReentrantLock();
 
 
     public PgClientConnectionPool(Vertx vertx, int size, PgConnectOptions options) {
     public PgClientConnectionPool(Vertx vertx, int size, PgConnectOptions options) {
         this.size = size;
         this.size = size;
@@ -24,29 +27,37 @@ class PgClientConnectionPool implements AutoCloseable {
 
 
     public PgClientConnection clientConnection() {
     public PgClientConnection clientConnection() {
         int bucket = Thread.currentThread().hashCode() % size;
         int bucket = Thread.currentThread().hashCode() % size;
+        if (connections[bucket] == null) {
+            try {
+                lock.lock();
+                if (connections[bucket] == null) {
+                    connect(bucket);
+                }
+            } finally {
+                lock.unlock();
+            }
+        }
         return connections[bucket];
         return connections[bucket];
     }
     }
 
 
-    public void connect() {
+    @Override
+    public void close() {
         try {
         try {
-            for (int i = 0; i < size; i++) {
-                PgConnection conn = PgConnection.connect(vertx, options)
-                        .toCompletionStage().toCompletableFuture().get();
-                PgClientConnection clientConn = new PgClientConnection(conn);
-                clientConn.prepare();
-                connections[i] = clientConn;
+            for (PgClientConnection connection : connections) {
+                connection.close();
             }
             }
         } catch (Exception e) {
         } catch (Exception e) {
             throw new RuntimeException(e);
             throw new RuntimeException(e);
         }
         }
     }
     }
 
 
-    @Override
-    public void close() {
+    private void connect(int bucket) {
         try {
         try {
-            for (PgClientConnection connection : connections) {
-                connection.close();
-            }
+            PgConnection conn = PgConnection.connect(vertx, options)
+                    .toCompletionStage().toCompletableFuture().get();
+            PgClientConnection clientConn = new PgClientConnection(conn);
+            clientConn.prepare();
+            connections[bucket] = clientConn;
         } catch (Exception e) {
         } catch (Exception e) {
             throw new RuntimeException(e);
             throw new RuntimeException(e);
         }
         }

+ 0 - 1
frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/models/PgClientRepository.java

@@ -34,7 +34,6 @@ public class PgClientRepository implements DbRepository {
                 .setPipeliningLimit(100000);
                 .setPipeliningLimit(100000);
         int sqlPoolSize = config.get("sql-pool-size").asInt().orElse(Runtime.getRuntime().availableProcessors());
         int sqlPoolSize = config.get("sql-pool-size").asInt().orElse(Runtime.getRuntime().availableProcessors());
         connectionPool = new PgClientConnectionPool(vertx, sqlPoolSize, connectOptions);
         connectionPool = new PgClientConnectionPool(vertx, sqlPoolSize, connectOptions);
-        connectionPool.connect();
     }
     }
 
 
     @Override
     @Override