Skip to content

Commit

Permalink
fix: backport more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Feb 9, 2024
1 parent 23256ee commit 4390abe
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [4.0.3] - 2024-02-07

- Adds tests to check connection pool behaviour.
- Adds `postgresql_idle_connection_timeout` and `postgresql_minimum_idle_connections` configs to control active connections to the database.

## [4.0.2]

Expand Down
8 changes: 8 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,11 @@ postgresql_config_version: 0
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "thirdparty_users") string value. Specify the name of the table
# that will store the thirdparty recipe users.
# postgresql_thirdparty_users_table_name

# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 60000) long value. Timeout in milliseconds for the idle connections
# to be closed.
# postgresql_idle_connection_timeout:

# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 1) integer value. Minimum number of idle connections to be kept
# active.
# postgresql_minimum_idle_connections:
8 changes: 8 additions & 0 deletions devConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ postgresql_password: "root"
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "thirdparty_users") string value. Specify the name of the table
# that will store the thirdparty recipe users.
# postgresql_thirdparty_users_table_name

# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 60000) long value. Timeout in milliseconds for the idle connections
# to be closed.
# postgresql_idle_connection_timeout:

# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 1) integer value. Minimum number of idle connections to be kept
# active.
# postgresql_minimum_idle_connections:
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ private synchronized void initialiseHikariDataSource() throws SQLException {
}
config.setMaximumPoolSize(userConfig.getConnectionPoolSize());
config.setConnectionTimeout(5000);
config.setIdleTimeout(userConfig.getIdleConnectionTimeout());
config.setMinimumIdle(userConfig.getMinimumIdleConnections());
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public class Start
// SaaS. If the core is not running in SuperTokens SaaS, this array has no effect.
private static String[] PROTECTED_DB_CONFIG = new String[]{"postgresql_connection_pool_size",
"postgresql_connection_uri", "postgresql_host", "postgresql_port", "postgresql_user", "postgresql_password",
"postgresql_database_name", "postgresql_table_schema"};
"postgresql_database_name", "postgresql_table_schema", "postgresql_idle_connection_timeout",
"postgresql_minimum_idle_connections"};
private static final Object appenderLock = new Object();
public static boolean silent = false;
private ResourceDistributor resourceDistributor = new ResourceDistributor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ public class PostgreSQLConfig {
@ConnectionPoolProperty
private String postgresql_connection_scheme = "postgresql";

@JsonProperty
@ConnectionPoolProperty
private long postgresql_idle_connection_timeout = 60000;

@JsonProperty
@ConnectionPoolProperty
private int postgresql_minimum_idle_connections = 1;

@IgnoreForAnnotationCheck
boolean isValidAndNormalised = false;

Expand Down Expand Up @@ -234,6 +242,14 @@ public String getThirdPartyUsersTable() {
return postgresql_thirdparty_users_table_name;
}

public long getIdleConnectionTimeout() {
return postgresql_idle_connection_timeout;
}

public int getMinimumIdleConnections() {
return postgresql_minimum_idle_connections;
}

public String getThirdPartyUserToTenantTable() {
return addSchemaAndPrefixToTableName("thirdparty_user_to_tenant");
}
Expand Down Expand Up @@ -340,6 +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 > postgresql_connection_pool_size) {
throw new InvalidConfigException(
"'postgresql_minimum_idle_connections' must be less than or equal to "
+ "'postgresql_connection_pool_size'");
}

// Normalisation
if (postgresql_connection_uri != null) {
{ // postgresql_connection_attributes
Expand Down
Loading

0 comments on commit 4390abe

Please sign in to comment.