Skip to content

Commit

Permalink
feat: make pg connections max lifetime configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitFicus committed Jul 11, 2024
1 parent c9316c2 commit 58a92e5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
8 changes: 6 additions & 2 deletions app/fr/maif/izanami/env/postgresql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class Postgresql(env: Env) {

import scala.jdk.CollectionConverters._

private val logger = Logger("izanami")
private val logger = Logger("izanami")

lazy val connectOptions = if (configuration.has("app.pg.uri")) {
logger.info(s"Postgres URI : ${configuration.get[String]("app.pg.uri")}")
val opts = PgConnectOptions.fromUri(configuration.get[String]("app.pg.uri"))
Expand Down Expand Up @@ -93,7 +94,10 @@ class Postgresql(env: Env) {
lazy val vertx = Vertx.vertx()
private lazy val poolOptions = new PoolOptions()
.setMaxSize(configuration.getOptional[Int]("app.pg.pool-size").getOrElse(100))
private lazy val pool = PgPool.pool(connectOptions, poolOptions)
.applyOnWithOpt(configuration.getOptional[Int]("idle-timeout"))((p, v) => p.setIdleTimeout(v))
.applyOnWithOpt(configuration.getOptional[Int]("max-lifetime"))((p, v) => p.setMaxLifetime(v))

private lazy val pool = PgPool.pool(connectOptions, poolOptions)

private val configuration = env.configuration

Expand Down
1 change: 1 addition & 0 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ app {
password = ${?POSTGRESQL_ADDON_PASSWORD}
connect-timeout = ${?IZANAMI_PG_CONNECT_TIMEOUT}
idle-timeout = ${?IZANAMI_PG_IDLE_TIMEOUT}
max-lifetime = ${?IZANAMI_PG_MAX_LIFETIME}
log-activity = ${?IZANAMI_PG_LOG_ACTIVITY}
pipelining-limit = ${?IZANAMI_PG_PIPELINING_LIMIT}
ssl {
Expand Down
27 changes: 14 additions & 13 deletions manual/docs/04-guides/11-configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ You can either set the `IZANAMI_SECRET` env variable or use the `app.secret` par

You can either provide a connection URI, or indicate database name, user, host, port and so on separately.

| | Environnement variable | Program argument | Default |
| ------------------- | --------------------------- | ----------------------- | -------------- |
| Uri | IZANAMI_PG_URI | app.pg.uri |
| Port | IZANAMI_PG_PORT | app.pg.port | 5432 |
| Host | IZANAMI_PG_HOST | app.pg.host |
| Database name | IZANAMI_PG_DATABASE | app.pg.database |
| User | IZANAMI_PG_USER | app.pg.user |
| Password | IZANAMI_PG_PASSWORD | app.pg.password |
| Pool size | IZANAMI_PG_POOL_SIZE | app.pg.pool-size | 20 |
| Connect timeout(ms) | IZANAMI_PG_CONNECT_TIMEOUT | app.pg.connect-timeout | 60000 |
| IDLE timeout(s) | IZANAMI_PG_IDLE_TIMEOUT | app.pg.idle-timeout | 0 (no timeout) |
| Log activity | IZANAMI_PG_LOG_ACTIVITY | app.pg.log-activity | false |
| Pipelining limit | IZANAMI_PG_PIPELINING_LIMIT | app.pg.pipelining-limit | 256 |
| | Environnement variable | Program argument | Default |
| ----------------------- | --------------------------- | ----------------------- | -------------- |
| Uri | IZANAMI_PG_URI | app.pg.uri |
| Port | IZANAMI_PG_PORT | app.pg.port | 5432 |
| Host | IZANAMI_PG_HOST | app.pg.host |
| Database name | IZANAMI_PG_DATABASE | app.pg.database |
| User | IZANAMI_PG_USER | app.pg.user |
| Password | IZANAMI_PG_PASSWORD | app.pg.password |
| Pool size | IZANAMI_PG_POOL_SIZE | app.pg.pool-size | 20 |
| Connect timeout(ms) | IZANAMI_PG_CONNECT_TIMEOUT | app.pg.connect-timeout | 60000 |
| Connection max lifetime | IZANAMI_PG_MAX_LIFETIME | app.pg.max-lifetime | 0 (no maximum) |
| IDLE timeout(s) | IZANAMI_PG_IDLE_TIMEOUT | app.pg.idle-timeout | 0 (no timeout) |
| Log activity | IZANAMI_PG_LOG_ACTIVITY | app.pg.log-activity | false |
| Pipelining limit | IZANAMI_PG_PIPELINING_LIMIT | app.pg.pipelining-limit | 256 |

### Admin account

Expand Down

0 comments on commit 58a92e5

Please sign in to comment.