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(clickhouse): exempt temporal activities from per-org query limiter (#72381)

posthog[bot]860c4f05424e
diff --git a/posthog/clickhouse/client/limit.py b/posthog/clickhouse/client/limit.py
index 9eec8964f294..adf9818ff405 100644
--- a/posthog/clickhouse/client/limit.py
+++ b/posthog/clickhouse/client/limit.py
@@ -65,6 +65,15 @@ return 1
"""
+def _is_in_temporal() -> bool:
+ try:
+ from temporalio import activity, workflow
+
+ return workflow.in_workflow() or activity.in_activity()
+ except ImportError:
+ return False
+
+
@dataclasses.dataclass
class RateLimit:
"""
@@ -266,6 +275,9 @@ def get_app_org_rate_limiter():
# if running in celery, we don't want rate limit to apply
# as celery tasks have their own limits on the queues + using @limit_concurrency
and not current_task
+ # if running in temporal workflow, don't apply rate limit
+ # as temporal activities have their own concurrency controls
+ and not _is_in_temporal()
),
limit_name="app_per_org",
get_task_name=lambda *args, **kwargs: f"app:query:per-org:{kwargs.get('org_id')}",
@@ -279,15 +291,6 @@ def get_app_dashboard_queries_rate_limiter():
"""
Limits the number of concurrent queries (running outside celery/temporal) per organization.
"""
-
- def _is_in_temporal() -> bool:
- try:
- from temporalio import activity, workflow
-
- return workflow.in_workflow() or activity.in_activity()
- except ImportError:
- return False
-
global __APP_CONCURRENT_DASHBOARD_QUERIES_PER_ORG
if __APP_CONCURRENT_DASHBOARD_QUERIES_PER_ORG is None:
__APP_CONCURRENT_DASHBOARD_QUERIES_PER_ORG = RateLimit(
diff --git a/posthog/clickhouse/client/test/test_limit.py b/posthog/clickhouse/client/test/test_limit.py
index cb185f73b098..44e22c8d124f 100644
--- a/posthog/clickhouse/client/test/test_limit.py
+++ b/posthog/clickhouse/client/test/test_limit.py
@@ -467,6 +467,36 @@ class TestMaterializedEndpointsRateLimiter(BaseTest):
rate_limiter.release(key, task_id)
+class TestAppOrgRateLimiter(BaseTest):
+ @patch("posthog.clickhouse.client.limit.TEST", False)
+ def test_rate_limiter_skips_for_temporal_activity(self):
+ """Experiment metric recalcs run their app queries inside Temporal activities,
+ which have their own concurrency controls — the per-org app limiter must not apply."""
+ from temporalio.testing import ActivityEnvironment
+
+ from posthog.clickhouse.client.limit import get_app_org_rate_limiter
+
+ async def check_rate_limiter_in_activity():
+ rate_limiter = get_app_org_rate_limiter()
+ return rate_limiter.applicable(org_id=str(self.organization.id), team_id=self.team.id)
+
+ env = ActivityEnvironment()
+ result = asyncio.run(env.run(check_rate_limiter_in_activity))
+
+ self.assertFalse(result, "Rate limiter should not apply in Temporal activity context")
+
+ @patch("posthog.clickhouse.client.limit.TEST", False)
+ def test_rate_limiter_applies_for_regular_requests(self):
+ from posthog.clickhouse.client.limit import get_app_org_rate_limiter
+
+ rate_limiter = get_app_org_rate_limiter()
+
+ with patch("posthog.clickhouse.client.limit.current_task", None):
+ with patch("temporalio.workflow.in_workflow", return_value=False):
+ is_applicable = rate_limiter.applicable(org_id=str(self.organization.id), team_id=self.team.id)
+ self.assertTrue(is_applicable)
+
+
class TestDashboardQueriesRateLimiter(BaseTest):
@patch("posthog.clickhouse.client.limit.TEST", False)
def test_rate_limiter_skips_for_celery_tasks(self):