: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(clickhouse): retry connect-time HTTP 429 rate limits in-process (#71744)
Tom Owers903205095597
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.pyindex 5a10f97dc5bc..04a81aacea1a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py@@ -112,6 +112,16 @@ _TRANSIENT_CONNECT_DROP_SUBSTRINGS = ("Tunnel connection failed: 502","Tunnel connection failed: 503","Tunnel connection failed: 504",+ # The ClickHouse host (or a proxy/gateway in front of it) rate-limited the+ # request with HTTP 429 ("HTTPDriver for <url> returned response code 429").+ # A 429 is a transient "back off and retry" signal, not a config error.+ # clickhouse-connect already retries 429 for queries (query_retries), but+ # the probe it runs while constructing the client passes retries=0, so a+ # 429 at connect time reaches us with no retry at all — a bounded backoff+ # retry here recovers the common transient burst. We match only 429; other+ # HTTP statuses keep their existing handling (404 is non-retryable in the+ # source, 5xx stay retryable via Temporal).+ "returned response code 429",)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.pyindex d62f2c3d08e8..0f6c2411e75c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py@@ -5,7 +5,7 @@ from posthog.test.base import BaseTestfrom unittest.mock import MagicMock, patchimport pyarrow as pa-from clickhouse_connect.driver.exceptions import ClickHouseError+from clickhouse_connect.driver.exceptions import ClickHouseError, OperationalErrorfrom products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchemafrom products.warehouse_sources.backend.models.external_data_source import ExternalDataSource@@ -687,6 +687,9 @@ class TestIsTransientConnectDrop:"502 Bad gateway'))) executing HTTP request attempt 1","Tunnel connection failed: 503 Service Unavailable","Tunnel connection failed: 504 Gateway Timeout",+ # HTTP 429 rate-limit at connect time — clickhouse-connect doesn't+ # retry the client-construction probe, so we retry it in-process.+ "HTTPDriver for https://host:8443 returned response code 429",],)def test_matches_transient_drops(self, message):@@ -725,6 +728,18 @@ class TestGetClientTransientRetry:assert self._connect() is clientassert mock_get_client.call_count == 2+ def test_retries_connect_time_429_then_succeeds(self):+ # clickhouse-connect raises OperationalError for a 429 exhausted at the+ # client-construction probe (which runs with retries=0). We retry it.+ client = MagicMock()+ rate_limited = OperationalError("HTTPDriver for https://host:8443 returned response code 429")+ with (+ patch.object(ch_module.time, "sleep"),+ patch.object(ch_module, "get_client", side_effect=[rate_limited, client]) as mock_get_client,+ ):+ assert self._connect() is client+ assert mock_get_client.call_count == 2+def test_gives_up_after_max_attempts(self):ssl_eof = ClickHouseError("[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol")with (