get db pool sizes from config

Signed-off-by: phernandez <paul@basicmachines.co>
This commit is contained in:
phernandez
2025-11-28 16:50:57 -06:00
parent 704338edcf
commit ed894fc3ed
2 changed files with 28 additions and 3 deletions
+17
View File
@@ -100,6 +100,23 @@ class BasicMemoryConfig(BaseSettings):
description="Database connection URL. For Postgres, use postgresql+asyncpg://user:pass@host:port/db. If not set, SQLite will use default path.",
)
# Database connection pool configuration (Postgres only)
db_pool_size: int = Field(
default=20,
description="Number of connections to keep in the pool (Postgres only)",
gt=0,
)
db_pool_overflow: int = Field(
default=40,
description="Max additional connections beyond pool_size under load (Postgres only)",
gt=0,
)
db_pool_recycle: int = Field(
default=3600,
description="Recycle connections after N seconds to prevent stale connections (Postgres only)",
gt=0,
)
# Watch service configuration
sync_delay: int = Field(
default=1000, description="Milliseconds to wait after changes before syncing", gt=0
+11 -3
View File
@@ -190,20 +190,28 @@ def _create_sqlite_engine(db_url: str, db_type: DatabaseType) -> AsyncEngine:
return engine
def _create_postgres_engine(db_url: str) -> AsyncEngine:
def _create_postgres_engine(db_url: str, config: BasicMemoryConfig) -> AsyncEngine:
"""Create Postgres async engine with appropriate configuration.
Args:
db_url: Postgres connection URL (postgresql+asyncpg://...)
config: BasicMemoryConfig with pool settings
Returns:
Configured async engine for Postgres
"""
# Postgres with asyncpg - use standard async connection
# Postgres with asyncpg - pool sized for concurrent operations
engine = create_async_engine(
db_url,
echo=False,
pool_pre_ping=True, # Verify connections before using them
pool_size=config.db_pool_size,
max_overflow=config.db_pool_overflow,
pool_recycle=config.db_pool_recycle,
)
logger.debug(
f"Created Postgres engine with pool_size={config.db_pool_size}, "
f"max_overflow={config.db_pool_overflow}, pool_recycle={config.db_pool_recycle}"
)
return engine
@@ -228,7 +236,7 @@ def _create_engine_and_session(
# Delegate to backend-specific engine creation
# Check explicit POSTGRES type first, then config setting
if db_type == DatabaseType.POSTGRES or config.database_backend == DatabaseBackend.POSTGRES:
engine = _create_postgres_engine(db_url)
engine = _create_postgres_engine(db_url, config)
else:
engine = _create_sqlite_engine(db_url, db_type)