Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Environment variables
| `TENANT_HEADER` | `<empty>` | The name of the HTTP header which contains the tenant name for multi-tenant setups. |
| `TENANT_PATH_PREFIX` | `@service_prefix@/@tenant@` | URL path prefix for all QWC services for multi-tenant setups. |
| `TENANT_ACCESS_COOKIE_PATH` | `<tenant_path_prefix>` | Path for which the access cookie is valid for multi-tenant setups. |
| `POOL_SIZE` | `5` | Maximum number of possible data base connections. |
| `MAX_OVERFLOW` | `10` | Additional connections beyond pool_size during peak load. |
| `POOL_TIMEOUT` | `30` | Time (in seconds) to wait for a connection to become available. |
| `POOL_RECYCLE` | `-1` | Time (in seconds) after idle connections will be resetted. |

Development
===========
Expand Down
18 changes: 15 additions & 3 deletions qwc_services_core/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from sqlalchemy import create_engine

from sqlalchemy.pool import QueuePool

class DatabaseEngine():
"""Helper for database connections using SQLAlchemy engines"""
Expand All @@ -15,12 +15,24 @@ def db_engine(self, conn_str):

:param str conn_str: DB connection string for SQLAlchemy engine

see http://docs.sqlalchemy.org/en/latest/core/engines.html#postgresql
see https://docs.sqlalchemy.org/en/latest/core/engines.html#postgresql
"""

db_pool_size = os.environ.get('POOL_SIZE', 5)
db_max_overflow = os.environ.get('MAX_OVERFLOW', 10)
db_pool_timeout = os.environ.get('POOL_TIMEOUT', 30)
db_pool_recycle = os.environ.get('POOL_RECYCLE', -1)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd set the default values directly here and remove them from the db_engine args.

Also, self.db_engine_var does not return an environment variable value, but invokes self.db_engine to return a DB Engine configured with the connections string as configured in the specified environment variable. Use os.environ.get here.

engine = self.engines.get(conn_str)
if not engine:
engine = create_engine(
conn_str, pool_pre_ping=True, echo=False)
conn_str,
poolclass=QueuePool,
pool_size=db_pool_size,
max_overflow=db_max_overflow,
pool_timeout=db_pool_timeout,
pool_recycle=db_pool_recycle,
pool_pre_ping=True, echo=False)
self.engines[conn_str] = engine
return engine

Expand Down