vttranlina commented on code in PR #2190:
URL: https://github.com/apache/james-project/pull/2190#discussion_r1576015166


##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PoolBackedPostgresConnectionFactory.java:
##########
@@ -0,0 +1,93 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.backends.postgres.utils;
+
+import java.util.Optional;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.pool.ConnectionPool;
+import io.r2dbc.pool.ConnectionPoolConfiguration;
+import io.r2dbc.spi.Connection;
+import io.r2dbc.spi.ConnectionFactory;
+import reactor.core.publisher.Mono;
+
+public class PoolBackedPostgresConnectionFactory implements 
JamesPostgresConnectionFactory {
+    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 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> maybeInitialSize, Optional<Integer> 
maybeMaxSize, ConnectionFactory connectionFactory) {
+        this.rowLevelSecurityEnabled = rowLevelSecurityEnabled;
+        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(), Optional.empty(), 
connectionFactory);
+    }
+
+    @Override
+    public Mono<Connection> getConnection(Optional<Domain> domain) {
+        if (rowLevelSecurityEnabled) {
+            return pool.create().flatMap(connection -> 
setDomainAttributeForConnection(domain.orElse(DEFAULT), connection));
+        } else {
+            return pool.create();
+        }
+    }
+
+    @Override
+    public Mono<Void> closeConnection(Connection connection) {
+        return Mono.from(connection.close());

Review Comment:
   IMO, we should unset `DOMAIN_ATTRIBUTE` before "releasing" the connection to 
the pool. 
   It will be safer, even if you already `set` a new attribute when get 
Connection.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org
For additional commands, e-mail: notifications-h...@james.apache.org

Reply via email to