: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(workflows): back off on ses throttling in the feedback forwarding backfill (#115519)
Meikel Ratzf9249a9463db
diff --git a/products/workflows/backend/management/commands/disable_ses_feedback_forwarding.py b/products/workflows/backend/management/commands/disable_ses_feedback_forwarding.pyindex 472298d1b92d..3fece6e76453 100644--- a/products/workflows/backend/management/commands/disable_ses_feedback_forwarding.py+++ b/products/workflows/backend/management/commands/disable_ses_feedback_forwarding.py@@ -1,3 +1,4 @@+import timeimport loggingfrom typing import Any@@ -11,6 +12,10 @@ from products.workflows.backend.providers import SESProviderlogger = logging.getLogger(__name__)+# SES rate-limits identity updates per account, and other app traffic shares that limit.+PACE_SECONDS = 0.5+THROTTLE_BACKOFF_SECONDS = [2, 4, 8, 16, 32, 64]+def ses_email_domains(domains: list[str]) -> list[str]:query = Integration.objects.filter(kind="email", config__provider="ses")@@ -50,8 +55,9 @@ class Command(BaseCommand):provider = SESProvider()failures: list[str] = []for domain in domains:+ time.sleep(PACE_SECONDS)try:- provider.disable_feedback_forwarding(domain)+ self._disable_with_backoff(provider, domain)except ClientError as e:# The integration can outlive its SES identity. Nothing forwards for a missing identity.if e.response["Error"]["Code"] == "NotFoundException":@@ -65,4 +71,14 @@ class Command(BaseCommand):self.stdout.write(f"Summary: {len(domains) - len(failures)} domain(s) processed, {len(failures)} failure(s)")if failures:- raise CommandError(f"Failed for {', '.join(failures)}. Rerun with --domains after you fix the cause.")+ raise CommandError(f"Failed for {len(failures)} domain(s). Rerun with --domains={','.join(failures)}")++ def _disable_with_backoff(self, provider: SESProvider, domain: str) -> None:+ for wait in [*THROTTLE_BACKOFF_SECONDS, None]:+ try:+ provider.disable_feedback_forwarding(domain)+ return+ except ClientError as e:+ if wait is None or e.response["Error"]["Code"] != "TooManyRequestsException":+ raise+ time.sleep(wait)diff --git a/products/workflows/backend/test/test_disable_ses_feedback_forwarding.py b/products/workflows/backend/test/test_disable_ses_feedback_forwarding.pyindex 4a617a51d2a0..384481192408 100644--- a/products/workflows/backend/test/test_disable_ses_feedback_forwarding.py+++ b/products/workflows/backend/test/test_disable_ses_feedback_forwarding.py@@ -21,20 +21,28 @@ class TestDisableSesFeedbackForwarding(BaseTest):config={"domain": domain, "provider": provider},)+ @patch("products.workflows.backend.management.commands.disable_ses_feedback_forwarding.time.sleep")@patch("products.workflows.backend.management.commands.disable_ses_feedback_forwarding.SESProvider")- def test_updates_ses_domains_and_reports_real_failures(self, mock_provider_cls: MagicMock) -> None:+ def test_updates_ses_domains_and_reports_real_failures(+ self, mock_provider_cls: MagicMock, _sleep: MagicMock+ ) -> None:+ throttled_once: set[str] = set()+def put(domain: str) -> None:+ if domain == "ses.com" and domain not in throttled_once:+ throttled_once.add(domain)+ raise ClientError({"Error": {"Code": "TooManyRequestsException"}}, "PutEmailIdentityFeedbackAttributes")code = {"gone.com": "NotFoundException", "broken.com": "TooManyRequestsException"}.get(domain)if code:raise ClientError({"Error": {"Code": code}}, "PutEmailIdentityFeedbackAttributes")mock_provider_cls.return_value.disable_feedback_forwarding.side_effect = put- with pytest.raises(CommandError, match="broken.com"):+ with pytest.raises(CommandError, match="--domains=broken.com$"):call_command("disable_ses_feedback_forwarding")called = [c.args[0] for c in mock_provider_cls.return_value.disable_feedback_forwarding.call_args_list]- assert called == ["broken.com", "gone.com", "ses.com"]+ assert called == ["broken.com"] * 7 + ["gone.com", "ses.com", "ses.com"]@patch("products.workflows.backend.management.commands.disable_ses_feedback_forwarding.SESProvider")def test_rejects_an_explicitly_empty_domains_value(self, mock_provider_cls: MagicMock) -> None: