From b418ce6db5d37f1734637436e75e8ffc741993d3 Mon Sep 17 00:00:00 2001 From: Neeraj Kashyap Date: Fri, 19 Aug 2022 09:00:09 -0700 Subject: [PATCH 1/2] Added pool_recycle setting on db connection --- brood/db.py | 7 +++++-- brood/settings.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/brood/db.py b/brood/db.py index 07c4aa7..cbb8ccb 100644 --- a/brood/db.py +++ b/brood/db.py @@ -11,23 +11,25 @@ BROOD_DB_URI_READ_ONLY, BROOD_POOL_SIZE, BROOD_DB_STATEMENT_TIMEOUT_MILLIS, + BROOD_DB_POOL_RECYCLE_SECONDS, ) -def create_brood_engine(url: Optional[str], pool_size: int, statement_timeout: int): +def create_brood_engine(url: Optional[str], pool_size: int, statement_timeout: int, pool_recycle: int = BROOD_DB_POOL_RECYCLE_SECONDS): # Pooling: https://docs.sqlalchemy.org/en/14/core/pooling.html#sqlalchemy.pool.QueuePool # Statement timeout: https://stackoverflow.com/a/44936982 return create_engine( url=url, pool_size=pool_size, + pool_recycle=pool_recycle, connect_args={"options": f"-c statement_timeout={statement_timeout}"}, ) - engine = create_brood_engine( url=BROOD_DB_URI, pool_size=BROOD_POOL_SIZE, statement_timeout=BROOD_DB_STATEMENT_TIMEOUT_MILLIS, + pool_recycle=BROOD_DB_POOL_RECYCLE_SECONDS, ) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) @@ -52,6 +54,7 @@ def yield_db_session_from_env() -> Session: url=BROOD_DB_URI_READ_ONLY, pool_size=BROOD_POOL_SIZE, statement_timeout=BROOD_DB_STATEMENT_TIMEOUT_MILLIS, + pool_recycle=BROOD_DB_POOL_RECYCLE_SECONDS, ) RO_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=RO_engine) diff --git a/brood/settings.py b/brood/settings.py index 01b2fe0..b028fae 100644 --- a/brood/settings.py +++ b/brood/settings.py @@ -53,6 +53,16 @@ f"BROOD_DB_STATEMENT_TIMEOUT_MILLIS must be an integer: {BROOD_DB_STATEMENT_TIMEOUT_MILLIS_RAW}" ) +BROOD_DB_POOL_RECYCLE_SECONDS_RAW = os.environ.get("BROOD_DB_POOL_RECYCLE_SECONDS") +BROOD_DB_POOL_RECYCLE_SECONDS = 1800 +try: + if BROOD_DB_POOL_RECYCLE_SECONDS_RAW is not None: + BROOD_DB_POOL_RECYCLE_SECONDS = int(BROOD_DB_POOL_RECYCLE_SECONDS_RAW) +except: + raise ValueError( + f"BROOD_DB_POOL_RECYCLE_SECONDS must be an integer: {BROOD_DB_POOL_RECYCLE_SECONDS_RAW}" + ) + # Bots BOT_INSTALLATION_TOKEN = os.environ.get("BUGOUT_BOT_INSTALLATION_TOKEN") BOT_INSTALLATION_TOKEN_HEADER_RAW = os.environ.get( From 0e20e29bc103b618430e1f432827eecc2809a38b Mon Sep 17 00:00:00 2001 From: Neeraj Kashyap Date: Fri, 19 Aug 2022 09:50:53 -0700 Subject: [PATCH 2/2] black formatting --- brood/db.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/brood/db.py b/brood/db.py index cbb8ccb..4af578d 100644 --- a/brood/db.py +++ b/brood/db.py @@ -15,7 +15,12 @@ ) -def create_brood_engine(url: Optional[str], pool_size: int, statement_timeout: int, pool_recycle: int = BROOD_DB_POOL_RECYCLE_SECONDS): +def create_brood_engine( + url: Optional[str], + pool_size: int, + statement_timeout: int, + pool_recycle: int = BROOD_DB_POOL_RECYCLE_SECONDS, +): # Pooling: https://docs.sqlalchemy.org/en/14/core/pooling.html#sqlalchemy.pool.QueuePool # Statement timeout: https://stackoverflow.com/a/44936982 return create_engine( @@ -25,6 +30,7 @@ def create_brood_engine(url: Optional[str], pool_size: int, statement_timeout: i connect_args={"options": f"-c statement_timeout={statement_timeout}"}, ) + engine = create_brood_engine( url=BROOD_DB_URI, pool_size=BROOD_POOL_SIZE,