From 002aa1d6fd42248b4834b7e7c5b8ba45d872dc74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Kol=C3=A1=C5=99?= Date: Sun, 15 Sep 2024 20:37:16 +0200 Subject: [PATCH] feat(db): reuse connections for 5 minutes --- fiesta/fiesta/settings/__init__.py | 6 +++- fiesta/fiesta/settings/db.py | 53 +++++++++++++++++------------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/fiesta/fiesta/settings/__init__.py b/fiesta/fiesta/settings/__init__.py index 3ee5960a..97906835 100644 --- a/fiesta/fiesta/settings/__init__.py +++ b/fiesta/fiesta/settings/__init__.py @@ -72,7 +72,11 @@ class Production( @property def DATABASES(self): return { - "default": dj_database_url.parse(self.DATABASE_URL), + "default": dj_database_url.parse( + self.DATABASE_URL, + conn_max_age=self.DATABASE_CONN_MAX_AGE, + conn_health_checks=self.DATABASE_CONN_HEALTH_CHECKS, + ), "wiki": DatabaseConfigMixin.DATABASES["wiki"], } diff --git a/fiesta/fiesta/settings/db.py b/fiesta/fiesta/settings/db.py index 0b72edc3..b27136c9 100644 --- a/fiesta/fiesta/settings/db.py +++ b/fiesta/fiesta/settings/db.py @@ -1,26 +1,35 @@ from __future__ import annotations +from configurations.values import Value + class DatabaseConfigMixin: - DATABASES = { - "default": { - "ENGINE": "django.db.backends.postgresql", - "HOST": "db", - "USER": "fiesta", - "NAME": "fiesta", - "PASSWORD": "fiesta", - }, - "legacydb": { - "ENGINE": "django.db.backends.mysql", - "HOST": "legacydb", - "NAME": "fiesta", - # TODO: access to legacy db by standard user - "USER": "root", - "PASSWORD": "root", - }, - "wiki": { - "ENGINE": "django.db.backends.sqlite3", - # TODO: pass from ENV - "NAME": "/usr/src/wiki/db/wiki.sqlite3", - }, - } + DATABASE_CONN_MAX_AGE = Value(default=5 * 60) + DATABASE_CONN_HEALTH_CHECKS = Value(default=True) + + @property + def DATABASES(self): + return { + "default": { + "ENGINE": "django.db.backends.postgresql", + "HOST": "db", + "USER": "fiesta", + "NAME": "fiesta", + "PASSWORD": "fiesta", + "CONN_MAX_AGE": self.DATABASE_CONN_MAX_AGE, + "CONN_HEALTH_CHECKS": self.DATABASE_CONN_HEALTH_CHECKS, + }, + "legacydb": { + "ENGINE": "django.db.backends.mysql", + "HOST": "legacydb", + "NAME": "fiesta", + # TODO: access to legacy db by standard user + "USER": "root", + "PASSWORD": "root", + }, + "wiki": { + "ENGINE": "django.db.backends.sqlite3", + # TODO: pass from ENV + "NAME": "/usr/src/wiki/db/wiki.sqlite3", + }, + }