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(shopify): honor rate-limit refill time when retrying throttled requests (#65081)

Tom Owersd0942c5fbecc
diff --git a/posthog/temporal/data_imports/sources/shopify/shopify.py b/posthog/temporal/data_imports/sources/shopify/shopify.py
index bc9e4beb7ad8..51c805532b31 100644
--- a/posthog/temporal/data_imports/sources/shopify/shopify.py
+++ b/posthog/temporal/data_imports/sources/shopify/shopify.py
@@ -7,7 +7,7 @@ from typing import Any
import requests
from structlog.types import FilteringBoundLogger
-from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
+from tenacity import RetryCallState, retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
from posthog.temporal.data_imports.pipelines.pipeline.typings import SourceResponse
from posthog.temporal.data_imports.sources.common.http import make_tracked_session
@@ -80,10 +80,47 @@ class ShopifyPermissionError(Exception):
super().__init__(message)
+# Shopify's GraphQL Admin API rate-limits on a cost-based leaky bucket, so a single bucket
+# can take this long to refill from empty (~2000 points at 100/sec on Plus). Cap the throttle
+# wait here so a malformed `restoreRate` can't stall the worker past its heartbeat.
+_SHOPIFY_MAX_THROTTLE_WAIT_SECONDS = 60.0
+
+
class ShopifyRetryableError(Exception):
- """Exception raised when Shopify issues a retryable error (e.g. rate limit, 5xx)."""
+ """Exception raised when Shopify issues a retryable error (e.g. rate limit, 5xx).
+
+ `retry_after`, when set, is the number of seconds Shopify's throttle status says we
+ should wait before the leaky bucket has refilled enough to satisfy the query again.
+ """
+
+ def __init__(self, message: str, retry_after: float | None = None):
+ super().__init__(message)
+ self.retry_after = retry_after
- pass
+
+def _throttle_retry_after(payload: Any) -> float | None:
+ """Seconds to wait for the cost bucket to refill enough for the requested query.
+
+ A throttled response carries `extensions.cost` with the points the query needed
+ (`requestedQueryCost`) and how fast the bucket refills (`throttleStatus.restoreRate`),
+ which is exactly how long to back off. Returns None when that data is missing or
+ malformed so the caller falls back to plain exponential backoff.
+ """
+ cost, ok = safe_unwrap(payload, path="extensions.cost")
+ if not ok or not isinstance(cost, dict):
+ return None
+ requested = cost.get("requestedQueryCost")
+ throttle = cost.get("throttleStatus")
+ if not isinstance(requested, int | float) or not isinstance(throttle, dict):
+ return None
+ available = throttle.get("currentlyAvailable")
+ restore_rate = throttle.get("restoreRate")
+ if not isinstance(available, int | float) or not isinstance(restore_rate, int | float) or restore_rate <= 0:
+ return None
+ deficit = requested - available
+ if deficit <= 0:
+ return None
+ return min(deficit / restore_rate, _SHOPIFY_MAX_THROTTLE_WAIT_SECONDS)
def _get_retryable_error(payload: Any) -> ShopifyRetryableError | None:
@@ -92,7 +129,7 @@ def _get_retryable_error(payload: Any) -> ShopifyRetryableError | None:
if ok:
serialized_errors = json.dumps(errors).lower()
if "throttled" in serialized_errors:
- return ShopifyRetryableError("Shopify: rate limit exceeded...")
+ return ShopifyRetryableError("Shopify: rate limit exceeded...", retry_after=_throttle_retry_after(payload))
if "internal_server_error" in serialized_errors:
return ShopifyRetryableError(f"Shopify: internal errors in payload {serialized_errors}")
currently_available, ok = safe_unwrap(payload, path="extensions.cost.throttleStatus.currentlyAvailable")
@@ -100,10 +137,29 @@ def _get_retryable_error(payload: Any) -> ShopifyRetryableError | None:
# this check is a little liberal. if we find that we are getting rate limited
# too often might be worth it to check against the requestedCost instead
if currently_available <= 0:
- return ShopifyRetryableError("Shopify: rate limit exceeded...")
+ return ShopifyRetryableError("Shopify: rate limit exceeded...", retry_after=_throttle_retry_after(payload))
return None
+_shopify_backoff = wait_exponential_jitter(initial=1, max=30)
+
+
+def _shopify_retry_wait(retry_state: RetryCallState) -> float:
+ """Back off exponentially, but never for less than Shopify's reported refill time.
+
+ Plain exponential backoff tops out around 15s across the 5 attempts, which can give up
+ while the cost bucket is still draining. When a rate-limit response told us how long the
+ bucket needs (see `_throttle_retry_after`), honor that instead.
+ """
+ backoff = _shopify_backoff(retry_state)
+ outcome = retry_state.outcome
+ if outcome is not None and not outcome.cancelled():
+ exc = outcome.exception()
+ if isinstance(exc, ShopifyRetryableError) and exc.retry_after is not None:
+ return max(backoff, exc.retry_after)
+ return backoff
+
+
def _make_paginated_shopify_request(
url: str,
sess: requests.Session,
@@ -119,7 +175,7 @@ def _make_paginated_shopify_request(
@retry(
retry=retry_if_exception_type(ShopifyRetryableError),
stop=stop_after_attempt(5),
- wait=wait_exponential_jitter(initial=1, max=30),
+ wait=_shopify_retry_wait,
reraise=True,
)
def execute(vars: dict[str, Any]):
diff --git a/posthog/temporal/data_imports/sources/shopify/tests/test_rate_limit_retry.py b/posthog/temporal/data_imports/sources/shopify/tests/test_rate_limit_retry.py
new file mode 100644
index 000000000000..18f5803016c6
--- /dev/null
+++ b/posthog/temporal/data_imports/sources/shopify/tests/test_rate_limit_retry.py
@@ -0,0 +1,83 @@
+import pytest
+
+from tenacity import Future, RetryCallState, Retrying
+
+from posthog.temporal.data_imports.sources.shopify.shopify import (
+ _SHOPIFY_MAX_THROTTLE_WAIT_SECONDS,
+ ShopifyRetryableError,
+ _get_retryable_error,
+ _shopify_retry_wait,
+ _throttle_retry_after,
+)
+
+
+def _throttled_payload(requested: float, available: float, restore_rate: float) -> dict:
+ return {
+ "errors": [{"message": "Throttled", "extensions": {"code": "THROTTLED"}}],
+ "extensions": {
+ "cost": {
+ "requestedQueryCost": requested,
+ "throttleStatus": {
+ "maximumAvailable": 2000,
+ "currentlyAvailable": available,
+ "restoreRate": restore_rate,
+ },
+ }
+ },
+ }
+
+
+def _retry_state(exc: Exception) -> RetryCallState:
+ state = RetryCallState(retry_object=Retrying(), fn=None, args=(), kwargs={})
+ state.outcome = Future.construct(1, exc, True)
+ return state
+
+
+def test_throttle_retry_after_computes_refill_time():
+ # deficit 90 points at 50/sec => 1.8s
+ assert _throttle_retry_after(_throttled_payload(100, 10, 50)) == pytest.approx(1.8)
+
+
+def test_throttle_retry_after_is_capped():
+ # An enormous deficit (or tiny restore rate) must not stall the worker indefinitely.
+ assert _throttle_retry_after(_throttled_payload(10_000, 0, 1)) == _SHOPIFY_MAX_THROTTLE_WAIT_SECONDS
+
+
+ "payload",
+ [
+ {},
+ {"extensions": {}},
+ {"extensions": {"cost": {"requestedQueryCost": 100}}},
+ {"extensions": {"cost": {"requestedQueryCost": 100, "throttleStatus": {"currentlyAvailable": 10}}}},
+ # restoreRate of 0 would divide by zero
+ _throttled_payload(100, 10, 0),
+ # bucket already has enough headroom
+ _throttled_payload(100, 200, 50),
+ ],
+)
+def test_throttle_retry_after_returns_none_when_unavailable(payload):
+ assert _throttle_retry_after(payload) is None
+
+
+def test_get_retryable_error_attaches_retry_after():
+ error = _get_retryable_error(_throttled_payload(100, 10, 50))
+ assert isinstance(error, ShopifyRetryableError)
+ assert error.retry_after == pytest.approx(1.8)
+
+
+def test_get_retryable_error_throttled_without_cost_has_no_retry_after():
+ error = _get_retryable_error({"errors": [{"message": "Throttled"}]})
+ assert isinstance(error, ShopifyRetryableError)
+ assert error.retry_after is None
+
+
+def test_retry_wait_honors_throttle_refill_time():
+ # The exponential floor is ~1s on the first attempt; Shopify's refill time must win.
+ wait = _shopify_retry_wait(_retry_state(ShopifyRetryableError("rate limit", retry_after=20.0)))
+ assert wait >= 20.0
+
+
+def test_retry_wait_falls_back_to_backoff_without_retry_after():
+ wait = _shopify_retry_wait(_retry_state(ShopifyRetryableError("internal error")))
+ assert 1.0 <= wait <= 2.0 # initial=1, max=30, jitter up to 1s on attempt 1