:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(dub): read a retry-after reset instant instead of waiting decades (#121970)
Tom Owers0af2746e7889
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.pyindex 5cb6aefb6d26..edeac354f1cc 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py@@ -211,6 +211,20 @@ def _seconds_from_epoch_reset(value: str) -> Optional[float]:return reset_epoch - datetime.now(UTC).timestamp()+# Dub puts the UNIX epoch in milliseconds at which its window resets into ``Retry-After``, not a+# delay. Read as seconds that is a wait of decades, so every 429 failed without a retry. A value+# that, read as epoch seconds or milliseconds, lands this close to now is taken as that instant.+_RETRY_AFTER_EPOCH_WINDOW_SECONDS = 24 * 60 * 60+++def _seconds_until_retry_after_epoch(value: float) -> Optional[float]:+ now = datetime.now(UTC).timestamp()+ for reset_epoch in (value, value / 1000):+ if abs(reset_epoch - now) <= _RETRY_AFTER_EPOCH_WINDOW_SECONDS:+ return reset_epoch - now+ return None++def _seconds_from_rfc3339_reset(value: str) -> Optional[float]:try:reset_at = datetime.fromisoformat(value.strip().replace("Z", "+00:00"))@@ -267,13 +281,17 @@ def _parse_retry_after(response: Response) -> Optional[float]:retry_after_header = response.headers.get("Retry-After")if retry_after_header:try:- return max(0.0, float(retry_after_header))+ delay = float(retry_after_header)except ValueError:try:dt = parsedate_to_datetime(retry_after_header)except (TypeError, ValueError):return Nonereturn max(0.0, (dt - datetime.now(UTC)).total_seconds())+ epoch_wait = _seconds_until_retry_after_epoch(delay)+ if epoch_wait is not None:+ return epoch_wait if epoch_wait > 0 else None+ return max(0.0, delay)for header, parse in _RATE_LIMIT_RESET_HEADERS:value = response.headers.get(header)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.pyindex 7d073068a0b2..9e5e1c55dd69 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py@@ -855,6 +855,25 @@ class TestRESTClient:assert _parse_retry_after(response) == 120+ @parameterized.expand(+ [+ ("epoch_milliseconds", 1_772_798_430_000, 30.0),+ ("epoch_seconds", 1_772_798_430, 30.0),+ ("elapsed_epoch_milliseconds", 1_772_798_390_000, None),+ ("plain_delay", 45, 45.0),+ ]+ )+ @patch("products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client.datetime")+ def test_parse_retry_after_reads_a_reset_instant_in_retry_after(+ self, _name: str, header_value: int, expected: float | None, mock_datetime+ ) -> None:+ mock_datetime.now.return_value = datetime(2026, 3, 6, 12, 0, 0, tzinfo=UTC)++ response = _make_response({"error": "rate limited"}, status_code=429)+ response.headers["Retry-After"] = str(header_value)++ assert _parse_retry_after(response) == expected+@patch("products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client.datetime")def test_parse_retry_after_ignores_already_elapsed_sentry_reset(self, mock_datetime) -> None:now = datetime(2026, 3, 6, 12, 0, 0, tzinfo=UTC)