Skip to content

Commit d4a6b81

Browse files
committed
Merge URL checks into single check
1 parent ff5d497 commit d4a6b81

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

src/stac_auth_proxy/app.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ def create_app(settings: Optional[Settings] = None) -> FastAPI:
3131
#
3232
# Application
3333
#
34-
upstream_urls = [
35-
settings.upstream_url,
36-
settings.oidc_discovery_internal_url or settings.oidc_discovery_url,
37-
]
3834
lifespan = LifespanManager(
3935
on_startup=(
40-
[ServerHealthCheck(url=url) for url in upstream_urls]
36+
[
37+
ServerHealthCheck(
38+
urls=[settings.upstream_url, settings.oidc_discovery_internal_url]
39+
)
40+
]
4141
if settings.wait_for_upstream
4242
else []
4343
)

src/stac_auth_proxy/lifespan/ServerHealthCheck.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,42 @@
1414
class ServerHealthCheck:
1515
"""Health check for upstream API."""
1616

17-
url: str | HttpUrl
17+
urls: list[str | HttpUrl]
1818
max_retries: int = 5
19-
retry_delay: float = 0.25
19+
retry_delay: float = 0.5
2020
retry_delay_max: float = 10.0
2121
timeout: float = 5.0
2222

2323
def __post_init__(self):
2424
"""Convert url to string if it's a HttpUrl."""
25-
if isinstance(self.url, HttpUrl):
26-
self.url = str(self.url)
25+
self.urls = [str(url) if isinstance(url, HttpUrl) else url for url in self.urls]
2726

28-
async def _check_health(self) -> bool:
27+
async def _check_health(self, url: str) -> bool:
2928
"""Check if upstream API is responding."""
30-
try:
31-
async with httpx.AsyncClient() as client:
32-
response = await client.get(
33-
self.url, timeout=self.timeout, follow_redirects=True
34-
)
35-
response.raise_for_status()
36-
return True
37-
except Exception as e:
38-
logger.warning(f"Upstream health check for {self.url!r} failed: {e}")
39-
return False
40-
41-
async def __call__(self) -> None:
42-
"""Wait for upstream API to become available."""
4329
for attempt in range(self.max_retries):
44-
if await self._check_health():
45-
logger.info(f"Upstream API {self.url!r} is healthy")
46-
return
30+
try:
31+
async with httpx.AsyncClient() as client:
32+
response = await client.get(
33+
url, timeout=self.timeout, follow_redirects=True
34+
)
35+
response.raise_for_status()
36+
logger.info(f"Upstream API {url!r} is healthy")
37+
return True
38+
except Exception as e:
39+
logger.warning(f"Upstream health check for {url!r} failed: {e}")
4740

4841
retry_in = min(self.retry_delay * (2**attempt), self.retry_delay_max)
4942
logger.warning(
50-
f"Upstream API {self.url!r} not healthy, retrying in {retry_in:.1f}s "
43+
f"Upstream API {url!r} not healthy, retrying in {retry_in:.1f}s "
5144
f"(attempt {attempt + 1}/{self.max_retries})"
5245
)
5346
await asyncio.sleep(retry_in)
5447

5548
raise RuntimeError(
56-
f"Upstream API {self.url!r} failed to respond after {self.max_retries} attempts"
49+
f"Upstream API {url!r} failed to respond after {self.max_retries} attempts"
5750
)
51+
52+
async def __call__(self) -> None:
53+
"""Wait for upstream API to become available."""
54+
for url in self.urls:
55+
await self._check_health(url)

0 commit comments

Comments
 (0)