Skip to content

Commit

Permalink
Pool implementation for one-shot queries does not use all available r…
Browse files Browse the repository at this point in the history
…esources

Fixes #742

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont committed Aug 20, 2020
1 parent 6bbdcf5 commit 1bfb2ae
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
34 changes: 33 additions & 1 deletion vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package io.vertx.pgclient;

import io.vertx.sqlclient.PoolOptions;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.sqlclient.PoolOptions;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -130,4 +130,36 @@ public void testMaxWaitQueueSize(TestContext ctx) {
pool.close();
}
}

@Test
public void testUseAvailableResources(TestContext ctx) {
int poolSize = 10;
Async async = ctx.async(poolSize + 1);
PgPool pool = PgPool.pool(options, new PoolOptions().setMaxSize(poolSize));
AtomicReference<PgConnection> ctrlConnRef = new AtomicReference<>();
PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(ctrlConn -> {
ctrlConnRef.set(ctrlConn);
for (int i = 0; i < poolSize; i++) {
vertx.setTimer(10 * (i + 1), l -> {
pool.query("select pg_sleep(5)").execute(ctx.asyncAssertSuccess(res -> async.countDown()));
});
}
vertx.setTimer(10 * (poolSize + 1), event -> {
ctrlConn.query("select count(*) as cnt from pg_stat_activity where application_name like '%vertx%'").execute(ctx.asyncAssertSuccess(rows -> {
Integer count = rows.iterator().next().getInteger("cnt");
ctx.assertEquals(poolSize + 1, count);
async.countDown();
}));
});
}));
try {
async.await();
} finally {
PgConnection ctrlConn = ctrlConnRef.get();
if (ctrlConn != null) {
ctrlConn.close();
}
pool.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@

package io.vertx.sqlclient.impl;

import io.vertx.sqlclient.PoolOptions;
import io.vertx.core.*;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.Transaction;
import io.vertx.sqlclient.impl.command.CommandBase;
import io.vertx.sqlclient.impl.command.CommandResponse;
import io.vertx.sqlclient.impl.command.CommandScheduler;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxException;

/**
* Todo :
Expand Down Expand Up @@ -88,9 +82,11 @@ public <R> void schedule(CommandBase<R> cmd, Handler<? super CommandResponse<R>>
pool.acquire(new CommandWaiter() { // SHOULD BE IT !!!!!
@Override
protected void onSuccess(Connection conn) {
cmd.handler = handler;
cmd.handler = commandResponse -> {
conn.close(this);
handler.handle(commandResponse);
};
conn.schedule(cmd);
conn.close(this);
}
@Override
protected void onFailure(Throwable cause) {
Expand Down

0 comments on commit 1bfb2ae

Please sign in to comment.