Skip to content

Commit 3629f5d

Browse files
committed
feat: add jitter to max lifetime in connection pool
Signed-off-by: priyanshu-d11 <[email protected]>
1 parent b9b5a60 commit 3629f5d

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

vertx-sql-client/src/main/generated/io/vertx/sqlclient/PoolOptionsConverter.java

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, PoolOpt
4444
obj.setMaxLifetime(((Number)member.getValue()).intValue());
4545
}
4646
break;
47+
case "jitter":
48+
if (member.getValue() instanceof Number) {
49+
obj.setJitter(((Number)member.getValue()).intValue());
50+
}
51+
break;
4752
case "poolCleanerPeriod":
4853
if (member.getValue() instanceof Number) {
4954
obj.setPoolCleanerPeriod(((Number)member.getValue()).intValue());
@@ -93,6 +98,7 @@ static void toJson(PoolOptions obj, java.util.Map<String, Object> json) {
9398
json.put("maxLifetimeUnit", obj.getMaxLifetimeUnit().name());
9499
}
95100
json.put("maxLifetime", obj.getMaxLifetime());
101+
json.put("jitter", obj.getJitter());
96102
json.put("poolCleanerPeriod", obj.getPoolCleanerPeriod());
97103
if (obj.getConnectionTimeoutUnit() != null) {
98104
json.put("connectionTimeoutUnit", obj.getConnectionTimeoutUnit().name());

vertx-sql-client/src/main/java/io/vertx/sqlclient/PoolOptions.java

+29
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public class PoolOptions {
9393
*/
9494
public static final int DEFAULT_EVENT_LOOP_SIZE = 0;
9595

96+
/**
97+
* Default jitter value = 0
98+
*/
99+
public static final int DEFAULT_JITTER = 0;
100+
96101
private int maxSize = DEFAULT_MAX_SIZE;
97102
private int maxWaitQueueSize = DEFAULT_MAX_WAIT_QUEUE_SIZE;
98103
private int idleTimeout = DEFAULT_IDLE_TIMEOUT;
@@ -105,6 +110,7 @@ public class PoolOptions {
105110
private boolean shared = DEFAULT_SHARED_POOL;
106111
private String name = DEFAULT_NAME;
107112
private int eventLoopSize = DEFAULT_EVENT_LOOP_SIZE;
113+
private int jitter = DEFAULT_JITTER;
108114

109115
public PoolOptions() {
110116
}
@@ -241,6 +247,29 @@ public PoolOptions setMaxLifetime(int maxLifetime) {
241247
return this;
242248
}
243249

250+
/**
251+
* Get the jitter value that will be applied to maxLifetime.
252+
*
253+
* @return the jitter value in milliseconds
254+
*/
255+
public int getJitter() {
256+
return this.jitter;
257+
}
258+
259+
/**
260+
* Set the jitter value, to be applied to maxLifetime.
261+
*
262+
* @param jitter the jitter value
263+
* @return a reference to this, so the API can be used fluently
264+
*/
265+
public PoolOptions setJitter(int jitter) {
266+
if (jitter < 0) {
267+
throw new IllegalArgumentException("jitter must be >= 0");
268+
}
269+
this.jitter = jitter;
270+
return this;
271+
}
272+
244273
/**
245274
* @return the connection pool cleaner period in ms.
246275
*/

vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/pool/SqlConnectionPool.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import io.vertx.sqlclient.spi.DatabaseMetadata;
3333

3434
import java.util.List;
35+
import java.util.concurrent.ThreadLocalRandom;
3536
import java.util.function.Function;
3637
import java.util.stream.Collectors;
3738

@@ -56,6 +57,7 @@ public class SqlConnectionPool {
5657
private final boolean pipelined;
5758
private final long idleTimeout;
5859
private final long maxLifetime;
60+
private final long jitter;
5961
private final int maxSize;
6062

6163
public SqlConnectionPool(Function<Context, Future<SqlConnection>> connectionProvider,
@@ -66,6 +68,7 @@ public SqlConnectionPool(Function<Context, Future<SqlConnection>> connectionProv
6668
VertxInternal vertx,
6769
long idleTimeout,
6870
long maxLifetime,
71+
long jitter,
6972
int maxSize,
7073
boolean pipelined,
7174
int maxWaitQueueSize,
@@ -82,6 +85,7 @@ public SqlConnectionPool(Function<Context, Future<SqlConnection>> connectionProv
8285
this.pipelined = pipelined;
8386
this.idleTimeout = idleTimeout;
8487
this.maxLifetime = maxLifetime;
88+
this.jitter = jitter;
8589
this.maxSize = maxSize;
8690
this.hook = hook;
8791
this.connectionProvider = connectionProvider;
@@ -309,7 +313,8 @@ public class PooledConnection implements Connection, Connection.Holder {
309313
this.factory = factory;
310314
this.conn = conn;
311315
this.listener = listener;
312-
this.lifetimeEvictionTimestamp = maxLifetime > 0 ? System.currentTimeMillis() + maxLifetime : Long.MAX_VALUE;
316+
long randomizedJitter = jitter > 0 ? ThreadLocalRandom.current().nextLong(-jitter, -jitter + 1) : 0;
317+
this.lifetimeEvictionTimestamp = maxLifetime > 0 ? System.currentTimeMillis() + maxLifetime + randomizedJitter : Long.MAX_VALUE;
313318
refresh();
314319
}
315320

vertx-sql-client/src/main/java/io/vertx/sqlclient/internal/pool/PoolImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class PoolImpl extends SqlClientBase implements Pool, Closeable {
4949
private final long idleTimeout;
5050
private final long connectionTimeout;
5151
private final long maxLifetime;
52+
private final long jitter;
5253
private final long cleanerPeriod;
5354
private final boolean pipelined;
5455
private final Handler<SqlConnection> connectionInitializer;
@@ -80,11 +81,12 @@ public PoolImpl(VertxInternal vertx,
8081
this.idleTimeout = MILLISECONDS.convert(poolOptions.getIdleTimeout(), poolOptions.getIdleTimeoutUnit());
8182
this.connectionTimeout = MILLISECONDS.convert(poolOptions.getConnectionTimeout(), poolOptions.getConnectionTimeoutUnit());
8283
this.maxLifetime = MILLISECONDS.convert(poolOptions.getMaxLifetime(), poolOptions.getMaxLifetimeUnit());
84+
this.jitter = MILLISECONDS.convert(poolOptions.getJitter(), poolOptions.getMaxLifetimeUnit());
8385
this.cleanerPeriod = poolOptions.getPoolCleanerPeriod();
8486
this.timerID = -1L;
8587
this.pipelined = pipelined;
8688
this.vertx = vertx;
87-
this.pool = new SqlConnectionPool(connectionProvider, poolMetrics, hook, afterAcquire, beforeRecycle, vertx, idleTimeout, maxLifetime, poolOptions.getMaxSize(), pipelined, poolOptions.getMaxWaitQueueSize(), poolOptions.getEventLoopSize());
89+
this.pool = new SqlConnectionPool(connectionProvider, poolMetrics, hook, afterAcquire, beforeRecycle, vertx, idleTimeout, maxLifetime, jitter, poolOptions.getMaxSize(), pipelined, poolOptions.getMaxWaitQueueSize(), poolOptions.getEventLoopSize());
8890
this.closeFuture = closeFuture;
8991
this.connectionInitializer = connectionInitializer;
9092
}

0 commit comments

Comments
 (0)