Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

fix/Connection Pool for Platform and Prompt service (#670)

harini-venkataraman37378ab41182
diff --git a/platform-service/sample.env b/platform-service/sample.env
index 0e533af8c..666835055 100644
--- a/platform-service/sample.env
+++ b/platform-service/sample.env
@@ -26,3 +26,10 @@ FLIPT_SERVICE_AVAILABLE=False
MODEL_PRICES_URL="https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json"
MODEL_PRICES_TTL_IN_DAYS=7
MODEL_PRICES_FILE_PATH="/tmp/model_prices.json"
+
+#Connection Pool ENVs
+POOL_MAX_CONNECTIONS=32
+# Number of seconds to allow connections to be used. Same as gunicorn timeout
+POOL_STALE_TIMEOUT=900
+# Number of seconds to block when pool is full. Set to 5 minutes.
+POOL_TIMEOUT=300
diff --git a/platform-service/src/unstract/platform_service/controller/platform.py b/platform-service/src/unstract/platform_service/controller/platform.py
index dbed388da..ad56a83b2 100644
--- a/platform-service/src/unstract/platform_service/controller/platform.py
+++ b/platform-service/src/unstract/platform_service/controller/platform.py
@@ -3,12 +3,12 @@ import uuid
from datetime import datetime
from typing import Any, Optional
-import peewee
import redis
from cryptography.fernet import Fernet, InvalidToken
from flask import Blueprint, Request
from flask import current_app as app
from flask import jsonify, make_response, request
+from playhouse.pool import PooledPostgresqlDatabase
from unstract.platform_service.constants import DBTable, DBTableV2, FeatureFlag
from unstract.platform_service.env import Env
from unstract.platform_service.exceptions import APIError
@@ -20,12 +20,15 @@ from unstract.platform_service.helper.prompt_studio import PromptStudioRequestHe
from unstract.flags.feature_flag import check_feature_flag_status
-be_db = peewee.PostgresqlDatabase(
+be_db = PooledPostgresqlDatabase(
Env.PG_BE_DATABASE,
user=Env.PG_BE_USERNAME,
password=Env.PG_BE_PASSWORD,
host=Env.PG_BE_HOST,
port=Env.PG_BE_PORT,
+ max_connections=Env.POOL_MAX_CONNECTIONS,
+ stale_timeout=Env.POOL_STALE_TIMEOUT,
+ timeout=Env.POOL_TIMEOUT,
)
be_db.init(Env.PG_BE_DATABASE)
be_db.connect()
@@ -110,6 +113,7 @@ def validate_bearer_token(token: Optional[str]) -> bool:
cursor = be_db.execute_sql(query)
result_row = cursor.fetchone()
cursor.close()
+ be_db.close()
if not result_row or len(result_row) == 0:
app.logger.error(f"Authentication failed. bearer token not found {token}")
return False
diff --git a/platform-service/src/unstract/platform_service/env.py b/platform-service/src/unstract/platform_service/env.py
index a938c4a44..f1a21c1f0 100644
--- a/platform-service/src/unstract/platform_service/env.py
+++ b/platform-service/src/unstract/platform_service/env.py
@@ -22,6 +22,11 @@ class Env:
EnvManager.get_required_setting("MODEL_PRICES_TTL_IN_DAYS")
)
MODEL_PRICES_FILE_PATH = EnvManager.get_required_setting("MODEL_PRICES_FILE_PATH")
+ POOL_MAX_CONNECTIONS = int(
+ EnvManager.get_required_setting("POOL_MAX_CONNECTIONS", 32)
+ )
+ POOL_STALE_TIMEOUT = int(EnvManager.get_required_setting("POOL_STALE_TIMEOUT", 900))
+ POOL_TIMEOUT = int(EnvManager.get_required_setting("POOL_TIMEOUT", 300))
EnvManager.raise_for_missing_envs()
diff --git a/platform-service/src/unstract/platform_service/helper/adapter_instance.py b/platform-service/src/unstract/platform_service/helper/adapter_instance.py
index b8bf5a418..6667c1791 100644
--- a/platform-service/src/unstract/platform_service/helper/adapter_instance.py
+++ b/platform-service/src/unstract/platform_service/helper/adapter_instance.py
@@ -1,6 +1,6 @@
from typing import Any, Optional
-import peewee
+from playhouse.pool import PooledPostgresqlDatabase
from unstract.platform_service.constants import DBTableV2, FeatureFlag
from unstract.platform_service.exceptions import APIError
@@ -10,7 +10,7 @@ from unstract.flags.feature_flag import check_feature_flag_status
class AdapterInstanceRequestHelper:
@staticmethod
def get_adapter_instance_from_db(
- db_instance: peewee.PostgresqlDatabase,
+ db_instance: PooledPostgresqlDatabase,
organization_id: str,
adapter_instance_id: str,
organization_uid: Optional[int] = None,
@@ -18,7 +18,7 @@ class AdapterInstanceRequestHelper:
"""Get adapter instance from Backend Database.
Args:
- db_instance (peewee.PostgresqlDatabase): Backend DB
+ db_instance (PooledPostgresqlDatabase): Backend DB Connection Pool
organization_id (str): organization schema id
adapter_instance_id (str): adapter instance id
diff --git a/platform-service/src/unstract/platform_service/helper/prompt_studio.py b/platform-service/src/unstract/platform_service/helper/prompt_studio.py
index 5de4b2cae..276358f76 100644
--- a/platform-service/src/unstract/platform_service/helper/prompt_studio.py
+++ b/platform-service/src/unstract/platform_service/helper/prompt_studio.py
@@ -1,6 +1,6 @@
from typing import Any
-import peewee
+from playhouse.pool import PooledPostgresqlDatabase
from unstract.platform_service.constants import DBTableV2, FeatureFlag
from unstract.platform_service.exceptions import APIError
@@ -10,14 +10,14 @@ from unstract.flags.feature_flag import check_feature_flag_status
class PromptStudioRequestHelper:
@staticmethod
def get_prompt_instance_from_db(
- db_instance: peewee.PostgresqlDatabase,
+ db_instance: PooledPostgresqlDatabase,
organization_id: str,
prompt_registry_id: str,
) -> dict[str, Any]:
"""Get prompt studio registry from Backend Database.
Args:
- db_instance (peewee.PostgresqlDatabase): Backend DB
+ db_instance (PooledPostgresqlDatabase): Backend DB Connection Pool
organization_id (str): organization schema id
prompt_registry_id (str): prompt_registry_id
diff --git a/prompt-service/sample.env b/prompt-service/sample.env
index 53f27dad2..752394286 100644
--- a/prompt-service/sample.env
+++ b/prompt-service/sample.env
@@ -27,3 +27,10 @@ EVALUATION_SERVER_PORT=9000
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
# Flipt Service
FLIPT_SERVICE_AVAILABLE=False
+
+#Connection Pool ENVs
+POOL_MAX_CONNECTIONS=32
+# Number of seconds to allow connections to be used. Same as gunicorn timeout
+POOL_STALE_TIMEOUT=900
+# Number of seconds to block when pool is full. Set to 5 minutes.
+POOL_TIMEOUT=300
diff --git a/prompt-service/src/unstract/prompt_service/authentication_middleware.py b/prompt-service/src/unstract/prompt_service/authentication_middleware.py
index fbf03c37b..530114ab9 100644
--- a/prompt-service/src/unstract/prompt_service/authentication_middleware.py
+++ b/prompt-service/src/unstract/prompt_service/authentication_middleware.py
@@ -25,6 +25,7 @@ class AuthenticationMiddleware:
cursor = db.execute_sql(query)
result_row = cursor.fetchone()
cursor.close()
+ db.close()
if not result_row or len(result_row) == 0:
current_app.logger.error(
f"Authentication failed. bearer token not found {token}"
diff --git a/prompt-service/src/unstract/prompt_service/config.py b/prompt-service/src/unstract/prompt_service/config.py
index 5ead148de..e61f8138b 100644
--- a/prompt-service/src/unstract/prompt_service/config.py
+++ b/prompt-service/src/unstract/prompt_service/config.py
@@ -4,7 +4,7 @@ from os import environ as env
from dotenv import load_dotenv
from flask import Flask
-from peewee import PostgresqlDatabase
+from playhouse.pool import PooledPostgresqlDatabase
from unstract.prompt_service.constants import LogLevel
load_dotenv()
@@ -28,7 +28,7 @@ dictConfig(
}
)
-db = PostgresqlDatabase(None)
+db = PooledPostgresqlDatabase(None)
def get_env_or_die(env_key: str) -> str:
@@ -54,6 +54,9 @@ def create_app() -> Flask:
db_user = get_env_or_die("PG_BE_USERNAME")
db_pass = get_env_or_die("PG_BE_PASSWORD")
db_name = get_env_or_die("PG_BE_DATABASE")
+ max_connections = int(get_env_or_die("POOL_MAX_CONNECTIONS"))
+ stale_timeout = int(get_env_or_die("POOL_STALE_TIMEOUT"))
+ timeout = int(get_env_or_die("POOL_TIMEOUT"))
# Initialize and connect to the database
db.init(
@@ -62,6 +65,9 @@ def create_app() -> Flask:
password=db_pass,
host=db_host,
port=db_port,
+ max_connections=max_connections,
+ stale_timeout=stale_timeout,
+ timeout=timeout,
)
db.connect()