You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by va...@apache.org on 2024/04/16 19:56:21 UTC
(tinkerpop) 01/01: draft
This is an automated email from the ASF dual-hosted git repository.
valentyn pushed a commit to branch valentyn/http-java-pooling-draft
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
commit 702b633e0a7aac144e3d537d7994b9e79aec5ebc
Author: Valentyn Kahamlyk <Va...@improving.com>
AuthorDate: Tue Apr 16 12:56:11 2024 -0700
draft
---
.../tinkerpop/gremlin/driver/ConnectionPool.java | 51 +++++++++-------------
1 file changed, 20 insertions(+), 31 deletions(-)
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ConnectionPool.java b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ConnectionPool.java
index 37ad1ac936..4fdfd71f17 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ConnectionPool.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ConnectionPool.java
@@ -146,12 +146,7 @@ final class ConnectionPool {
" Successful connections=" + this.connections.size() +
". Closing the connection pool.";
- Throwable cause;
- Throwable result = ce;
-
- if (null != (cause = result.getCause())) {
- result = cause;
- }
+ final Throwable result = ce.getCause() == null ? ce : ce.getCause();
throw new CompletionException(errMsg, result);
}
@@ -183,9 +178,9 @@ final class ConnectionPool {
}
// Get the least used valid connection
- final Connection leastUsedConn = getLeastUsedValidConnection();
+ final Connection validConnection = getValidConnection();
- if (null == leastUsedConn) {
+ if (null == validConnection) {
if (isClosed())
throw new ConnectionException(host.getHostUri(), host.getAddress(), "Pool is shutdown");
logger.debug("Pool was initialized but a connection could not be selected earlier - waiting for connection on {}", host);
@@ -193,8 +188,8 @@ final class ConnectionPool {
}
if (logger.isDebugEnabled())
- logger.debug("Return least used {} on {}", leastUsedConn.getConnectionInfo(), host);
- return leastUsedConn;
+ logger.debug("Return least used {} on {}", validConnection.getConnectionInfo(), host);
+ return validConnection;
}
public void returnConnection(final Connection connection) throws ConnectionException {
@@ -215,9 +210,9 @@ final class ConnectionPool {
}
final int poolSize = connections.size();
- if (poolSize > minPoolSize ) {
+ if (poolSize > minPoolSize) {
if (logger.isDebugEnabled())
- logger.debug("destroy {}",connection.getConnectionInfo());
+ logger.debug("destroy {}", connection.getConnectionInfo());
destroyConnection(connection);
} else if (maxPoolSize > 1) {
if (logger.isDebugEnabled())
@@ -422,12 +417,12 @@ final class ConnectionPool {
if (isClosed())
throw new ConnectionException(host.getHostUri(), host.getAddress(), "Pool is shutdown");
- final Connection leastUsed = getLeastUsedValidConnection();
+ final Connection validConnection = getValidConnection();
- if (leastUsed != null) {
+ if (validConnection != null) {
if (logger.isDebugEnabled())
- logger.debug("Return least used {} on {} after waiting", leastUsed.getConnectionInfo(), host);
- return leastUsed;
+ logger.debug("Return least used {} on {} after waiting", validConnection.getConnectionInfo(), host);
+ return validConnection;
}
remaining = to - TimeUtil.timeSince(start, unit);
@@ -547,28 +542,22 @@ final class ConnectionPool {
* @return The least-used connection from the pool. Returns null if no valid connection could be retrieved from the
* pool.
*/
- private synchronized Connection getLeastUsedValidConnection() {
- // todo: just take first unused
- int minInFlight = Integer.MAX_VALUE;
+ private synchronized Connection getValidConnection() {
Connection leastBusy = null;
+ int availableConnection = 0;
for (Connection connection : connections) {
- final int inFlight = connection.borrowed.get();
- if (!connection.isDead() && inFlight < minInFlight && inFlight < 1) {
- minInFlight = inFlight;
+ if (!connection.isDead() && connection.borrowed.get() < 1) {
leastBusy = connection;
+ availableConnection++;
}
}
if (leastBusy != null) {
- // Increment borrow count and consider making a new connection if least used connection hits usage maximum
- if (leastBusy.borrowed.incrementAndGet() > 0
- && connections.size() < maxPoolSize) {
- if (logger.isDebugEnabled())
- logger.debug("Least used {} on {} reached maxSimultaneousUsagePerConnection but pool size {} < maxPoolSize - consider new connection",
- leastBusy.getConnectionInfo(), host, connections.size());
- considerNewConnection();
- }
- } else if (connections.size() < maxPoolSize) {
+ leastBusy.borrowed.incrementAndGet();
+ }
+
+ // todo: test
+ if (availableConnection < 2 && connections.size() < maxPoolSize) {
// A safeguard for scenarios where consideration of a new connection was somehow not triggered by an
// existing connection hitting the usage maximum
considerNewConnection();