diff --git a/api/chalicelib/core/sessions_mobs.py b/api/chalicelib/core/sessions_mobs.py index 7c3e7fd0a..ea020d412 100644 --- a/api/chalicelib/core/sessions_mobs.py +++ b/api/chalicelib/core/sessions_mobs.py @@ -1,10 +1,10 @@ from chalicelib.utils.helper import environ -import boto3 +from chalicelib.utils.s3 import client def get_web(sessionId): - return boto3.client('s3', region_name=environ["sessions_region"]).generate_presigned_url( + return client.generate_presigned_url( 'get_object', Params={ 'Bucket': environ["sessions_bucket"], @@ -15,7 +15,7 @@ def get_web(sessionId): def get_ios(sessionId): - return boto3.client('s3', region_name=environ["ios_region"]).generate_presigned_url( + return client.generate_presigned_url( 'get_object', Params={ 'Bucket': environ["ios_bucket"], diff --git a/api/chalicelib/utils/pg_client.py b/api/chalicelib/utils/pg_client.py index 8d1e37d40..89a9dc8fa 100644 --- a/api/chalicelib/utils/pg_client.py +++ b/api/chalicelib/utils/pg_client.py @@ -9,9 +9,25 @@ PG_CONFIG = {"host": environ["pg_host"], "port": int(environ["pg_port"])} from psycopg2 import pool +from threading import Semaphore + + +class ORThreadedConnectionPool(psycopg2.pool.ThreadedConnectionPool): + def __init__(self, minconn, maxconn, *args, **kwargs): + self._semaphore = Semaphore(maxconn) + super().__init__(minconn, maxconn, *args, **kwargs) + + def getconn(self, *args, **kwargs): + self._semaphore.acquire() + return super().getconn(*args, **kwargs) + + def putconn(self, *args, **kwargs): + super().putconn(*args, **kwargs) + self._semaphore.release() + try: - postgreSQL_pool = psycopg2.pool.ThreadedConnectionPool(6, 20, **PG_CONFIG) + postgreSQL_pool = ORThreadedConnectionPool(20, 100, **PG_CONFIG) if (postgreSQL_pool): print("Connection pool created successfully") except (Exception, psycopg2.DatabaseError) as error: @@ -19,13 +35,6 @@ except (Exception, psycopg2.DatabaseError) as error: raise error -# finally: -# # closing database connection. -# # use closeall method to close all the active connection if you want to turn of the application -# if (postgreSQL_pool): -# postgreSQL_pool.closeall -# print("PostgreSQL connection pool is closed") - class PostgresClient: connection = None cursor = None