Skip to content

Commit

Permalink
Introduce maxLifeTime for connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
hamadodene committed May 5, 2023
1 parent 868d50f commit b99d8c0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static final class ConnectionPoolBean {
private int connectTimeout;
private int stuckRequestTimeout;
private int idleTimeout;
private int maxLifeTime;
private int disposeTimeout;
private int keepaliveIdle;
private int keepaliveInterval;
Expand Down Expand Up @@ -92,6 +93,7 @@ public Map<String, ConnectionPoolBean> getAll() {
conf.getConnectTimeout(),
conf.getStuckRequestTimeout(),
conf.getIdleTimeout(),
conf.getMaxLifeTime(),
conf.getDisposeTimeout(),
conf.getKeepaliveIdle(),
conf.getKeepaliveInterval(),
Expand All @@ -114,6 +116,7 @@ public Map<String, ConnectionPoolBean> getAll() {
defaultConnectionPool.getConnectTimeout(),
defaultConnectionPool.getStuckRequestTimeout(),
defaultConnectionPool.getIdleTimeout(),
defaultConnectionPool.getMaxLifeTime(),
defaultConnectionPool.getDisposeTimeout(),
defaultConnectionPool.getKeepaliveIdle(),
defaultConnectionPool.getKeepaliveInterval(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ private boolean isConnectionsConfigurationChanged(RuntimeServerConfiguration new
|| newConfiguration.getConnectTimeout() != currentConfiguration.getConnectTimeout()
|| newConfiguration.getStuckRequestTimeout() != currentConfiguration.getStuckRequestTimeout()
|| newConfiguration.getIdleTimeout() != currentConfiguration.getIdleTimeout()
|| newConfiguration.getMaxLifeTime() != currentConfiguration.getMaxLifeTime()
|| !newConfiguration.getConnectionPools().equals(currentConfiguration.getConnectionPools());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ public void reloadConfiguration(RuntimeServerConfiguration newConfiguration, Col
spec.maxConnections(connectionPool.getMaxConnectionsPerEndpoint());
spec.pendingAcquireTimeout(Duration.ofMillis(connectionPool.getBorrowTimeout()));
spec.maxIdleTime(Duration.ofMillis(connectionPool.getIdleTimeout()));
spec.maxLifeTime(Duration.ofMillis(connectionPool.getMaxLifeTime()));
spec.evictInBackground(Duration.ofMillis(connectionPool.getIdleTimeout() * 2));
spec.metrics(true);
spec.lifo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class RuntimeServerConfiguration {

private int maxConnectionsPerEndpoint = 10;
private int idleTimeout = 60_000;
private int maxLifeTime = idleTimeout * 2;
private int stuckRequestTimeout = 120_000;
private boolean backendsUnreachableOnStuckRequests = false;
private int connectTimeout = 10_000;
Expand Down Expand Up @@ -120,6 +121,7 @@ public RuntimeServerConfiguration() {
connectTimeout,
stuckRequestTimeout,
idleTimeout,
maxLifeTime,
disposeTimeout,
keepaliveIdle,
keepaliveInterval,
Expand All @@ -136,6 +138,10 @@ public void configure(ConfigurationStore properties) throws ConfigurationNotVali
if (this.idleTimeout <= 0) {
throw new ConfigurationNotValidException("Invalid value '" + this.idleTimeout + "' for connectionsmanager.idletimeout");
}
this.maxLifeTime = properties.getInt("connectionsmanager.maxlifetime", maxLifeTime);
if (this.maxLifeTime <= 0) {
throw new ConfigurationNotValidException("Invalid value '" + this.maxLifeTime + "' for connectionsmanager.maxlifetime");
}
this.stuckRequestTimeout = properties.getInt("connectionsmanager.stuckrequesttimeout", stuckRequestTimeout);
this.backendsUnreachableOnStuckRequests = properties.getBoolean("connectionsmanager.backendsunreachableonstuckrequests", backendsUnreachableOnStuckRequests);
this.connectTimeout = properties.getInt("connectionsmanager.connecttimeout", connectTimeout);
Expand All @@ -146,6 +152,7 @@ public void configure(ConfigurationStore properties) throws ConfigurationNotVali
this.keepaliveCount = properties.getInt("connectionsmanager.keepalivecount", keepaliveCount);
LOG.log(Level.INFO, "connectionsmanager.maxconnectionsperendpoint={0}", maxConnectionsPerEndpoint);
LOG.log(Level.INFO, "connectionsmanager.idletimeout={0}", idleTimeout);
LOG.log(Level.INFO, "connectionsmanager.maxlifetime={0}", maxLifeTime);
LOG.log(Level.INFO, "connectionsmanager.stuckrequesttimeout={0}", stuckRequestTimeout);
LOG.log(Level.INFO, "connectionsmanager.backendsunreachableonstuckrequests={0}", backendsUnreachableOnStuckRequests);
LOG.log(Level.INFO, "connectionsmanager.connecttimeout={0}", connectTimeout);
Expand Down Expand Up @@ -354,6 +361,7 @@ private void configureConnectionPools(ConfigurationStore properties) throws Conf
int connecttimeout = properties.getInt(prefix + "connecttimeout", connectTimeout);
int stuckrequesttimeout = properties.getInt(prefix + "stuckrequesttimeout", stuckRequestTimeout);
int idletimeout = properties.getInt(prefix + "idletimeout", idleTimeout);
int maxlifetime = properties.getInt(prefix + "maxlifetime", maxLifeTime);
int disposetimeout = properties.getInt(prefix + "disposetimeout", disposeTimeout);
int keepaliveidle = properties.getInt(prefix + "keepaliveidle", keepaliveIdle);
int keepaliveinterval = properties.getInt(prefix + "keepaliveinterval", keepaliveInterval);
Expand All @@ -368,6 +376,7 @@ private void configureConnectionPools(ConfigurationStore properties) throws Conf
connecttimeout,
stuckrequesttimeout,
idletimeout,
maxlifetime,
disposetimeout,
keepaliveidle,
keepaliveinterval,
Expand All @@ -387,6 +396,7 @@ private void configureConnectionPools(ConfigurationStore properties) throws Conf
getConnectTimeout(),
getStuckRequestTimeout(),
getIdleTimeout(),
getMaxLifeTime(),
getDisposeTimeout(),
getKeepaliveIdle(),
getKeepaliveInterval(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ConnectionPoolConfiguration {
private int connectTimeout;
private int stuckRequestTimeout;
private int idleTimeout;
private int maxLifeTime;
private int disposeTimeout;
private int keepaliveIdle;
private int keepaliveInterval;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void test() throws Exception {

// default pool
ConnectionPoolConfiguration defaultPool = new ConnectionPoolConfiguration(
"*", "*", 10, 5_000, 10_000, 15_000, 20_000, 50_000, 500, 50, 5, true,true
"*", "*", 10, 5_000, 10_000, 15_000, 20_000, 40_000, 50_000, 500, 50, 5, true,true
);
{
ConnectionProvider provider = connectionPools.get(defaultPool);
Expand All @@ -177,7 +177,7 @@ public void test() throws Exception {

// pool with defaults
ConnectionPoolConfiguration poolWithDefaults = new ConnectionPoolConfiguration(
"localhost", "localhost", 10, 5_000, 10_000, 15_000, 20_000, 50_000, 500, 50, 5, true, true
"localhost", "localhost", 10, 5_000, 10_000, 15_000, 20_000, 40_000, 50_000, 500, 50, 5, true, true
);
{
ConnectionProvider provider = connectionPools.get(poolWithDefaults);
Expand All @@ -189,7 +189,7 @@ public void test() throws Exception {

// custom pool
ConnectionPoolConfiguration customPool = new ConnectionPoolConfiguration(
"localhosts", "localhost[0-9]", 20, 21_000, 22_000, 23_000, 24_000, 25_000, 250, 25, 2, true,true
"localhosts", "localhost[0-9]", 20, 21_000, 22_000, 23_000, 24_000, 40_000, 25_000, 250, 25, 2, true,true
);
{
ConnectionProvider provider = connectionPools.get(customPool);
Expand Down Expand Up @@ -294,22 +294,22 @@ public void testAPIResource() throws Exception {

// default pool
assertThat(pools.get("*"), is(new ConnectionPoolsResource.ConnectionPoolBean(
"*", "*", 10, 5_000, 10_000, 15_000, 20_000, 50_000, 500, 50, 5, true,true, 0
"*", "*", 10, 5_000, 10_000, 15_000, 20_000, 40_000, 50_000, 500, 50, 5, true,true, 0
)));

// pool with defaults
assertThat(pools.get("localhost"), is(new ConnectionPoolsResource.ConnectionPoolBean(
"localhost", "localhost", 10, 5_000, 10_000, 15_000, 20_000, 50_000, 500, 50, 5, true,true, 1
"localhost", "localhost", 10, 5_000, 10_000, 15_000, 20_000, 40_000,50_000, 500, 50, 5, true,true, 1
)));

// disabled custom pool
assertThat(pools.get("localhost2"), is(new ConnectionPoolsResource.ConnectionPoolBean(
"localhost2", "localhost2", 10, 5_000, 10_000, 15_000, 20_000, 50_000, 500, 50, 5, true, false, 0
"localhost2", "localhost2", 10, 5_000, 10_000, 15_000, 20_000, 40_000, 50_000, 500, 50, 5, true, false, 0
)));

// custom pool
assertThat(pools.get("localhosts"), is(new ConnectionPoolsResource.ConnectionPoolBean(
"localhosts", "localhost[0-9]", 20, 21_000, 22_000, 23_000, 24_000, 25_000, 250, 25, 2, true, true, 0
"localhosts", "localhost[0-9]", 20, 21_000, 22_000, 23_000, 24_000, 40_000, 25_000, 250, 25, 2, true, true, 0
)));
}
}
Expand Down

0 comments on commit b99d8c0

Please sign in to comment.