diff --git a/config.yaml b/config.yaml index 81032770..38ade78f 100644 --- a/config.yaml +++ b/config.yaml @@ -72,6 +72,6 @@ postgresql_config_version: 0 # to be closed. # postgresql_idle_connection_timeout: -# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 1) integer value. Minimum number of idle connections to be kept -# active. +# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: null) integer value. Minimum number of idle connections to be kept +# active. If not set, minimum idle connections will be same as the connection pool size. # postgresql_minimum_idle_connections: diff --git a/devConfig.yaml b/devConfig.yaml index 3af330ff..a25dba97 100644 --- a/devConfig.yaml +++ b/devConfig.yaml @@ -74,6 +74,6 @@ postgresql_password: "root" # to be closed. # postgresql_idle_connection_timeout: -# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 1) integer value. Minimum number of idle connections to be kept -# active. +# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: null) integer value. Minimum number of idle connections to be kept +# active. If not set, minimum idle connections will be same as the connection pool size. # postgresql_minimum_idle_connections: diff --git a/src/main/java/io/supertokens/storage/postgresql/ConnectionPool.java b/src/main/java/io/supertokens/storage/postgresql/ConnectionPool.java index 4f5cc53a..d209411f 100644 --- a/src/main/java/io/supertokens/storage/postgresql/ConnectionPool.java +++ b/src/main/java/io/supertokens/storage/postgresql/ConnectionPool.java @@ -82,7 +82,9 @@ private synchronized void initialiseHikariDataSource() throws SQLException { config.setMaximumPoolSize(userConfig.getConnectionPoolSize()); config.setConnectionTimeout(5000); config.setIdleTimeout(userConfig.getIdleConnectionTimeout()); - config.setMinimumIdle(userConfig.getMinimumIdleConnections()); + if (userConfig.getMinimumIdleConnections() != null) { + config.setMinimumIdle(userConfig.getMinimumIdleConnections()); + } config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); diff --git a/src/main/java/io/supertokens/storage/postgresql/config/PostgreSQLConfig.java b/src/main/java/io/supertokens/storage/postgresql/config/PostgreSQLConfig.java index 1e57cc2a..2c464849 100644 --- a/src/main/java/io/supertokens/storage/postgresql/config/PostgreSQLConfig.java +++ b/src/main/java/io/supertokens/storage/postgresql/config/PostgreSQLConfig.java @@ -118,7 +118,7 @@ public class PostgreSQLConfig { @JsonProperty @ConnectionPoolProperty - private int postgresql_minimum_idle_connections = 1; + private Integer postgresql_minimum_idle_connections = null; @IgnoreForAnnotationCheck boolean isValidAndNormalised = false; @@ -246,7 +246,7 @@ public long getIdleConnectionTimeout() { return postgresql_idle_connection_timeout; } - public int getMinimumIdleConnections() { + public Integer getMinimumIdleConnections() { return postgresql_minimum_idle_connections; } @@ -356,15 +356,17 @@ public void validateAndNormalise() throws InvalidConfigException { "'postgresql_connection_pool_size' in the config.yaml file must be > 0"); } - if (postgresql_minimum_idle_connections <= 0) { - throw new InvalidConfigException( - "'postgresql_minimum_idle_connections' must be a positive value"); - } + if (postgresql_minimum_idle_connections != null) { + if (postgresql_minimum_idle_connections < 0) { + throw new InvalidConfigException( + "'postgresql_minimum_idle_connections' must be >= 0"); + } - if (postgresql_minimum_idle_connections > postgresql_connection_pool_size) { - throw new InvalidConfigException( - "'postgresql_minimum_idle_connections' must be less than or equal to " - + "'postgresql_connection_pool_size'"); + if (postgresql_minimum_idle_connections > postgresql_connection_pool_size) { + throw new InvalidConfigException( + "'postgresql_minimum_idle_connections' must be less than or equal to " + + "'postgresql_connection_pool_size'"); + } } // Normalisation diff --git a/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java b/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java index 94434670..cdf0c28c 100644 --- a/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java +++ b/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java @@ -66,7 +66,6 @@ public void testActiveConnectionsWithTenants() throws Exception { TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false); FeatureFlagTestContent.getInstance(process.getProcess()) .setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY}); - Utils.setValueInConfig("postgresql_minimum_idle_connections", "10"); process.startProcess(); assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); @@ -90,7 +89,6 @@ public void testActiveConnectionsWithTenants() throws Exception { // change connection pool size config.addProperty("postgresql_connection_pool_size", 20); - config.addProperty("postgresql_minimum_idle_connections", 20); Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig( new TenantIdentifier(null, null, "t1"), @@ -123,7 +121,6 @@ public void testDownTimeWhenChangingConnectionPoolSize() throws Exception { FeatureFlagTestContent.getInstance(process.getProcess()) .setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY}); process.startProcess(); - Utils.setValueInConfig("postgresql_minimum_idle_connections", "10"); assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); Start start = (Start) StorageLayer.getBaseStorage(process.getProcess()); @@ -132,7 +129,6 @@ public void testDownTimeWhenChangingConnectionPoolSize() throws Exception { JsonObject config = new JsonObject(); start.modifyConfigToAddANewUserPoolForTesting(config, 1); config.addProperty("postgresql_connection_pool_size", 300); - config.addProperty("postgresql_minimum_idle_connections", 300); AtomicLong firstErrorTime = new AtomicLong(-1); AtomicLong successAfterErrorTime = new AtomicLong(-1); AtomicInteger errorCount = new AtomicInteger(0); @@ -196,7 +192,6 @@ public void testDownTimeWhenChangingConnectionPoolSize() throws Exception { // change connection pool size config.addProperty("postgresql_connection_pool_size", 200); - config.addProperty("postgresql_minimum_idle_connections", 200); Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig( new TenantIdentifier(null, null, "t1"), @@ -271,7 +266,6 @@ public void testMinimumIdleConnectionForTenants() throws Exception { TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false); FeatureFlagTestContent.getInstance(process.getProcess()) .setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY}); - Utils.setValueInConfig("postgresql_minimum_idle_connections", "10"); process.startProcess(); assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); @@ -326,7 +320,6 @@ public void testIdleConnectionTimeout() throws Exception { TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false); FeatureFlagTestContent.getInstance(process.getProcess()) .setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY}); - Utils.setValueInConfig("postgresql_minimum_idle_connections", "10"); process.startProcess(); assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));