Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pool implementation for one-shot queries does not use all available resources #743

Merged
merged 1 commit into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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