: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
chore(infra): return frozen dataclasses instead of tuples (#76125)
Arno Hillen7027817efaae
diff --git a/posthog/db_circuit_breaker.py b/posthog/db_circuit_breaker.pyindex 6eaee4669192..45d13718bfcf 100644--- a/posthog/db_circuit_breaker.py+++ b/posthog/db_circuit_breaker.py@@ -10,6 +10,7 @@ from django.conf import settingsimport redisfrom statshog.defaults.django import statsd+from posthog.dataclasses import frozenfrom posthog.redis import get_clientlogger = logging.getLogger(__name__)@@ -141,6 +142,13 @@ def _get_redis() -> redis.Redis | None:)+@frozen+class _BreakerKeys:+ fails: str+ open_until: str+ probe: str++class ProductDBCircuitBreaker:"""Per-alias fail-fast circuit breaker for product databases.@@ -164,9 +172,9 @@ class ProductDBCircuitBreaker:# alias -> local monotonic-ish deadline (time.time) we believe it's open untilself._local_open_until: dict[str, float] = {}- def _keys(self, alias: str) -> tuple[str, str, str]:+ def _keys(self, alias: str) -> _BreakerKeys:base = f"{_KEY_PREFIX}:{alias}"- return f"{base}:fails", f"{base}:open_until", f"{base}:probe"+ return _BreakerKeys(fails=f"{base}:fails", open_until=f"{base}:open_until", probe=f"{base}:probe")def before_connect(self, alias: str) -> BreakerDecision:config = _load_config()@@ -184,12 +192,12 @@ class ProductDBCircuitBreaker:if client is None:return _CLOSED- _, open_until_key, probe_key = self._keys(alias)+ keys = self._keys(alias)if self._allow_script is None:self._allow_script = client.register_script(_ALLOW_SCRIPT)try:result = self._allow_script(- keys=[open_until_key, probe_key], args=[now, config.probe_timeout_seconds], client=client+ keys=[keys.open_until, keys.probe], args=[now, config.probe_timeout_seconds], client=client)except Exception:logger.exception("product_db_circuit_breaker_allow_failed", extra={"alias": alias})@@ -220,7 +228,7 @@ class ProductDBCircuitBreaker:if client is None:return- fails_key, open_until_key, probe_key = self._keys(alias)+ keys = self._keys(alias)if self._failure_script is None:self._failure_script = client.register_script(_FAILURE_SCRIPT)# Capture once so the local deadline matches the open_until Redis computes.@@ -230,7 +238,7 @@ class ProductDBCircuitBreaker:open_marker_ttl = max(config.cooldown_seconds * 10, _OPEN_MARKER_MIN_TTL_SECONDS)try:opened = self._failure_script(- keys=[fails_key, open_until_key, probe_key],+ keys=[keys.fails, keys.open_until, keys.probe],args=[now,config.failure_threshold,@@ -265,9 +273,9 @@ class ProductDBCircuitBreaker:if client is None:return- fails_key, open_until_key, probe_key = self._keys(alias)+ keys = self._keys(alias)try:- client.delete(fails_key, open_until_key, probe_key)+ client.delete(keys.fails, keys.open_until, keys.probe)except Exception:logger.exception("product_db_circuit_breaker_record_success_failed", extra={"alias": alias})returndiff --git a/posthog/test/test_db_circuit_breaker.py b/posthog/test/test_db_circuit_breaker.pyindex 52a44eeb2dad..a6a7600bbfa2 100644--- a/posthog/test/test_db_circuit_breaker.py+++ b/posthog/test/test_db_circuit_breaker.py@@ -67,7 +67,7 @@ class TestProductDBCircuitBreaker(SimpleTestCase):# resumes. TTL uses fakeredis wall-clock, so assert right after opening.self._open_breaker(ProductDBCircuitBreaker())- _, open_until_key, _ = ProductDBCircuitBreaker()._keys(ALIAS)+ open_until_key = ProductDBCircuitBreaker()._keys(ALIAS).open_untilttl = get_client().ttl(open_until_key)self.assertGreater(ttl, BREAKER_SETTINGS["PRODUCT_DB_CIRCUIT_BREAKER_COOLDOWN_SECONDS"])