From 6fa6f1c056419263fd575968581e619b3f917923 Mon Sep 17 00:00:00 2001 From: Artur Shiriev Date: Wed, 28 Oct 2026 16:56:43 +0300 Subject: [PATCH] perf: await the framework health check directly when there are no probes With no health_checks_probes, the default, asyncio.gather wrapped the one framework check in a Task and cost two event-loop iterations per health request. Fixes #337. --- .../instruments/healthchecks_instrument.py | 5 ++++- tests/instruments/test_healthchecks_instrument.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lite_bootstrap/instruments/healthchecks_instrument.py b/lite_bootstrap/instruments/healthchecks_instrument.py index b8837ac..fd6c178 100644 --- a/lite_bootstrap/instruments/healthchecks_instrument.py +++ b/lite_bootstrap/instruments/healthchecks_instrument.py @@ -53,9 +53,12 @@ def render_health_check_data(self, *, health_status: bool = True) -> HealthCheck return health_check_data async def define_health_status(self) -> bool: + probes = self.bootstrap_config.health_checks_probes + if not probes: + return await self._define_framework_health_status() results = await asyncio.gather( self._define_framework_health_status(), - *(self._run_probe(name, probe) for name, probe in self.bootstrap_config.health_checks_probes.items()), + *(self._run_probe(name, probe) for name, probe in probes.items()), ) return all(results) diff --git a/tests/instruments/test_healthchecks_instrument.py b/tests/instruments/test_healthchecks_instrument.py index 178834b..b1a29ec 100644 --- a/tests/instruments/test_healthchecks_instrument.py +++ b/tests/instruments/test_healthchecks_instrument.py @@ -1,6 +1,10 @@ import asyncio import time +from unittest.mock import MagicMock +import pytest + +from lite_bootstrap.instruments import healthchecks_instrument from lite_bootstrap.instruments.healthchecks_instrument import HealthChecksConfig, HealthChecksInstrument @@ -72,3 +76,11 @@ async def test_healthchecks_status_is_healthy_without_probes() -> None: def test_healthchecks_render_data_unhealthy() -> None: instrument = HealthChecksInstrument(bootstrap_config=HealthChecksConfig()) assert instrument.render_health_check_data(health_status=False)["health_status"] is False + + +async def test_healthchecks_status_without_probes_skips_gather(monkeypatch: pytest.MonkeyPatch) -> None: + gather = MagicMock(side_effect=asyncio.gather) + monkeypatch.setattr(healthchecks_instrument.asyncio, "gather", gather) + instrument = HealthChecksInstrument(bootstrap_config=HealthChecksConfig()) + assert await instrument.define_health_status() + gather.assert_not_called()