Skip to content

Commit

Permalink
Update uvicorn server configuration and add pool config (#2052)
Browse files Browse the repository at this point in the history
The Makefile has been updated to add support for specifying the number
of workers for the uvicorn server. The pyproject.toml file has also been
updated to upgrade the uvicorn dependency to version 0.30.0.
Additionally, the DatabaseService class in service.py has been
refactored to use the pool_size and max_overflow settings from the
SettingsService. This change allows for better control over the number
of connections in the connection pool and the number of connections that
can be opened beyond the pool size. The create_engine function has also
been modified to pass the pool_size and max_overflow parameters.
  • Loading branch information
ogabrielluiz committed Jun 3, 2024
2 parents 5fe568c + 5ea2699 commit d2f1448
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 24 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ port ?= 7860
env ?= .env
open_browser ?= true
path = src/backend/base/langflow/frontend
workers ?= 1

codespell:
@poetry install --with spelling
Expand Down Expand Up @@ -144,10 +145,10 @@ backend:
@-kill -9 $(lsof -t -i:7860)
ifdef login
@echo "Running backend autologin is $(login)";
LANGFLOW_AUTO_LOGIN=$(login) poetry run uvicorn --factory langflow.main:create_app --host 0.0.0.0 --port 7860 --reload --env-file .env --loop asyncio
LANGFLOW_AUTO_LOGIN=$(login) poetry run uvicorn --factory langflow.main:create_app --host 0.0.0.0 --port 7860 --reload --env-file .env --loop asyncio --workers $(workers)
else
@echo "Running backend respecting the .env file";
poetry run uvicorn --factory langflow.main:create_app --host 0.0.0.0 --port 7860 --reload --env-file .env --loop asyncio
poetry run uvicorn --factory langflow.main:create_app --host 0.0.0.0 --port 7860 --reload --env-file .env --loop asyncio --workers $(workers)
endif

build_and_run:
Expand Down
16 changes: 8 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/backend/base/langflow/components/models/OpenAIModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def build(
self,
input_value: Text,
openai_api_key: str,
temperature: Optional[float] = 0.1,
temperature: float = 0.1,
model_name: str = "gpt-4o",
max_tokens: Optional[int] = 256,
model_kwargs: NestedDict = {},
Expand Down
2 changes: 1 addition & 1 deletion src/backend/base/langflow/services/database/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def create(self, settings_service: "SettingsService"):
# Here you would have logic to create and configure a DatabaseService
if not settings_service.settings.database_url:
raise ValueError("No database URL provided")
return DatabaseService(settings_service.settings.database_url)
return DatabaseService(settings_service)
17 changes: 14 additions & 3 deletions src/backend/base/langflow/services/database/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
if TYPE_CHECKING:
from sqlalchemy.engine import Engine

from langflow.services.settings.service import SettingsService


class DatabaseService(Service):
name = "database_service"

def __init__(self, database_url: str):
self.database_url = database_url
def __init__(self, settings_service: "SettingsService"):
self.settings_service = settings_service
if settings_service.settings.database_url is None:
raise ValueError("No database URL provided")
self.database_url: str = settings_service.settings.database_url
# This file is in langflow.services.database.manager.py
# the ini is in langflow
langflow_dir = Path(__file__).parent.parent.parent
Expand All @@ -41,7 +46,12 @@ def _create_engine(self) -> "Engine":
connect_args = {"check_same_thread": False}
else:
connect_args = {}
return create_engine(self.database_url, connect_args=connect_args)
return create_engine(
self.database_url,
connect_args=connect_args,
pool_size=self.settings_service.settings.pool_size,
max_overflow=self.settings_service.settings.max_overflow,
)

def __enter__(self):
self._session = Session(self.engine)
Expand Down Expand Up @@ -267,3 +277,4 @@ def teardown(self):
logger.error(f"Error tearing down database: {exc}")

self.engine.dispose()
self.engine.dispose()
5 changes: 5 additions & 0 deletions src/backend/base/langflow/services/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class Settings(BaseSettings):

dev: bool = False
database_url: Optional[str] = None
"""Database URL for Langflow. If not provided, Langflow will use a SQLite database."""
pool_size: int = 10
"""The number of connections to keep open in the connection pool. If not provided, the default is 10."""
max_overflow: int = 10
"""The number of connections to allow that can be opened beyond the pool size. If not provided, the default is 10."""
cache_type: str = "async"
remove_api_keys: bool = False
components_path: List[str] = []
Expand Down
16 changes: 8 additions & 8 deletions src/backend/base/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/backend/base/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ langflow-base = "langflow.__main__:main"
python = ">=3.10,<3.13"
fastapi = "^0.111.0"
httpx = "*"
uvicorn = "^0.29.0"
uvicorn = "^0.30.0"
gunicorn = "^22.0.0"
langchain = "~0.2.0"
langchainhub = "~0.1.15"
Expand Down

0 comments on commit d2f1448

Please sign in to comment.