Skip to content

: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.

fix(clickhouse): retry transient 429 rate-limit responses on connect (#71742)

Tom Owers5b0c32e0887a
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.py
index 04a81aacea1a..52e3a08421ae 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
@@ -129,6 +129,20 @@ def _is_transient_connect_drop(error_message: str) -> bool:
return any(substring in error_message for substring in _TRANSIENT_CONNECT_DROP_SUBSTRINGS)
+# clickhouse-connect surfaces an upstream rate-limit as a full HTTP response
+# ("HTTPDriver for <url> returned response code 429"), not a dropped connection:
+# the request reached the server (or a proxy in front of it) and it told us to
+# slow down. A 429 is explicitly "retry later", so a brief backed-off re-attempt
+# often clears a short rate-limit burst; if it doesn't, the failing Temporal
+# activity stays retryable and recovers later. We match only 429 — other 4xx
+# response codes are deterministic (e.g. 404 stays non-retryable).
+_TRANSIENT_RATE_LIMIT_SUBSTRING = "returned response code 429"
+
+
+def _is_retryable_connect_error(error_message: str) -> bool:
+ return _is_transient_connect_drop(error_message) or _TRANSIENT_RATE_LIMIT_SUBSTRING in error_message
+
+
def _get_client(
*,
host: str,
@@ -173,9 +187,9 @@ def _get_client(
# the request.
attempt += 1
message = str(e)
- if attempt < _MAX_CONNECT_ATTEMPTS and _is_transient_connect_drop(message):
+ if attempt < _MAX_CONNECT_ATTEMPTS and _is_retryable_connect_error(message):
structlog.get_logger().warning(
- "Transient ClickHouse connection drop during connect; retrying",
+ "Transient ClickHouse connect error; retrying",
attempt=attempt,
max_attempts=_MAX_CONNECT_ATTEMPTS,
exc_info=e,
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.py
index 0f6c2411e75c..9cfd6e57f500 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
@@ -661,6 +661,8 @@ class TestClickHouseSourceNonRetryableErrors:
# Transient gateway errors must stay retryable — only 404 is permanent.
"HTTPDriver for https://example.ngrok-free.dev:443 returned response code 502",
"HTTPDriver for https://example.ngrok-free.dev:443 returned response code 503",
+ # 429 Too Many Requests is a rate-limit ("retry later"), not a permanent error.
+ "HTTPDriver for https://example.ngrok-free.dev:443 returned response code 429",
],
)
def test_transient_errors_are_retryable(self, source, error_msg):
@@ -750,6 +752,16 @@ class TestGetClientTransientRetry:
self._connect()
assert mock_get_client.call_count == _MAX_CONNECT_ATTEMPTS
+ def test_retries_rate_limit_then_succeeds(self):
+ 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_does_not_retry_deterministic_failure(self):
cert_error = ClickHouseError("certificate verify failed")
with (