From 499048e46ced7725f4dffc17f0d839716724c859 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Fri, 14 Mar 2025 13:50:54 +0100 Subject: [PATCH] refactor(chalice): changed pg_client to send keep-alive signals --- api/chalicelib/utils/pg_client.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/api/chalicelib/utils/pg_client.py b/api/chalicelib/utils/pg_client.py index 0f7d498b1..fea58c457 100644 --- a/api/chalicelib/utils/pg_client.py +++ b/api/chalicelib/utils/pg_client.py @@ -19,6 +19,16 @@ PG_CONFIG = dict(_PG_CONFIG) if config("PG_TIMEOUT", cast=int, default=0) > 0: PG_CONFIG["options"] = f"-c statement_timeout={config('PG_TIMEOUT', cast=int) * 1000}" +if config('PG_POOL', cast=bool, default=True): + PG_CONFIG = { + **PG_CONFIG, + # Keepalive settings + "keepalives": 1, # Enable keepalives + "keepalives_idle": 300, # Seconds before sending keepalive + "keepalives_interval": 10, # Seconds between keepalives + "keepalives_count": 3 # Number of keepalives before giving up + } + class ORThreadedConnectionPool(psycopg2.pool.ThreadedConnectionPool): def __init__(self, minconn, maxconn, *args, **kwargs): @@ -55,6 +65,7 @@ RETRY = 0 def make_pool(): if not config('PG_POOL', cast=bool, default=True): + logger.info("PG_POOL is disabled, not creating a new one") return global postgreSQL_pool global RETRY @@ -176,8 +187,7 @@ class PostgresClient: async def init(): logger.info(f">use PG_POOL:{config('PG_POOL', default=True)}") - if config('PG_POOL', cast=bool, default=True): - make_pool() + make_pool() async def terminate():