forked from Disroot/gpg-lacre
lacre.repositories: Configure SQLAlchemy connection pooling
Provide 3 new configuration parameters in database section: - max_connection_age --- number of seconds before an idle connection is "recycled", i.e. replaced with a new one; - pool_size --- number of simultaneous connections kept in the pool; - max_overflow --- maximum number of simultaneous connections we could make to the database. Update sample config, including links to documentation.
This commit is contained in:
parent
18a64bcd72
commit
86cc27e918
2 changed files with 31 additions and 2 deletions
|
@ -109,6 +109,18 @@ url = sqlite:///test.db
|
|||
# For other RDBMS backends, see:
|
||||
# https://docs.sqlalchemy.org/en/14/core/engines.html#database-urls
|
||||
|
||||
# Number of seconds after which an idle connection is recycled. This is
|
||||
# useful with MySQL servers. For more information, see:
|
||||
# https://docs.sqlalchemy.org/en/14/core/engines.html#sqlalchemy.create_engine.params.pool_recycle
|
||||
#max_connection_age = 3600
|
||||
|
||||
# Number of connections stored in the pool.
|
||||
#pool_size = 5
|
||||
|
||||
# If the pool size is not enough for current traffic, some connections can be
|
||||
# made and closed after use, to avoid pool growth and connection rejections.
|
||||
#max_overflow = 10
|
||||
|
||||
[enc_keymap]
|
||||
# You can find these by running the following command:
|
||||
# gpg --list-keys --keyid-format long user@example.com
|
||||
|
|
|
@ -4,7 +4,7 @@ from sqlalchemy import create_engine, select, delete, and_, func
|
|||
from sqlalchemy.exc import OperationalError
|
||||
import logging
|
||||
|
||||
from lacre.config import flag_enabled
|
||||
from lacre.config import flag_enabled, config_item_set, get_item
|
||||
from lacre._keyringcommon import KeyRing, KeyCache
|
||||
import lacre.dbschema as db
|
||||
|
||||
|
@ -19,10 +19,27 @@ def connect(url):
|
|||
global _engine
|
||||
|
||||
if not _engine:
|
||||
_engine = create_engine(url)
|
||||
config = _conn_config()
|
||||
_engine = create_engine(url, **config)
|
||||
|
||||
return _engine.connect()
|
||||
|
||||
|
||||
def _conn_config():
|
||||
config = dict()
|
||||
|
||||
if config_item_set('database', 'max_connection_age'):
|
||||
config['pool_recycle'] = get_item('database', 'max_connection_age')
|
||||
|
||||
if config_item_set('database', 'pool_size'):
|
||||
config['pool_size'] = get_item('database', 'pool_size')
|
||||
|
||||
if config_item_set('database', 'max_overflow'):
|
||||
config['max_overflow'] = get_item('database', 'max_overflow')
|
||||
|
||||
return config
|
||||
|
||||
|
||||
class IdentityRepository(KeyRing):
|
||||
def __init__(self, /, connection=None, db_url=None):
|
||||
self._identities = db.LACRE_IDENTITIES
|
||||
|
|
Loading…
Reference in a new issue