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:
parent
e53b1e120a
commit
2f3d2dc480
2 changed files with 15 additions and 0 deletions
|
|
@ -3,6 +3,7 @@ from datetime import datetime, timedelta
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import logging.config
|
import logging.config
|
||||||
|
import time
|
||||||
from typing import Annotated, AsyncGenerator, Optional
|
from typing import Annotated, AsyncGenerator, Optional
|
||||||
from api.auth import get_current_user
|
from api.auth import get_current_user
|
||||||
from api.config import DEV_TIER_ORIGINS, PROD_TIER_ORIGINS, APP_ENV
|
from api.config import DEV_TIER_ORIGINS, PROD_TIER_ORIGINS, APP_ENV
|
||||||
|
|
@ -456,10 +457,15 @@ async def stream_listing_geojson(
|
||||||
else:
|
else:
|
||||||
limit = _rate_limit_config.geojson_stream_limit_cap
|
limit = _rate_limit_config.geojson_stream_limit_cap
|
||||||
|
|
||||||
|
timings: list[str] = []
|
||||||
|
|
||||||
# Build POI distances lookup if requested
|
# 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
|
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)
|
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:
|
if cached_count is not None and cached_count > 0 and not include_poi_distances:
|
||||||
app_metrics.geojson_cache_operations.add(1, {"result": "hit"})
|
app_metrics.geojson_cache_operations.add(1, {"result": "hit"})
|
||||||
generator = _stream_from_cache(
|
generator = _stream_from_cache(
|
||||||
|
|
@ -482,6 +488,7 @@ async def stream_listing_geojson(
|
||||||
headers={
|
headers={
|
||||||
"Cache-Control": "no-cache",
|
"Cache-Control": "no-cache",
|
||||||
"X-Accel-Buffering": "no", # Disable nginx buffering
|
"X-Accel-Buffering": "no", # Disable nginx buffering
|
||||||
|
"Server-Timing": ", ".join(timings),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -355,4 +355,12 @@ class TestStreamingEndpoint:
|
||||||
assert 2 not in feature_ids
|
assert 2 not in feature_ids
|
||||||
assert 1 in feature_ids
|
assert 1 in feature_ids
|
||||||
|
|
||||||
|
def test_streaming_includes_server_timing_header(self, client, mock_repository):
|
||||||
|
"""Test that streaming response includes Server-Timing header."""
|
||||||
|
response = client.get("/api/listing_geojson/stream?listing_type=RENT&limit=10")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "server-timing" in response.headers
|
||||||
|
assert "cache_check" in response.headers["server-timing"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue