: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(paddle): retry rate-limit and 5xx responses with backoff (#65041)
Tom Owerse8ce8ee26e56
diff --git a/posthog/temporal/data_imports/sources/paddle/paddle.py b/posthog/temporal/data_imports/sources/paddle/paddle.pyindex a80adb612ade..92ce0348d4aa 100644--- a/posthog/temporal/data_imports/sources/paddle/paddle.py+++ b/posthog/temporal/data_imports/sources/paddle/paddle.py@@ -5,11 +5,10 @@ from typing import Any, Optionalimport requestsfrom dateutil import parserfrom structlog.types import FilteringBoundLogger-from urllib3.util.retry import Retryfrom posthog.temporal.data_imports.pipelines.pipeline.batcher import Batcherfrom posthog.temporal.data_imports.pipelines.pipeline.typings import SourceResponse-from posthog.temporal.data_imports.sources.common.http import make_tracked_session+from posthog.temporal.data_imports.sources.common.http import DEFAULT_RETRY, make_tracked_sessionfrom posthog.temporal.data_imports.sources.common.resumable import ResumableSourceManagerfrom posthog.temporal.data_imports.sources.paddle.settings import ENDPOINTS, INCREMENTAL_FIELDS@@ -43,15 +42,22 @@ def _format_paddle_datetime_query_value(value: Any) -> str:return parsed.isoformat().replace("+00:00", "Z")-def _get_paddle_session() -> requests.Session:- # Plain session with no retry adapter: errors fail fast so the caller- # can surface them immediately rather than silently retrying.- return make_tracked_session(retry=Retry(total=0))+def _get_paddle_session(api_key: str) -> requests.Session:+ # DEFAULT_RETRY backs off on 429/5xx (honoring Retry-After) but leaves auth/4xx+ # failures to surface immediately, so a transient rate-limit doesn't fail the sync.+ # The bearer token is set on the session and registered with redact_values so the+ # tracked transport scrubs it from logged URLs, headers, and captured samples.+ return make_tracked_session(+ retry=DEFAULT_RETRY,+ headers={+ "Authorization": f"Bearer {api_key}",+ "Content-Type": "application/json",+ },+ redact_values=(api_key,),+ )def paddle_request(session: requests.Session, method: str, url: str, **kwargs) -> requests.Response:- # No retry adapter on the session — errors surface immediately so the- # caller (get_rows / validate_credentials) can decide how to handle them.response = session.request(method, url, **kwargs)return response@@ -64,11 +70,6 @@ def get_rows(resumable_source_manager: ResumableSourceManager[PaddleResumeConfig],should_use_incremental_field: bool = False,):- headers = {- "Authorization": f"Bearer {api_key}",- "Content-Type": "application/json",- }-url = f"{PADDLE_BASE_URL}/{endpoint}"params: dict[str, Any] = {"per_page": 200}incremental_field_config = INCREMENTAL_FIELDS.get(endpoint, [])@@ -88,7 +89,7 @@ def get_rows(params = {}batcher = Batcher(logger=logger)- session = _get_paddle_session()+ session = _get_paddle_session(api_key)seen_urls: set[str] = set()while url:@@ -96,7 +97,7 @@ def get_rows(breakseen_urls.add(url)- response = paddle_request(session, "GET", url, headers=headers, params=params)+ response = paddle_request(session, "GET", url, params=params)response.raise_for_status()data = response.json()@@ -169,15 +170,11 @@ def paddle_source(def validate_credentials(api_key: str, table_name: Optional[str] = None) -> bool:- headers = {- "Authorization": f"Bearer {api_key}",- }-endpoints_to_check = [table_name] if table_name else ENDPOINTS- session = _get_paddle_session()+ session = _get_paddle_session(api_key)for endpoint in endpoints_to_check:- response = paddle_request(session, "GET", f"{PADDLE_BASE_URL}/{endpoint}", headers=headers)+ response = paddle_request(session, "GET", f"{PADDLE_BASE_URL}/{endpoint}")if response.status_code == 403:raise PaddlePermissionError(f"Missing permissions for {endpoint}")response.raise_for_status()diff --git a/posthog/temporal/data_imports/sources/paddle/tests/test_paddle.py b/posthog/temporal/data_imports/sources/paddle/tests/test_paddle.pynew file mode 100644index 000000000000..64df358c994c--- /dev/null+++ b/posthog/temporal/data_imports/sources/paddle/tests/test_paddle.py@@ -0,0 +1,28 @@+from typing import cast++from requests.adapters import HTTPAdapter++from posthog.temporal.data_imports.sources.paddle.paddle import PADDLE_BASE_URL, _get_paddle_session+++class TestPaddleSession:+ def test_session_retries_rate_limits(self):+ session = _get_paddle_session("pdl_test_key")+ retry = cast(HTTPAdapter, session.get_adapter(PADDLE_BASE_URL)).max_retries++ # A transient 429 must back off and retry rather than failing the whole sync.+ assert retry.total is not None and retry.total > 0+ assert retry.is_retry("GET", 429) is True+ assert retry.respect_retry_after_header is True+ # Persistent failures still surface via response.raise_for_status(), not MaxRetryError.+ assert retry.raise_on_status is False++ def test_auth_failures_are_not_retried(self):+ session = _get_paddle_session("pdl_test_key")+ retry = cast(HTTPAdapter, session.get_adapter(PADDLE_BASE_URL)).max_retries++ # 401/403/400 are credential/config problems handled by get_non_retryable_errors;+ # retrying them would only delay surfacing the error to the user.+ assert retry.is_retry("GET", 401) is False+ assert retry.is_retry("GET", 403) is False+ assert retry.is_retry("GET", 400) is Falsediff --git a/posthog/temporal/tests/data_imports/test_paddle_source.py b/posthog/temporal/tests/data_imports/test_paddle_source.pyindex 5b1d00773888..1b4f979fbf1c 100644--- a/posthog/temporal/tests/data_imports/test_paddle_source.py+++ b/posthog/temporal/tests/data_imports/test_paddle_source.py@@ -184,7 +184,7 @@ def test_paddle_request_fails_fast(mock_request):from posthog.temporal.data_imports.sources.paddle.paddle import _get_paddle_session, paddle_request- session = _get_paddle_session()+ session = _get_paddle_session("fake")response = paddle_request(session,