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(recharge): request smaller payment_methods pages to avoid read timeouts (#62132)

Tom Owers92304f594b82
diff --git a/posthog/temporal/data_imports/sources/recharge/recharge.py b/posthog/temporal/data_imports/sources/recharge/recharge.py
index 9c0673d74679..655e1c96ba85 100644
--- a/posthog/temporal/data_imports/sources/recharge/recharge.py
+++ b/posthog/temporal/data_imports/sources/recharge/recharge.py
@@ -32,9 +32,8 @@ from posthog.temporal.data_imports.sources.recharge.settings import RECHARGE_END
RECHARGE_BASE_URL = "https://api.rechargeapps.com"
RECHARGE_API_VERSION = "2021-11"
-# Recharge caps page size at 250 (default 50). Larger pages = fewer round-trips
-# against the leaky-bucket rate limit (40 burst, ~2 req/s sustained).
-PAGE_SIZE = 250
+# Per-endpoint page size lives on each `RechargeEndpointConfig` (default 250,
+# Recharge's max); see `settings.py` for why some endpoints request less.
REQUEST_TIMEOUT_SECONDS = 60
@@ -86,7 +85,7 @@ def _build_initial_params(
incremental_field: str | None,
) -> dict[str, Any]:
"""Query params for the first page (cursor pages drop everything but limit)."""
- params: dict[str, Any] = {"limit": PAGE_SIZE}
+ params: dict[str, Any] = {"limit": config.page_size}
use_incremental = (
config.supports_incremental
@@ -202,7 +201,7 @@ def get_rows(
# When following a cursor, Recharge only accepts `cursor` + `limit` —
# the original sort/filters are baked into the cursor itself. Sending
# them again returns a 422.
- request_params: dict[str, Any] = {"cursor": cursor, "limit": PAGE_SIZE} if cursor else params
+ request_params: dict[str, Any] = {"cursor": cursor, "limit": config.page_size} if cursor else params
data = fetch_page(request_params)
items = _extract_items(data, endpoint)
diff --git a/posthog/temporal/data_imports/sources/recharge/settings.py b/posthog/temporal/data_imports/sources/recharge/settings.py
index 8ef7ac7cd617..39f108d7653b 100644
--- a/posthog/temporal/data_imports/sources/recharge/settings.py
+++ b/posthog/temporal/data_imports/sources/recharge/settings.py
@@ -3,6 +3,16 @@ from typing import Optional
from products.data_warehouse.backend.types import IncrementalField, IncrementalFieldType
+# Recharge caps page size at 250 (default 50). Larger pages mean fewer
+# round-trips against the leaky-bucket rate limit (40 burst, ~2 req/s sustained).
+DEFAULT_PAGE_SIZE = 250
+
+# `payment_methods` is an expensive endpoint: a full 250-record page consistently
+# exceeds the 60s read timeout (the server can't generate the page in time),
+# surfacing as upstream read timeouts. Request smaller pages so each response
+# returns well within the timeout.
+PAYMENT_METHODS_PAGE_SIZE = 50
+
@dataclass
class RechargeEndpointConfig:
@@ -33,6 +43,9 @@ class RechargeEndpointConfig:
# `<field>-asc` / `<field>-desc`; `id-asc` is a stable monotonic order that
# avoids page-boundary skips/dupes when rows are inserted mid-sync.
default_sort_field: str = "id"
+ # Records requested per page. Capped at 250 by Recharge; lower it for
+ # endpoints whose pages are too slow to generate within the read timeout.
+ page_size: int = DEFAULT_PAGE_SIZE
def _timestamp_field(name: str) -> IncrementalField:
@@ -100,6 +113,7 @@ RECHARGE_ENDPOINTS: dict[str, RechargeEndpointConfig] = {
name="payment_methods",
path="/payment_methods",
incremental_fields=_UPDATED_AND_CREATED,
+ page_size=PAYMENT_METHODS_PAGE_SIZE,
),
# Collections expose `created_at`/`updated_at` on the object but the list
# endpoint's server-side timestamp filtering is not documented as reliably
diff --git a/posthog/temporal/data_imports/sources/recharge/tests/test_recharge.py b/posthog/temporal/data_imports/sources/recharge/tests/test_recharge.py
index 413ed47ed699..1aa5d214c84c 100644
--- a/posthog/temporal/data_imports/sources/recharge/tests/test_recharge.py
+++ b/posthog/temporal/data_imports/sources/recharge/tests/test_recharge.py
@@ -77,6 +77,23 @@ class TestBuildInitialParams:
assert params["sort_by"] == "id-asc"
assert not any(k.endswith("_min") for k in params)
+ @parameterized.expand(
+ [
+ ("default_endpoint_uses_max_page_size", "customers", 250),
+ ("payment_methods_uses_smaller_page_size", "payment_methods", 50),
+ ]
+ )
+ def test_page_size_is_per_endpoint(self, _name: str, endpoint: str, expected_limit: int) -> None:
+ # `payment_methods` pages are too slow to generate within the read
+ # timeout at the 250 max, so it requests smaller pages.
+ params = _build_initial_params(
+ RECHARGE_ENDPOINTS[endpoint],
+ should_use_incremental_field=False,
+ db_incremental_field_last_value=None,
+ incremental_field=None,
+ )
+ assert params["limit"] == expected_limit
+
@parameterized.expand(
[
# `collections` is full-refresh but still sortable -> keeps `id-asc`.
@@ -188,6 +205,34 @@ class TestGetRows:
assert mock_session.call_args.kwargs["redact_values"] == ("secret-token",)
+ @parameterized.expand(
+ [
+ ("payment_methods_uses_smaller_limit", "payment_methods", 50),
+ ("customers_use_default_limit", "customers", 250),
+ ]
+ )
+ @patch("posthog.temporal.data_imports.sources.recharge.recharge.make_tracked_session")
+ def test_cursor_pages_use_per_endpoint_limit(
+ self, _name: str, endpoint: str, expected_limit: int, mock_session: MagicMock
+ ) -> None:
+ # Both the first page and cursor pages must carry the per-endpoint limit;
+ # for `payment_methods` that's 50 (to avoid the 60s read timeout), for
+ # other endpoints it's the 250 max — otherwise the second page would time
+ # out at the wrong size again.
+ mock_session.return_value.get.side_effect = [
+ _mock_response(json_body={endpoint: [{"id": 1}], "next_cursor": "cursor-2"}),
+ _mock_response(json_body={endpoint: [{"id": 2}], "next_cursor": None}),
+ ]
+ manager = MagicMock()
+ manager.can_resume.return_value = False
+
+ list(get_rows(api_key="t", endpoint=endpoint, logger=MagicMock(), resumable_source_manager=manager))
+
+ first_url, second_url = (call.args[0] for call in mock_session.return_value.get.call_args_list)
+ assert f"limit={expected_limit}" in first_url
+ assert "cursor=cursor-2" in second_url
+ assert f"limit={expected_limit}" in second_url
+
@patch("posthog.temporal.data_imports.sources.recharge.recharge.make_tracked_session")
def test_products_request_omits_sort_by(self, mock_session: MagicMock) -> None:
# Regression: the 2021-11 `/products` endpoint 422s on `sort_by`, so the