Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -238,7 +239,7 @@ public void testNoConnectionLeaks(TestContext ctx) {
.setMaxSize(1)
.setIdleTimeout(idleTimeout)
.setIdleTimeoutUnit(TimeUnit.MILLISECONDS)
.setPoolCleanerPeriod(5);
.setRefreshPeriod(Duration.ofMillis(5));
pool = MySQLBuilder.pool(builder -> builder.with(poolOptions).connectingTo(options).using(vertx));

Async async = ctx.async();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.junit.Rule;
import org.junit.Test;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -387,7 +388,7 @@ public void testPoolIdleTimeout(TestContext ctx) {
listenLatch.awaitSuccess(20_000);

poolOptions
.setPoolCleanerPeriod(poolCleanerPeriod)
.setRefreshPeriod(Duration.ofMillis(poolCleanerPeriod))
.setMaxLifetime(0)
.setIdleTimeout(idleTimeout)
.setIdleTimeoutUnit(TimeUnit.MILLISECONDS);
Expand Down Expand Up @@ -430,7 +431,7 @@ public void testPoolMaxLifetime(TestContext ctx) {
listenLatch.awaitSuccess(20_000);

poolOptions
.setPoolCleanerPeriod(poolCleanerPeriod)
.setRefreshPeriod(Duration.ofMillis(poolCleanerPeriod))
.setIdleTimeout(0)
.setMaxLifetime(maxLifetime)
.setMaxLifetimeUnit(TimeUnit.MILLISECONDS);
Expand Down Expand Up @@ -503,7 +504,7 @@ public void testNoConnectionLeaks(TestContext ctx) {
.setMaxSize(1)
.setIdleTimeout(idleTimeout)
.setIdleTimeoutUnit(TimeUnit.MILLISECONDS)
.setPoolCleanerPeriod(5);
.setRefreshPeriod(Duration.ofMillis(5));
Pool pool = createPool(options, poolOptions);

Async async = ctx.async();
Expand Down
11 changes: 11 additions & 0 deletions vertx-sql-client/src/main/asciidoc/pool_config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ has been created and before it is inserted in a pool.

Once you are done with the connection, you should simply close it to signal the pool to use it.

=== Pool maintenance

The pool periodically refreshes its state ({@link io.vertx.sqlclient.PoolOptions#setRefreshPeriod}): a
refresh reaps the connections that exceeded their idle timeout or maximum lifetime and times out the expired
connection acquisitions.

NOTE: the refresh period bounds the resolution of the connection timeout
({@link io.vertx.sqlclient.PoolOptions#setConnectionTimeout}): an acquisition times out on the first refresh
following its deadline. The default refresh period (100 ms) is small enough for any meaningful connection
timeout.

=== Dynamic connection configuration

You can configure the pool connection details using a Java supplier instead of an instance of `SqlConnectOptions`.
Expand Down
40 changes: 40 additions & 0 deletions vertx-sql-client/src/main/java/io/vertx/sqlclient/PoolOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.vertx.codegen.json.annotations.JsonGen;
import io.vertx.core.json.JsonObject;

import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -65,9 +66,17 @@ public class PoolOptions {

/**
* Default pool cleaner period = 1000 ms (1 second)
*
* @deprecated instead use {@link #DEFAULT_REFRESH_PERIOD}
*/
@Deprecated
Comment thread
tsegismont marked this conversation as resolved.
Resolved
public static final int DEFAULT_POOL_CLEANER_PERIOD = 1000;

/**
* Default pool refresh period = 100 ms
*/
public static final Duration DEFAULT_REFRESH_PERIOD = Duration.ofMillis(100);

/**
* Default connection timeout in the pool = 30 seconds
*/
Expand Down Expand Up @@ -100,6 +109,7 @@ public class PoolOptions {
private int maxLifetime = DEFAULT_MAXIMUM_LIFETIME;
private TimeUnit maxLifetimeUnit = DEFAULT_MAXIMUM_LIFETIME_TIME_UNIT;
private int poolCleanerPeriod = DEFAULT_POOL_CLEANER_PERIOD;
private Duration refreshPeriod = DEFAULT_REFRESH_PERIOD;
private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
private TimeUnit connectionTimeoutUnit = DEFAULT_CONNECTION_TIMEOUT_TIME_UNIT;
private boolean shared = DEFAULT_SHARED_POOL;
Expand All @@ -121,6 +131,7 @@ public PoolOptions(PoolOptions other) {
maxLifetime = other.maxLifetime;
maxLifetimeUnit = other.maxLifetimeUnit;
poolCleanerPeriod = other.poolCleanerPeriod;
refreshPeriod = other.refreshPeriod;
connectionTimeout = other.connectionTimeout;
connectionTimeoutUnit = other.connectionTimeoutUnit;
shared= other.shared;
Expand Down Expand Up @@ -248,7 +259,10 @@ public PoolOptions setMaxLifetime(int maxLifetime) {

/**
* @return the connection pool cleaner period in ms.
*
* @deprecated the pool cleaner period is unused, instead use {@link #getRefreshPeriod()}
*/
@Deprecated
public int getPoolCleanerPeriod() {
return poolCleanerPeriod;
}
Expand All @@ -259,12 +273,38 @@ public int getPoolCleanerPeriod() {
*
* @param poolCleanerPeriod the pool cleaner period
* @return a reference to this, so the API can be used fluently
*
* @deprecated the pool cleaner period is unused, instead use {@link #setRefreshPeriod(Duration)}
*/
@Deprecated
public PoolOptions setPoolCleanerPeriod(int poolCleanerPeriod) {
this.poolCleanerPeriod = poolCleanerPeriod;
return this;
}

/**
* @return the pool refresh period
*/
public Duration getRefreshPeriod() {
return refreshPeriod;
}

/**
* Set the period of the pool refresh timer: the refresh performs the pool maintenance tasks, it reaps the
* stale connections and times out the expired connection acquisitions. The period bounds the resolution of
* {@link #setConnectionTimeout}.
*
* @param refreshPeriod the pool refresh period, at least one millisecond
* @return a reference to this, so the API can be used fluently
*/
public PoolOptions setRefreshPeriod(Duration refreshPeriod) {
if (refreshPeriod == null || refreshPeriod.toMillis() < 1) {
throw new IllegalArgumentException("refreshPeriod must be at least one millisecond");
}
this.refreshPeriod = refreshPeriod;
return this;
}

/**
* @return the time unit of @link #setConnectionTimeout(int)}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/

package io.vertx.sqlclient.impl.pool;

import io.vertx.core.Promise;
import io.vertx.core.internal.ContextInternal;
import io.vertx.core.internal.pool.Lease;
import io.vertx.sqlclient.impl.pool.SqlConnectionPool.PooledConnection;

/**
* The acquisition request of a pooled connection: the borrowed connection is handed over to the requester,
* which gives it back to the pool when it closes it.
*
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
class AcquireRequest extends PoolRequest {

private final Promise<PooledConnection> handler;

AcquireRequest(SqlConnectionPool pool, ContextInternal context, Promise<PooledConnection> handler) {
super(pool, context);
this.handler = handler;
}

@Override
void handleSuccess(Lease<PooledConnection> lease) {
if (pool.afterAcquire != null) {
pool.afterAcquire.apply(lease.get().conn).onComplete((v, err) -> {
if (err == null) {
handleConnection(lease);
} else {
lease.recycle();
handler.tryFail(err);
}
});
} else {
handleConnection(lease);
}
}

private void handleConnection(Lease<PooledConnection> lease) {
PooledConnection pooled = lease.get();
pooled.timerMetric = pool.beginMetric();
// The lease is recycled when the connection is given back to the pool, see PooledConnection#cleanup
pooled.lease = lease;
handler.succeed(pooled);
}

@Override
void handleFailure(Throwable failure) {
handler.tryFail(failure);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/

package io.vertx.sqlclient.impl.pool;

import io.vertx.core.Completable;
import io.vertx.core.Future;
import io.vertx.core.internal.ContextInternal;
import io.vertx.core.internal.PromiseInternal;
import io.vertx.core.internal.pool.Lease;
import io.vertx.sqlclient.impl.pool.SqlConnectionPool.PooledConnection;
import io.vertx.sqlclient.spi.connection.Connection;
import io.vertx.sqlclient.spi.protocol.CommandBase;

/**
* The acquisition request of a pooled query: borrow a connection, schedule the command on it and give the
* connection back to the pool.
*
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
class ExecuteRequest<R> extends PoolRequest {

private final CommandBase<R> cmd;
private final Completable<R> handler;

ExecuteRequest(SqlConnectionPool pool, ContextInternal context, CommandBase<R> cmd, Completable<R> handler) {
super(pool, context);
this.cmd = cmd;
this.handler = handler;
}

@Override
void handleSuccess(Lease<PooledConnection> lease) {
// The pool delivers the lease on the context of the connection: emit on the context of the requester,
// like the command outcome
context.emit(lease, this::processSuccess);
}

private void processSuccess(Lease<PooledConnection> lease) {
PooledConnection pooled = lease.get();
pooled.timerMetric = pool.beginMetric();
Future<R> future;
if (pool.afterAcquire != null) {
Connection conn = pooled.conn;
future = pool.afterAcquire.apply(conn)
.compose(v -> Future.<R>future(d -> pooled.schedule(cmd, d)))
.eventually(() -> pool.beforeRecycle.apply(conn));
} else {
PromiseInternal<R> pp = context.promise();
pooled.schedule(cmd, pp);
future = pp;
}
future.onComplete((res, err) -> {
pool.endMetric(pooled.timerMetric);
pooled.refresh();
lease.recycle();
handler.complete(res, err);
});
}

@Override
void handleFailure(Throwable failure) {
context.emit(failure, handler::fail);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class PoolImpl extends SqlClientBase implements PoolInternal {
private final long idleTimeout;
private final long connectionTimeout;
private final long maxLifetime;
private final long cleanerPeriod;
private final long refreshPeriod;
private final boolean pipelined;
private final Handler<SqlConnection> connectionInitializer;
private final ConnectionWrapper<?> connectionWrapper;
Expand Down Expand Up @@ -85,7 +85,7 @@ public <O extends SqlConnectOptions> PoolImpl(VertxInternal vertx,
this.idleTimeout = MILLISECONDS.convert(poolOptions.getIdleTimeout(), poolOptions.getIdleTimeoutUnit());
this.connectionTimeout = MILLISECONDS.convert(poolOptions.getConnectionTimeout(), poolOptions.getConnectionTimeoutUnit());
this.maxLifetime = MILLISECONDS.convert(poolOptions.getMaxLifetime(), poolOptions.getMaxLifetimeUnit());
this.cleanerPeriod = poolOptions.getPoolCleanerPeriod();
this.refreshPeriod = poolOptions.getRefreshPeriod().toMillis();
this.timerID = -1L;
this.pipelined = pipelined;
this.vertx = vertx;
Expand All @@ -105,27 +105,27 @@ private void initializeConnection(SqlConnectionPool.PooledConnection conn) {
}

public Pool init() {
if ((idleTimeout > 0 || maxLifetime > 0) && cleanerPeriod > 0) {
synchronized (this) {
timerID = vertx.setTimer(cleanerPeriod, id -> {
runEviction();
});
}
// The periodic refresh performs all the pool maintenance tasks: it reaps the stale connections and times
// out the expired connection waiters
synchronized (this) {
timerID = vertx.setTimer(refreshPeriod, id -> {
refresh();
});
}
return this;
}

private void runEviction() {
private void refresh() {
synchronized (this) {
if (timerID == -1) {
// Cancelled
return;
}
timerID = vertx.setTimer(cleanerPeriod, id -> {
runEviction();
timerID = vertx.setTimer(refreshPeriod, id -> {
refresh();
});
}
pool.evict();
pool.refresh();
}

@Override
Expand Down
Loading
Loading