Skip to content

Commit b418ce6

Browse files
committed
Added pool_recycle setting on db connection
1 parent d5bce39 commit b418ce6

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

brood/db.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,25 @@
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(url: Optional[str], pool_size: int, statement_timeout: int, pool_recycle: int = BROOD_DB_POOL_RECYCLE_SECONDS):
1819
# Pooling: https://docs.sqlalchemy.org/en/14/core/pooling.html#sqlalchemy.pool.QueuePool
1920
# Statement timeout: https://stackoverflow.com/a/44936982
2021
return create_engine(
2122
url=url,
2223
pool_size=pool_size,
24+
pool_recycle=pool_recycle,
2325
connect_args={"options": f"-c statement_timeout={statement_timeout}"},
2426
)
2527

26-
2728
engine = create_brood_engine(
2829
url=BROOD_DB_URI,
2930
pool_size=BROOD_POOL_SIZE,
3031
statement_timeout=BROOD_DB_STATEMENT_TIMEOUT_MILLIS,
32+
pool_recycle=BROOD_DB_POOL_RECYCLE_SECONDS,
3133
)
3234
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
3335

@@ -52,6 +54,7 @@ def yield_db_session_from_env() -> Session:
5254
url=BROOD_DB_URI_READ_ONLY,
5355
pool_size=BROOD_POOL_SIZE,
5456
statement_timeout=BROOD_DB_STATEMENT_TIMEOUT_MILLIS,
57+
pool_recycle=BROOD_DB_POOL_RECYCLE_SECONDS,
5558
)
5659
RO_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=RO_engine)
5760

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)