Skip to content

Commit 9053990

Browse files
feat: add environment variables to allow configuration of django persistent connections (#1861)
1 parent 16fecbe commit 9053990

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ DB_PASSWORD=CHANGE_ME
2626
DB_ENGINE=django.db.backends.postgresql
2727
DB_OPTIONS_CONNECT_TIMEOUT=16
2828

29+
# Django persistent connections (production): enable for better performance
30+
# DB_CONN_MAX_AGE=60 # Seconds to keep connections open (0 = close after each request)
31+
# DB_CONN_HEALTH_CHECKS=True # Verify connection is alive before reuse (recommended with persistent connections)
32+
2933
# Optional: separate application database user/name for setup-dashboard-db.sh.
3034
# Defaults to DB_USER / DB_NAME if not set.
3135
# APP_DB_USER=dashboard

backend/README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ export DB_PORT=5432
5252
export DB_ENGINE=django_prometheus.db.backends.postgresql
5353
export DB_OPTIONS_CONNECT_TIMEOUT=16
5454
```
55+
56+
#### Connection Pooling (Production)
57+
58+
For production deployments, you can enable persistent database connections to reduce connection overhead:
59+
60+
```sh
61+
# Enable persistent connections (value in seconds)
62+
export DB_CONN_MAX_AGE=60
63+
64+
# Enable health checks to verify connections before reuse (recommended by Django docs)
65+
export DB_CONN_HEALTH_CHECKS=True
66+
```
67+
68+
- `DB_CONN_MAX_AGE`: Controls how long database connections are kept open for reuse.
69+
- `0` (default): Close connection after each request (development mode)
70+
- Positive integer: Keep connections open for that many seconds
71+
- Not recommended for local development as the dev server creates a new thread per request
72+
73+
- `DB_CONN_HEALTH_CHECKS`: When `True`, Django verifies the connection is still alive before reusing it. This prevents errors after database restarts or when connections are terminated by the server.
74+
5575
> [!NOTE]
5676
> It is possible to have authentication issues when escaping special characters. In some cases, it is necessary to add more than one backslash, while in others, no addition is needed. To assist with this, you can export `DEBUG_DB_VARS=True` to check the database connection info in the terminal, allowing you to determine if the characters got escaped as intended. **This variable should NOT be set to True in production**.
5777

@@ -229,7 +249,7 @@ In order to debug backend in PyCharm, just follow these steps:
229249
- in `Run` session of the dialog, select `script`, then find the script `manage.py` at the `backend` folder
230250
- at `script` name input, just enter `runserver`
231251
- at `Environment Variables`, enter the following values:
232-
- `DB_ENGINE`: `django.db.backends.postgresql`, `DB_NAME`: `dashboard`, `DB_USER`: `<youremail>@profusion.mobi`, `DB_PASSWORD`: `<yourpassword>`, `DB_HOST`: `127.0.0.1`, `DB_PORT`: `5432`, `DB_OPTIONS_CONNECT_TIMEOUT`: `16`
252+
- `DB_ENGINE`: `django.db.backends.postgresql`, `DB_NAME`: `dashboard`, `DB_USER`: `<youremail>@profusion.mobi`, `DB_PASSWORD`: `<yourpassword>`, `DB_HOST`: `127.0.0.1`, `DB_PORT`: `5432`, `DB_OPTIONS_CONNECT_TIMEOUT`: `16`, `DB_CONN_MAX_AGE`: `0`, `DB_CONN_HEALTH_CHECKS`: `False`
233253
- `DEBUG`: `True`
234254
235255
Quote character in password field is escaped normally with `\"` .

backend/kernelCI/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ def get_json_env_var(name, default):
261261
"HOST": os.getenv("DB_HOST", "127.0.0.1"),
262262
"PORT": os.getenv("DB_PORT", "5432"),
263263
"ENGINE": os.getenv("DB_ENGINE", "django_prometheus.db.backends.postgresql"),
264+
"CONN_MAX_AGE": int(os.getenv("DB_CONN_MAX_AGE", "0")),
265+
"CONN_HEALTH_CHECKS": is_boolean_or_string_true(
266+
os.environ.get("DB_CONN_HEALTH_CHECKS", False)
267+
),
264268
"OPTIONS": {
265269
"connect_timeout": int(os.getenv("DB_OPTIONS_CONNECT_TIMEOUT", "16")),
266270
},

0 commit comments

Comments
 (0)