Skip to content

Commit 1621798

Browse files
authored
Merge pull request #46 from bugout-dev/db-pool-recycle
Added pool_recycle setting on db connection
2 parents d5bce39 + 0e20e29 commit 1621798

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

brood/db.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@
1111
BROOD_DB_URI_READ_ONLY,
1212
BROOD_POOL_SIZE,
1313
BROOD_DB_STATEMENT_TIMEOUT_MILLIS,
14+
BROOD_DB_POOL_RECYCLE_SECONDS,
1415
)
1516

1617

17-
def create_brood_engine(url: Optional[str], pool_size: int, statement_timeout: int):
18+
def create_brood_engine(
19+
url: Optional[str],
20+
pool_size: int,
21+
statement_timeout: int,
22+
pool_recycle: int = BROOD_DB_POOL_RECYCLE_SECONDS,
23+
):
1824
# Pooling: https://docs.sqlalchemy.org/en/14/core/pooling.html#sqlalchemy.pool.QueuePool
1925
# Statement timeout: https://stackoverflow.com/a/44936982
2026
return create_engine(
2127
url=url,
2228
pool_size=pool_size,
29+
pool_recycle=pool_recycle,
2330
connect_args={"options": f"-c statement_timeout={statement_timeout}"},
2431
)
2532

@@ -28,6 +35,7 @@ def create_brood_engine(url: Optional[str], pool_size: int, statement_timeout: i
2835
url=BROOD_DB_URI,
2936
pool_size=BROOD_POOL_SIZE,
3037
statement_timeout=BROOD_DB_STATEMENT_TIMEOUT_MILLIS,
38+
pool_recycle=BROOD_DB_POOL_RECYCLE_SECONDS,
3139
)
3240
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
3341

@@ -52,6 +60,7 @@ def yield_db_session_from_env() -> Session:
5260
url=BROOD_DB_URI_READ_ONLY,
5361
pool_size=BROOD_POOL_SIZE,
5462
statement_timeout=BROOD_DB_STATEMENT_TIMEOUT_MILLIS,
63+
pool_recycle=BROOD_DB_POOL_RECYCLE_SECONDS,
5564
)
5665
RO_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=RO_engine)
5766

brood/settings.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@
5353
f"BROOD_DB_STATEMENT_TIMEOUT_MILLIS must be an integer: {BROOD_DB_STATEMENT_TIMEOUT_MILLIS_RAW}"
5454
)
5555

56+
BROOD_DB_POOL_RECYCLE_SECONDS_RAW = os.environ.get("BROOD_DB_POOL_RECYCLE_SECONDS")
57+
BROOD_DB_POOL_RECYCLE_SECONDS = 1800
58+
try:
59+
if BROOD_DB_POOL_RECYCLE_SECONDS_RAW is not None:
60+
BROOD_DB_POOL_RECYCLE_SECONDS = int(BROOD_DB_POOL_RECYCLE_SECONDS_RAW)
61+
except:
62+
raise ValueError(
63+
f"BROOD_DB_POOL_RECYCLE_SECONDS must be an integer: {BROOD_DB_POOL_RECYCLE_SECONDS_RAW}"
64+
)
65+
5666
# Bots
5767
BOT_INSTALLATION_TOKEN = os.environ.get("BUGOUT_BOT_INSTALLATION_TOKEN")
5868
BOT_INSTALLATION_TOKEN_HEADER_RAW = os.environ.get(

0 commit comments

Comments
 (0)