Add Server-Timing header to streaming endpoint

Reports cache_check latency in the response header, visible in
browser DevTools Network tab for ongoing performance monitoring.
This commit is contained in:
Viktor Barzin 2026-02-22 13:27:11 +00:00
parent e53b1e120a
commit 2f3d2dc480
No known key found for this signature in database
GPG key ID: 0EB088298288D958
2 changed files with 15 additions and 0 deletions

View file

@ -3,6 +3,7 @@ from datetime import datetime, timedelta
import json
import logging
import logging.config
import time
from typing import Annotated, AsyncGenerator, Optional
from api.auth import get_current_user
from api.config import DEV_TIER_ORIGINS, PROD_TIER_ORIGINS, APP_ENV
@ -456,10 +457,15 @@ async def stream_listing_geojson(
else:
limit = _rate_limit_config.geojson_stream_limit_cap
timings: list[str] = []
# Build POI distances lookup if requested
poi_distances_lookup = _build_poi_distances_lookup(user.email, query_parameters.listing_type) if include_poi_distances else None
t0 = time.monotonic()
cached_count = get_cached_count(query_parameters)
timings.append(f"cache_check;dur={(time.monotonic() - t0) * 1000:.1f}")
if cached_count is not None and cached_count > 0 and not include_poi_distances:
app_metrics.geojson_cache_operations.add(1, {"result": "hit"})
generator = _stream_from_cache(
@ -482,6 +488,7 @@ async def stream_listing_geojson(
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no", # Disable nginx buffering
"Server-Timing": ", ".join(timings),
}
)