Use MariaDB connection pooling instead of single connection
This commit is contained in:
@@ -144,6 +144,7 @@ def load_config(config_path):
|
|||||||
|
|
||||||
cfg.setdefault("database", {})
|
cfg.setdefault("database", {})
|
||||||
cfg["database"].setdefault("backend", "sqlite")
|
cfg["database"].setdefault("backend", "sqlite")
|
||||||
|
cfg["database"].setdefault("pool_size", 5)
|
||||||
|
|
||||||
cfg.setdefault("dns_service", {})
|
cfg.setdefault("dns_service", {})
|
||||||
cfg["dns_service"].setdefault("dns_server", "127.0.0.1")
|
cfg["dns_service"].setdefault("dns_server", "127.0.0.1")
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ from peewee import (
|
|||||||
ForeignKeyField,
|
ForeignKeyField,
|
||||||
IntegerField,
|
IntegerField,
|
||||||
Model,
|
Model,
|
||||||
MySQLDatabase,
|
|
||||||
SqliteDatabase,
|
SqliteDatabase,
|
||||||
)
|
)
|
||||||
|
from playhouse.pool import PooledMySQLDatabase
|
||||||
|
|
||||||
# Database proxy (initialized later with actual backend)
|
# Database proxy (initialized later with actual backend)
|
||||||
db = DatabaseProxy()
|
db = DatabaseProxy()
|
||||||
@@ -124,12 +124,13 @@ def init_database(config: dict):
|
|||||||
logging.debug(f"Database backend: SQLite path={db_path}")
|
logging.debug(f"Database backend: SQLite path={db_path}")
|
||||||
|
|
||||||
elif backend == "mariadb":
|
elif backend == "mariadb":
|
||||||
actual_db = MySQLDatabase(
|
actual_db = PooledMySQLDatabase(
|
||||||
config["database"]["database"],
|
config["database"]["database"],
|
||||||
host=config["database"].get("host", "localhost"),
|
host=config["database"].get("host", "localhost"),
|
||||||
port=config["database"].get("port", 3306),
|
port=config["database"].get("port", 3306),
|
||||||
user=config["database"]["user"],
|
user=config["database"]["user"],
|
||||||
password=config["database"]["password"],
|
password=config["database"]["password"],
|
||||||
|
max_connections=config["database"].get("pool_size", 5),
|
||||||
)
|
)
|
||||||
db.initialize(actual_db)
|
db.initialize(actual_db)
|
||||||
db_name = config['database']['database']
|
db_name = config['database']['database']
|
||||||
|
|||||||
Reference in New Issue
Block a user