This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch postgresql in repository https://gitbox.apache.org/repos/asf/james-project.git
commit a9492092cd39c90db014d4f30ce60a7a5bcb41e4 Author: hung phan <hp...@linagora.com> AuthorDate: Wed Apr 17 11:08:27 2024 +0700 JAMES-2586 Add connection pool config to PostgresConfiguration --- .../backends/postgres/PostgresConfiguration.java | 45 +++++++++++++++++++++- .../utils/PoolBackedPostgresConnectionFactory.java | 20 +++++----- .../sample-configuration/postgres.properties | 6 +++ .../james/modules/data/PostgresCommonModule.java | 14 +++---- 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresConfiguration.java b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresConfiguration.java index e00c803fd3..bfffc4ddee 100644 --- a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresConfiguration.java +++ b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresConfiguration.java @@ -45,6 +45,8 @@ public class PostgresConfiguration { public static final String NON_RLS_USERNAME = "database.non-rls.username"; public static final String NON_RLS_PASSWORD = "database.non-rls.password"; public static final String RLS_ENABLED = "row.level.security.enabled"; + public static final String POOL_INITIAL_SIZE = "pool.initial.size"; + public static final String POOL_MAX_SIZE = "pool.max.size"; public static final String SSL_MODE = "ssl.mode"; public static final String SSL_MODE_DEFAULT_VALUE = "allow"; public static final String JOOQ_REACTIVE_TIMEOUT = "jooq.reactive.timeout"; @@ -79,6 +81,8 @@ public class PostgresConfiguration { private Optional<String> nonRLSUser = Optional.empty(); private Optional<String> nonRLSPassword = Optional.empty(); private Optional<Boolean> rowLevelSecurityEnabled = Optional.empty(); + private Optional<Integer> poolInitialSize = Optional.empty(); + private Optional<Integer> poolMaxSize = Optional.empty(); private Optional<String> sslMode = Optional.empty(); private Optional<Duration> jooqReactiveTimeout = Optional.empty(); @@ -172,6 +176,26 @@ public class PostgresConfiguration { return this; } + public Builder poolInitialSize(Optional<Integer> poolInitialSize) { + this.poolInitialSize = poolInitialSize; + return this; + } + + public Builder poolInitialSize(Integer poolInitialSize) { + this.poolInitialSize = Optional.of(poolInitialSize); + return this; + } + + public Builder poolMaxSize(Optional<Integer> poolMaxSize) { + this.poolMaxSize = poolMaxSize; + return this; + } + + public Builder poolMaxSize(Integer poolMaxSize) { + this.poolMaxSize = Optional.of(poolMaxSize); + return this; + } + public Builder sslMode(Optional<String> sslMode) { this.sslMode = sslMode; return this; @@ -203,6 +227,8 @@ public class PostgresConfiguration { new Credential(username.get(), password.get()), new Credential(nonRLSUser.orElse(username.get()), nonRLSPassword.orElse(password.get())), rowLevelSecurityEnabled.orElse(false), + poolInitialSize, + poolMaxSize, SSLMode.fromValue(sslMode.orElse(SSL_MODE_DEFAULT_VALUE)), jooqReactiveTimeout.orElse(JOOQ_REACTIVE_TIMEOUT_DEFAULT_VALUE)); } @@ -223,6 +249,8 @@ public class PostgresConfiguration { .nonRLSUser(Optional.ofNullable(propertiesConfiguration.getString(NON_RLS_USERNAME))) .nonRLSPassword(Optional.ofNullable(propertiesConfiguration.getString(NON_RLS_PASSWORD))) .rowLevelSecurityEnabled(propertiesConfiguration.getBoolean(RLS_ENABLED, false)) + .poolInitialSize(Optional.ofNullable(propertiesConfiguration.getInteger(POOL_INITIAL_SIZE, null))) + .poolMaxSize(Optional.ofNullable(propertiesConfiguration.getInteger(POOL_MAX_SIZE, null))) .sslMode(Optional.ofNullable(propertiesConfiguration.getString(SSL_MODE))) .jooqReactiveTimeout(Optional.ofNullable(propertiesConfiguration.getString(JOOQ_REACTIVE_TIMEOUT)) .map(value -> DurationParser.parse(value, ChronoUnit.SECONDS))) @@ -236,11 +264,14 @@ public class PostgresConfiguration { private final Credential credential; private final Credential nonRLSCredential; private final boolean rowLevelSecurityEnabled; + private final Optional<Integer> poolInitialSize; + private final Optional<Integer> poolMaxSize; private final SSLMode sslMode; private final Duration jooqReactiveTimeout; private PostgresConfiguration(String host, int port, String databaseName, String databaseSchema, Credential credential, Credential nonRLSCredential, boolean rowLevelSecurityEnabled, + Optional<Integer> poolInitialSize, Optional<Integer> poolMaxSize, SSLMode sslMode, Duration jooqReactiveTimeout) { this.host = host; this.port = port; @@ -249,6 +280,8 @@ public class PostgresConfiguration { this.credential = credential; this.nonRLSCredential = nonRLSCredential; this.rowLevelSecurityEnabled = rowLevelSecurityEnabled; + this.poolInitialSize = poolInitialSize; + this.poolMaxSize = poolMaxSize; this.sslMode = sslMode; this.jooqReactiveTimeout = jooqReactiveTimeout; } @@ -281,6 +314,14 @@ public class PostgresConfiguration { return rowLevelSecurityEnabled; } + public Optional<Integer> poolInitialSize() { + return poolInitialSize; + } + + public Optional<Integer> poolMaxSize() { + return poolMaxSize; + } + public SSLMode getSslMode() { return sslMode; } @@ -291,7 +332,7 @@ public class PostgresConfiguration { @Override public final int hashCode() { - return Objects.hash(host, port, databaseName, databaseSchema, credential, nonRLSCredential, rowLevelSecurityEnabled, sslMode, jooqReactiveTimeout); + return Objects.hash(host, port, databaseName, databaseSchema, credential, nonRLSCredential, rowLevelSecurityEnabled, poolInitialSize, poolMaxSize, sslMode, jooqReactiveTimeout); } @Override @@ -306,6 +347,8 @@ public class PostgresConfiguration { && Objects.equals(this.nonRLSCredential, that.nonRLSCredential) && Objects.equals(this.databaseName, that.databaseName) && Objects.equals(this.databaseSchema, that.databaseSchema) + && Objects.equals(this.poolInitialSize, that.poolInitialSize) + && Objects.equals(this.poolMaxSize, that.poolMaxSize) && Objects.equals(this.sslMode, that.sslMode) && Objects.equals(this.jooqReactiveTimeout, that.jooqReactiveTimeout); } diff --git a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PoolBackedPostgresConnectionFactory.java b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PoolBackedPostgresConnectionFactory.java index 7ad10839bf..ea97e70627 100644 --- a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PoolBackedPostgresConnectionFactory.java +++ b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PoolBackedPostgresConnectionFactory.java @@ -19,7 +19,6 @@ package org.apache.james.backends.postgres.utils; -import java.time.Duration; import java.util.Optional; import org.apache.james.core.Domain; @@ -36,25 +35,26 @@ public class PoolBackedPostgresConnectionFactory implements JamesPostgresConnect private static final Logger LOGGER = LoggerFactory.getLogger(PoolBackedPostgresConnectionFactory.class); private static final Domain DEFAULT = Domain.of("default"); private static final String DEFAULT_DOMAIN_ATTRIBUTE_VALUE = ""; - private static final int INITIAL_SIZE = 10; - private static final int MAX_SIZE = 20; - private static final Duration MAX_IDLE_TIME = Duration.ofMillis(5000); + private static final int DEFAULT_INITIAL_SIZE = 10; + private static final int DEFAULT_MAX_SIZE = 20; private final boolean rowLevelSecurityEnabled; private final ConnectionPool pool; - public PoolBackedPostgresConnectionFactory(boolean rowLevelSecurityEnabled, Optional<Integer> maxSize, ConnectionFactory connectionFactory) { + public PoolBackedPostgresConnectionFactory(boolean rowLevelSecurityEnabled, Optional<Integer> maybeInitialSize, Optional<Integer> maybeMaxSize, ConnectionFactory connectionFactory) { this.rowLevelSecurityEnabled = rowLevelSecurityEnabled; - final ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory) - .maxIdleTime(MAX_IDLE_TIME) - .initialSize(INITIAL_SIZE) - .maxSize(maxSize.orElse(MAX_SIZE)) + int initialSize = maybeInitialSize.orElse(DEFAULT_INITIAL_SIZE); + int maxSize = maybeMaxSize.orElse(DEFAULT_MAX_SIZE); + ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory) + .initialSize(initialSize) + .maxSize(maxSize) .build(); + LOGGER.info("Creating new postgres ConnectionPool with initialSize {} and maxSize {}", initialSize, maxSize); pool = new ConnectionPool(configuration); } public PoolBackedPostgresConnectionFactory(boolean rowLevelSecurityEnabled, ConnectionFactory connectionFactory) { - this(rowLevelSecurityEnabled, Optional.empty(), connectionFactory); + this(rowLevelSecurityEnabled, Optional.empty(), Optional.empty(), connectionFactory); } @Override diff --git a/server/apps/postgres-app/sample-configuration/postgres.properties b/server/apps/postgres-app/sample-configuration/postgres.properties index 8710adb814..a8780c5133 100644 --- a/server/apps/postgres-app/sample-configuration/postgres.properties +++ b/server/apps/postgres-app/sample-configuration/postgres.properties @@ -25,6 +25,12 @@ row.level.security.enabled=false # String. It is required when row.level.security.enabled is true. Database password of non-rls user. #database.non-rls.password=secret1 +# Integer. Optional, default to 5432. Database connection pool initial size. +pool.initial.size=10 + +# Integer. Optional, default to 5432. Database connection pool max size. +pool.max.size=20 + # String. Optional, defaults to allow. SSLMode required to connect to the Postgresql db server. # Check https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-PROTECTION for a list of supported SSLModes. ssl.mode=allow diff --git a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java index cc5214df17..9bcb3b3196 100644 --- a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java +++ b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java @@ -75,15 +75,13 @@ public class PostgresCommonModule extends AbstractModule { @Provides @Singleton JamesPostgresConnectionFactory provideJamesPostgresConnectionFactory(PostgresConfiguration postgresConfiguration, - ConnectionFactory connectionFactory, - @Named(JamesPostgresConnectionFactory.NON_RLS_INJECT) JamesPostgresConnectionFactory jamesPostgresConnectionFactory) { - if (postgresConfiguration.rowLevelSecurityEnabled()) { - LOGGER.info("PostgreSQL row level security enabled"); - LOGGER.info("Implementation for PostgreSQL connection factory: {}", PoolBackedPostgresConnectionFactory.class.getName()); - return new PoolBackedPostgresConnectionFactory(true, connectionFactory); - } + ConnectionFactory connectionFactory) { + LOGGER.info("Is PostgreSQL row level security enabled? {}", postgresConfiguration.rowLevelSecurityEnabled()); LOGGER.info("Implementation for PostgreSQL connection factory: {}", PoolBackedPostgresConnectionFactory.class.getName()); - return new PoolBackedPostgresConnectionFactory(false, connectionFactory); + return new PoolBackedPostgresConnectionFactory(postgresConfiguration.rowLevelSecurityEnabled(), + postgresConfiguration.poolInitialSize(), + postgresConfiguration.poolMaxSize(), + connectionFactory); } @Provides --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org