refactor(chalice): changed pg_client to send keep-alive signals
This commit is contained in:
parent
5b6c653862
commit
499048e46c
1 changed files with 12 additions and 2 deletions
|
|
@ -19,6 +19,16 @@ PG_CONFIG = dict(_PG_CONFIG)
|
||||||
if config("PG_TIMEOUT", cast=int, default=0) > 0:
|
if config("PG_TIMEOUT", cast=int, default=0) > 0:
|
||||||
PG_CONFIG["options"] = f"-c statement_timeout={config('PG_TIMEOUT', cast=int) * 1000}"
|
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):
|
class ORThreadedConnectionPool(psycopg2.pool.ThreadedConnectionPool):
|
||||||
def __init__(self, minconn, maxconn, *args, **kwargs):
|
def __init__(self, minconn, maxconn, *args, **kwargs):
|
||||||
|
|
@ -55,6 +65,7 @@ RETRY = 0
|
||||||
|
|
||||||
def make_pool():
|
def make_pool():
|
||||||
if not config('PG_POOL', cast=bool, default=True):
|
if not config('PG_POOL', cast=bool, default=True):
|
||||||
|
logger.info("PG_POOL is disabled, not creating a new one")
|
||||||
return
|
return
|
||||||
global postgreSQL_pool
|
global postgreSQL_pool
|
||||||
global RETRY
|
global RETRY
|
||||||
|
|
@ -176,8 +187,7 @@ class PostgresClient:
|
||||||
|
|
||||||
async def init():
|
async def init():
|
||||||
logger.info(f">use PG_POOL:{config('PG_POOL', default=True)}")
|
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():
|
async def terminate():
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue