Skip to content

🎨Director-v0: set default timeout to 20s and potentially allow setting it up #7460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import datetime

import httpx
from fastapi import FastAPI


def setup_client_session(app: FastAPI, *, max_keepalive_connections: int = 20) -> None:
def setup_client_session(
app: FastAPI,
*,
default_timeout: datetime.timedelta = datetime.timedelta(seconds=20),
max_keepalive_connections: int = 20
) -> None:
async def on_startup() -> None:
session = httpx.AsyncClient(
transport=httpx.AsyncHTTPTransport(http2=True),
limits=httpx.Limits(max_keepalive_connections=max_keepalive_connections),
timeout=default_timeout.total_seconds(),
)
app.state.aiohttp_client_session = session

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def create_app(settings: ApplicationSettings) -> FastAPI:
setup_client_session(
app,
max_keepalive_connections=settings.DIRECTOR_REGISTRY_CLIENT_MAX_KEEPALIVE_CONNECTIONS,
default_timeout=settings.DIRECTOR_REGISTRY_CLIENT_TIMEOUT,
)
setup_registry(app)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,21 @@ class ApplicationSettings(BaseApplicationSettings, MixinLoggingSettings):
),
)

DIRECTOR_REGISTRY_CLIENT_MAX_KEEPALIVE_CONNECTIONS: NonNegativeInt = 0
DIRECTOR_REGISTRY_CLIENT_MAX_KEEPALIVE_CONNECTIONS: NonNegativeInt = 5
DIRECTOR_REGISTRY_CLIENT_TIMEOUT: datetime.timedelta = datetime.timedelta(
seconds=20
)
DIRECTOR_REGISTRY_CLIENT_MAX_CONCURRENT_CALLS: PositiveInt = 20
DIRECTOR_REGISTRY_CLIENT_MAX_NUMBER_OF_RETRIEVED_OBJECTS: PositiveInt = 30

@field_validator("DIRECTOR_REGISTRY_CLIENT_TIMEOUT")
@classmethod
def _check_positive(cls, value: datetime.timedelta) -> datetime.timedelta:
if value.total_seconds() < 0:
msg = "DIRECTOR_REGISTRY_CLIENT_TIMEOUT must be positive"
raise ValueError(msg)
return value

@field_validator("DIRECTOR_GENERIC_RESOURCE_PLACEMENT_CONSTRAINTS_SUBSTITUTIONS")
@classmethod
def _validate_substitutions(cls, v):
Expand Down
13 changes: 13 additions & 0 deletions services/director/tests/unit/test_core_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
# pylint: disable=too-many-arguments


import datetime

import pytest
from pydantic import ValidationError
from pytest_simcore.helpers.monkeypatch_envs import (
setenvs_from_dict,
setenvs_from_envfile,
Expand Down Expand Up @@ -37,6 +40,16 @@ def test_valid_web_application_settings(app_environment: EnvVarsDict):
)


def test_invalid_client_timeout_raises(
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setenv(
"DIRECTOR_REGISTRY_CLIENT_TIMEOUT", f"{datetime.timedelta(seconds=-10)}"
)
with pytest.raises(ValidationError):
ApplicationSettings.create_from_envs()


def test_docker_container_env_sample(monkeypatch: pytest.MonkeyPatch):
monkeypatch.delenv("DIRECTOR_DEFAULT_MAX_MEMORY", raising=False)

Expand Down
Loading