Skip to content

Commit 92b7a0f

Browse files
authored
Set max-life-time to shrink pool (#237)
* Even though idle connections are evicted from the pool it's likeley that connections in the pool are "touched" also during infrequent usage, i.e. they are not idle. * By setting the max-life-time the connections are cycled out from the pool and the number of connections in the pool will shrink, as long as there is not demand for more connections.
1 parent 14057a2 commit 92b7a0f

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

core/src/main/resources/reference.conf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,15 @@ akka.persistence.r2dbc {
141141
acquire-retry = 20
142142

143143
# Maximum idle time of the connection in the pool.
144-
# This value is used as an interval for background eviction of idle connections.
144+
# Background eviction interval of idle connections is derived from this property
145+
# and max-life-time.
145146
max-idle-time = 30 minutes
146147

148+
# Maximum lifetime of the connection in the pool.
149+
# Background eviction interval of connections is derived from this property
150+
# and max-idle-time.
151+
max-life-time = 60 minutes
152+
147153
# Configures the statement cache size.
148154
# 0 means no cache, negative values will select an unbounded cache
149155
# a positive value will configure a bounded cache with the passed size.

core/src/main/scala/akka/persistence/r2dbc/ConnectionFactoryProvider.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.time.{ Duration => JDuration }
88
import java.util.concurrent.ConcurrentHashMap
99

1010
import scala.concurrent.Future
11+
import scala.concurrent.duration.Duration
1112
import scala.jdk.CollectionConverters.ConcurrentMapHasAsScala
1213

1314
import akka.Done
@@ -96,6 +97,19 @@ class ConnectionFactoryProvider(system: ActorSystem[_]) extends Extension {
9697
private def createConnectionPoolFactory(settings: ConnectionFactorySettings): ConnectionPool = {
9798
val connectionFactory = createConnectionFactory(settings)
9899

100+
val evictionInterval = {
101+
import settings.{ maxIdleTime, maxLifeTime }
102+
if (maxIdleTime <= Duration.Zero && maxLifeTime <= Duration.Zero) {
103+
JDuration.ZERO
104+
} else if (maxIdleTime <= Duration.Zero) {
105+
JDuration.ofMillis((maxLifeTime / 4).toMillis)
106+
} else if (maxLifeTime <= Duration.Zero) {
107+
JDuration.ofMillis((maxIdleTime / 4).toMillis)
108+
} else {
109+
JDuration.ofMillis((maxIdleTime.min(maxIdleTime) / 4).toMillis)
110+
}
111+
}
112+
99113
val poolConfiguration = ConnectionPoolConfiguration
100114
.builder(connectionFactory)
101115
.initialSize(settings.initialSize)
@@ -104,9 +118,8 @@ class ConnectionFactoryProvider(system: ActorSystem[_]) extends Extension {
104118
.maxAcquireTime(JDuration.ofMillis(settings.acquireTimeout.toMillis))
105119
.acquireRetry(settings.acquireRetry)
106120
.maxIdleTime(JDuration.ofMillis(settings.maxIdleTime.toMillis))
107-
// setting lifetime due to issue https://github.com/r2dbc/r2dbc-pool/issues/129
108-
.maxLifeTime(JDuration.ofDays(365 * 100))
109-
.backgroundEvictionInterval(JDuration.ofMillis((settings.maxIdleTime / 4).toMillis))
121+
.maxLifeTime(JDuration.ofMillis(settings.maxLifeTime.toMillis))
122+
.backgroundEvictionInterval(evictionInterval)
110123

111124
if (settings.validationQuery.nonEmpty)
112125
poolConfiguration.validationQuery(settings.validationQuery)

core/src/main/scala/akka/persistence/r2dbc/R2dbcSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ final class ConnectionFactorySettings(config: Config) {
101101
val initialSize: Int = config.getInt("initial-size")
102102
val maxSize: Int = config.getInt("max-size")
103103
val maxIdleTime: FiniteDuration = config.getDuration("max-idle-time").asScala
104+
val maxLifeTime: FiniteDuration = config.getDuration("max-life-time").asScala
104105

105106
val createTimeout: FiniteDuration = config.getDuration("create-timeout").asScala
106107
val acquireTimeout: FiniteDuration = config.getDuration("acquire-timeout").asScala

0 commit comments

Comments
 (0)