Skip to content

Commit

Permalink
Enhanced fix for #5855
Browse files Browse the repository at this point in the history
removed duplicate code to FutureConnection.
  • Loading branch information
gregw committed Jan 6, 2021
1 parent b854b34 commit d226896
Showing 1 changed file with 42 additions and 64 deletions.
Expand Up @@ -102,39 +102,7 @@ public CompletableFuture<Void> preCreateConnections(int connectionCount)

final Pool<Connection>.Entry reserved = entry;

Promise.Completable<Connection> future = new Promise.Completable<Connection>()
{
@Override
public void succeeded(Connection connection)
{
if (LOG.isDebugEnabled())
LOG.debug("Connection creation succeeded {}: {}", reserved, connection);
if (connection instanceof Attachable)
{
((Attachable)connection).setAttachment(reserved);
onCreated(connection);
reserved.enable(connection, false);
idle(connection, false);
complete(null);
proceed();
}
else
{
failed(new IllegalArgumentException("Invalid connection object: " + connection));
}
}

@Override
public void failed(Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug("Connection creation failed {}", reserved, x);
reserved.remove();
completeExceptionally(x);
requester.failed(x);
}
};

Promise.Completable<Connection> future = new FutureConnection(reserved);
futures.add(future);
if (LOG.isDebugEnabled())
LOG.debug("Creating connection {}/{} at {}", futures.size() + 1, getMaxConnectionCount(), reserved);
Expand Down Expand Up @@ -291,37 +259,7 @@ protected void tryCreate()
if (LOG.isDebugEnabled())
LOG.debug("Creating connection {}/{} at {}", connectionCount, getMaxConnectionCount(), entry);

Promise<Connection> future = new Promise<Connection>()
{
@Override
public void succeeded(Connection connection)
{
if (LOG.isDebugEnabled())
LOG.debug("Connection {}/{} creation succeeded at {}: {}", connectionCount, getMaxConnectionCount(), entry, connection);
if (connection instanceof Attachable)
{
((Attachable)connection).setAttachment(entry);
onCreated(connection);
entry.enable(connection, false);
idle(connection, false);
proceed();
}
else
{
failed(new IllegalArgumentException("Invalid connection object: " + connection));
}
}

@Override
public void failed(Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug("Connection {}/{} creation failed at {}", connectionCount, getMaxConnectionCount(), entry, x);
entry.remove();
requester.failed(x);
}
};

Promise<Connection> future = new FutureConnection(entry);
destination.newConnection(future);
}

Expand Down Expand Up @@ -504,4 +442,44 @@ public String toString()
getActiveConnectionCount(),
getIdleConnectionCount());
}

private class FutureConnection extends Promise.Completable<Connection>
{
private final Pool<Connection>.Entry reserved;

public FutureConnection(Pool<Connection>.Entry reserved)
{
this.reserved = reserved;
}

@Override
public void succeeded(Connection connection)
{
if (LOG.isDebugEnabled())
LOG.debug("Connection creation succeeded {}: {}", reserved, connection);
if (connection instanceof Attachable)
{
((Attachable)connection).setAttachment(reserved);
onCreated(connection);
reserved.enable(connection, false);
idle(connection, false);
complete(null);
proceed();
}
else
{
failed(new IllegalArgumentException("Invalid connection object: " + connection));
}
}

@Override
public void failed(Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug("Connection creation failed {}", reserved, x);
reserved.remove();
completeExceptionally(x);
requester.failed(x);
}
}
}

0 comments on commit d226896

Please sign in to comment.