: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(notion): honor full Retry-After on rate limits (#63032)
Tom Owers17fa4c31b7f5
diff --git a/posthog/temporal/data_imports/sources/notion/notion.py b/posthog/temporal/data_imports/sources/notion/notion.pyindex ff32a3cfa0bb..4451e3c9ddbb 100644--- a/posthog/temporal/data_imports/sources/notion/notion.py+++ b/posthog/temporal/data_imports/sources/notion/notion.py@@ -33,6 +33,13 @@ MAX_BLOCK_DEPTH = 2MAX_CHILD_PAGES_PER_PARENT = 50MAX_RETRY_WAIT_SECONDS = 30.0+# Notion can ask us to back off for several minutes via Retry-After under sustained load (values+# of 5+ minutes are common). Honor that instruction up to this bound instead of retrying early and+# getting throttled again — retrying inside the penalty window just burns attempts and can extend+# the penalty. Blocking this long is safe: the import activity has a week-long timeout and+# heartbeats on an independent timer, so a waiting request won't trip the heartbeat. The bound is a+# backstop against a pathologically large Retry-After.+MAX_RETRY_AFTER_SECONDS = 600.0class NotionRetryableError(Exception):@@ -76,7 +83,7 @@ def _wait_strategy(retry_state: RetryCallState) -> float:# Honor Notion's Retry-After on 429s; fall back to exponential backoff otherwise.exc = retry_state.outcome.exception() if retry_state.outcome is not None else Noneif isinstance(exc, NotionRetryableError) and exc.retry_after is not None:- return min(exc.retry_after, MAX_RETRY_WAIT_SECONDS)+ return min(exc.retry_after, MAX_RETRY_AFTER_SECONDS)return _wait_exponential(retry_state)diff --git a/posthog/temporal/data_imports/sources/notion/tests/test_notion.py b/posthog/temporal/data_imports/sources/notion/tests/test_notion.pyindex d75882cb875c..6f559450ee7e 100644--- a/posthog/temporal/data_imports/sources/notion/tests/test_notion.py+++ b/posthog/temporal/data_imports/sources/notion/tests/test_notion.py@@ -11,7 +11,7 @@ from tenacity import RetryCallStatefrom posthog.temporal.data_imports.sources.notion.notion import (MAX_BLOCK_DEPTH,MAX_CHILD_PAGES_PER_PARENT,- MAX_RETRY_WAIT_SECONDS,+ MAX_RETRY_AFTER_SECONDS,NOTION_VERSION,NotionResumeConfig,NotionRetryableError,@@ -214,9 +214,16 @@ class TestNotion:state = _FakeRetryState(NotionRetryableError("rate limited", retry_after=3.0))assert _wait_strategy(cast(RetryCallState, state)) == 3.0+ def test_wait_strategy_honors_multi_minute_retry_after(self) -> None:+ # Notion routinely asks for several minutes under sustained load. Clamping that to the+ # exponential ceiling retried inside the penalty window and exhausted attempts, so the+ # full Retry-After must be honored.+ state = _FakeRetryState(NotionRetryableError("rate limited", retry_after=336.0))+ assert _wait_strategy(cast(RetryCallState, state)) == 336.0+def test_wait_strategy_caps_retry_after(self) -> None:state = _FakeRetryState(NotionRetryableError("rate limited", retry_after=10_000.0))- assert _wait_strategy(cast(RetryCallState, state)) == MAX_RETRY_WAIT_SECONDS+ assert _wait_strategy(cast(RetryCallState, state)) == MAX_RETRY_AFTER_SECONDSdef test_comments_stream_respects_page_cap(self) -> None:# First call is the page search (one page, then done); every subsequent /v1/comments