feat(chalice): PG-client unkeyed connexion

This commit is contained in:
Taha Yassine Kraiem 2022-08-08 19:42:45 +02:00
parent b7685b0239
commit 210cfe8511
6 changed files with 24 additions and 7 deletions

View file

@ -5,7 +5,7 @@ RUN apk upgrade busybox --no-cache --repository=http://dl-cdn.alpinelinux.org/al
RUN apk add --no-cache build-base tini
ARG envarg
ENV APP_NAME=alerts \
pg_minconn=2 \
pg_minconn=1 \
pg_maxconn=10 \
ENTERPRISE_BUILD=${envarg}

View file

@ -44,6 +44,8 @@ RETRY = 0
def make_pool():
if not config('PG_POOL', cast=bool, default=True):
return
global postgreSQL_pool
global RETRY
if postgreSQL_pool is not None:
@ -68,7 +70,8 @@ def make_pool():
raise error
make_pool()
if config('PG_POOL', cast=bool, default=True):
make_pool()
class PostgresClient:
@ -87,8 +90,14 @@ class PostgresClient:
elif long_query:
long_config = dict(_PG_CONFIG)
long_config["application_name"] += "-LONG"
long_config["options"] = f"-c statement_timeout={config('pg_long_timeout', cast=int, default=5 * 60) * 1000}"
long_config["options"] = f"-c statement_timeout=" \
f"{config('pg_long_timeout', cast=int, default=5 * 60) * 1000}"
self.connection = psycopg2.connect(**long_config)
elif not config('PG_POOL', cast=bool, default=True):
single_config = dict(_PG_CONFIG)
single_config["application_name"] += "-NOPOOL"
single_config["options"] = f"-c statement_timeout={config('pg_timeout', cast=int, default=3 * 60) * 1000}"
self.connection = psycopg2.connect(**single_config)
else:
self.connection = postgreSQL_pool.getconn()
@ -105,13 +114,18 @@ class PostgresClient:
self.connection.close()
except Exception as error:
print("Error while committing/closing PG-connection", error)
if str(error) == "connection already closed" and not self.long_query and not self.unlimited_query:
if str(error) == "connection already closed" \
and not self.long_query \
and not self.unlimited_query \
and config('PG_POOL', cast=bool, default=True):
print("Recreating the connexion pool")
make_pool()
else:
raise error
finally:
if not self.long_query and not self.unlimited_query:
if config('PG_POOL', cast=bool, default=True) \
and not self.long_query \
and not self.unlimited_query:
postgreSQL_pool.putconn(self.connection)

View file

@ -40,6 +40,7 @@ pg_minconn=20
pg_maxconn=50
PG_RETRY_MAX=50
PG_RETRY_INTERVAL=2
PG_POOL=true
put_S3_TTL=20
sentryURL=
sessions_bucket=mobs

View file

@ -5,7 +5,7 @@ RUN apk upgrade busybox --no-cache --repository=http://dl-cdn.alpinelinux.org/al
RUN apk add --no-cache build-base tini
ARG envarg
ENV APP_NAME=alerts \
pg_minconn=2 \
pg_minconn=1 \
pg_maxconn=10 \
ENTERPRISE_BUILD=${envarg}

View file

@ -8,7 +8,8 @@ ENV APP_NAME=crons \
pg_minconn=2 \
pg_maxconn=10 \
ENTERPRISE_BUILD=${envarg} \
ACTION=""
ACTION="" \
PG_POOL=false
WORKDIR /work_tmp
COPY requirements-crons.txt /work_tmp/requirements.txt

View file

@ -49,6 +49,7 @@ pg_minconn=20
pg_maxconn=50
PG_RETRY_MAX=50
PG_RETRY_INTERVAL=2
PG_POOL=true
put_S3_TTL=20
sentryURL=
sessions_bucket=mobs