LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
fix/Connection Pool for Platform and Prompt service (#670)
harini-venkataraman37378ab41182
diff --git a/platform-service/sample.env b/platform-service/sample.envindex 0e533af8c..666835055 100644--- a/platform-service/sample.env+++ b/platform-service/sample.env@@ -26,3 +26,10 @@ FLIPT_SERVICE_AVAILABLE=FalseMODEL_PRICES_URL="https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json"MODEL_PRICES_TTL_IN_DAYS=7MODEL_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=300diff --git a/platform-service/src/unstract/platform_service/controller/platform.py b/platform-service/src/unstract/platform_service/controller/platform.pyindex 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 uuidfrom datetime import datetimefrom typing import Any, Optional-import peeweeimport redisfrom cryptography.fernet import Fernet, InvalidTokenfrom flask import Blueprint, Requestfrom flask import current_app as appfrom flask import jsonify, make_response, request+from playhouse.pool import PooledPostgresqlDatabasefrom unstract.platform_service.constants import DBTable, DBTableV2, FeatureFlagfrom unstract.platform_service.env import Envfrom unstract.platform_service.exceptions import APIError@@ -20,12 +20,15 @@ from unstract.platform_service.helper.prompt_studio import PromptStudioRequestHefrom 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 Falsediff --git a/platform-service/src/unstract/platform_service/env.py b/platform-service/src/unstract/platform_service/env.pyindex 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.pyindex 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 PooledPostgresqlDatabasefrom unstract.platform_service.constants import DBTableV2, FeatureFlagfrom unstract.platform_service.exceptions import APIError@@ -10,7 +10,7 @@ from unstract.flags.feature_flag import check_feature_flag_statusclass AdapterInstanceRequestHelper:@staticmethoddef 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 Poolorganization_id (str): organization schema idadapter_instance_id (str): adapter instance iddiff --git a/platform-service/src/unstract/platform_service/helper/prompt_studio.py b/platform-service/src/unstract/platform_service/helper/prompt_studio.pyindex 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 PooledPostgresqlDatabasefrom unstract.platform_service.constants import DBTableV2, FeatureFlagfrom unstract.platform_service.exceptions import APIError@@ -10,14 +10,14 @@ from unstract.flags.feature_flag import check_feature_flag_statusclass PromptStudioRequestHelper:@staticmethoddef 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 Poolorganization_id (str): organization schema idprompt_registry_id (str): prompt_registry_iddiff --git a/prompt-service/sample.env b/prompt-service/sample.envindex 53f27dad2..752394286 100644--- a/prompt-service/sample.env+++ b/prompt-service/sample.env@@ -27,3 +27,10 @@ EVALUATION_SERVER_PORT=9000PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python# Flipt ServiceFLIPT_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=300diff --git a/prompt-service/src/unstract/prompt_service/authentication_middleware.py b/prompt-service/src/unstract/prompt_service/authentication_middleware.pyindex 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.pyindex 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 envfrom dotenv import load_dotenvfrom flask import Flask-from peewee import PostgresqlDatabase+from playhouse.pool import PooledPostgresqlDatabasefrom unstract.prompt_service.constants import LogLevelload_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 databasedb.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()