: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(conversations): keep first page views out of the widget poll bucket (#129036)
Alex V43d9a0e4f9df
diff --git a/products/conversations/backend/api/tests/test_widget.py b/products/conversations/backend/api/tests/test_widget.pyindex ad0d9ce6c3b5..a383cd333448 100644--- a/products/conversations/backend/api/tests/test_widget.py+++ b/products/conversations/backend/api/tests/test_widget.py@@ -20,6 +20,8 @@ from posthog.rate_limit import WidgetTeamPollThrottlefrom posthog.test.authentication_checks import covers_authenticationfrom products.conversations.backend.api.serializers import WidgetMessageSerializer, WidgetTicketsQuerySerializer+from products.conversations.backend.api.widget import WidgetTicketsTeamThrottle+from products.conversations.backend.cache import get_session_has_tickets_cache_keyfrom products.conversations.backend.models import SigningSecret, Ticketfrom products.conversations.backend.models.constants import Channel, ChannelDetail, Statusfrom products.conversations.backend.services.identity import (@@ -106,6 +108,12 @@ class TestWidgetAPI(BaseTest):def test_exhausted_poll_throttle_does_not_block_send(self):cache.clear()+ Ticket.objects.create_with_number(+ team=self.team,+ widget_session_id=self.widget_session_id,+ distinct_id=self.distinct_id,+ channel_source="widget",+ )tickets_url = f"/api/conversations/v1/widget/tickets?widget_session_id={self.widget_session_id}"try:with patch.object(WidgetTeamPollThrottle, "rate", "1/minute"):@@ -126,6 +134,76 @@ class TestWidgetAPI(BaseTest):finally:cache.clear()+ @parameterized.expand(+ [+ ("canonical", str),+ ("uppercase", lambda session_id: session_id.upper()),+ ("hyphenless", lambda session_id: session_id.replace("-", "")),+ ]+ )+ def test_sessions_without_tickets_do_not_use_the_poll_bucket(self, _name, spell_session_id):+ cache.clear()+ Ticket.objects.create_with_number(+ team=self.team,+ widget_session_id=self.widget_session_id,+ distinct_id=self.distinct_id,+ channel_source="widget",+ )+ tickets_url = "/api/conversations/v1/widget/tickets?widget_session_id={}"+ try:+ with (+ patch.object(WidgetTeamPollThrottle, "rate", "1/minute"),+ patch.object(WidgetTicketsTeamThrottle, "new_session_rate", "1/minute"),+ ):+ self.client.get(tickets_url.format(self.widget_session_id), **self._get_headers())+ existing_session = self.client.get(+ tickets_url.format(spell_session_id(self.widget_session_id)), **self._get_headers()+ )+ new_session = self.client.get(tickets_url.format(uuid.uuid4()), **self._get_headers())+ other_new_session = self.client.get(tickets_url.format(uuid.uuid4()), **self._get_headers())+ self.assertEqual(existing_session.status_code, status.HTTP_429_TOO_MANY_REQUESTS)+ self.assertEqual(new_session.status_code, status.HTTP_200_OK)+ self.assertEqual(other_new_session.status_code, status.HTTP_429_TOO_MANY_REQUESTS)+ finally:+ cache.clear()++ def test_full_new_session_bucket_does_not_use_the_poll_bucket(self):+ cache.clear()+ Ticket.objects.create_with_number(+ team=self.team,+ widget_session_id=self.widget_session_id,+ distinct_id=self.distinct_id,+ channel_source="widget",+ )+ tickets_url = "/api/conversations/v1/widget/tickets?widget_session_id={}"+ try:+ with (+ patch.object(WidgetTeamPollThrottle, "rate", "3/minute"),+ patch.object(WidgetTicketsTeamThrottle, "new_session_rate", "1/minute"),+ patch.object(WidgetTicketsTeamThrottle, "session_lookup_rate", "1/minute"),+ ):+ first_poll = self.client.get(tickets_url.format(self.widget_session_id), **self._get_headers())+ cache.delete(get_session_has_tickets_cache_key(self.team.id, self.widget_session_id))+ self.client.get(tickets_url.format(uuid.uuid4()), **self._get_headers())+ returning_session = self.client.get(tickets_url.format(self.widget_session_id), **self._get_headers())+ with patch("products.conversations.backend.api.widget._session_has_tickets") as ticket_lookup:+ overflow = self.client.get(tickets_url.format(uuid.uuid4()), **self._get_headers())+ last_poll = self.client.get(tickets_url.format(self.widget_session_id), **self._get_headers())+ self.assertEqual(first_poll.status_code, status.HTTP_200_OK)+ self.assertEqual(returning_session.status_code, status.HTTP_200_OK)+ self.assertEqual(overflow.status_code, status.HTTP_429_TOO_MANY_REQUESTS)+ ticket_lookup.assert_not_called()+ self.assertEqual(last_poll.status_code, status.HTTP_200_OK)+ finally:+ cache.clear()++ @parameterized.expand([("not_a_uuid", "not-a-uuid"), ("nul_byte", "%00")])+ def test_list_tickets_rejects_invalid_widget_session_id(self, _name, widget_session_id):+ response = self.client.get(+ f"/api/conversations/v1/widget/tickets?widget_session_id={widget_session_id}", **self._get_headers()+ )+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+def test_create_ticket_channel_detail_widget_enabled(self):self.team.conversations_settings = {**self.team.conversations_settings, "widget_enabled": True}self.team.save()diff --git a/products/conversations/backend/api/widget.py b/products/conversations/backend/api/widget.pyindex a92f84e01e51..7b29bf2d3717 100644--- a/products/conversations/backend/api/widget.py+++ b/products/conversations/backend/api/widget.py@@ -34,7 +34,12 @@ from posthog.event_usage import report_team_actionfrom posthog.exceptions_capture import capture_exceptionfrom posthog.models import Teamfrom posthog.models.comment import Comment-from posthog.rate_limit import WIDGET_POLL_THROTTLES, WIDGET_WRITE_THROTTLES+from posthog.rate_limit import (+ WIDGET_POLL_THROTTLES,+ WIDGET_WRITE_THROTTLES,+ WidgetTeamPollThrottle,+ WidgetUserBurstThrottle,+)from products.conversations.backend.api.serializers import (WIDGET_TICKETS_DEFAULT_LIMIT,@@ -46,6 +51,7 @@ from products.conversations.backend.api.serializers import ()from products.conversations.backend.cache import (get_cached_messages,+ get_cached_session_has_tickets,get_cached_tickets,get_identity_tickets_cache_namespace,get_person_distinct_ids,@@ -53,6 +59,7 @@ from products.conversations.backend.cache import (invalidate_tickets_cache,invalidate_unread_count_cache,set_cached_messages,+ set_cached_session_has_tickets,set_cached_tickets,)from products.conversations.backend.models import SigningSecret, Ticket@@ -291,6 +298,115 @@ def _identity_tickets_cache_key(cache_namespace: str, verified_distinct_id: str,return f"{base_key}:email:{email_digest}"+def _anonymous_widget_session_id(request: Request) -> str | None:+ """Canonical widget_session_id of an anonymous request, or None.++ The tickets view parses the parameter as a UUID, so the lookup here must use the same+ spelling. Otherwise an uppercase or hyphenless form of a session that has tickets would+ look like a new session.+ """+ raw_session_id = request.query_params.get("widget_session_id")+ if not raw_session_id or request.query_params.get("identity_distinct_id"):+ return None+ try:+ return str(uuid.UUID(raw_session_id))+ except ValueError:+ return None+++def _session_has_tickets(team: Team, widget_session_id: str) -> bool:+ has_tickets = Ticket.objects.filter(team=team, widget_session_id=widget_session_id).exists()+ if has_tickets:+ set_cached_session_has_tickets(team.id, widget_session_id)+ return has_tickets+++@frozen+class _CounterWindow:+ key: str+ limit: int+ duration: int+ seconds_left: float+++class WidgetTicketsTeamThrottle(WidgetTeamPollThrottle):+ """Moves ticket list requests from anonymous sessions that have no tickets to their own bucket.++ The widget asks for the ticket list on each page load. On a busy site, first page views,+ crawlers included, would use up the poll bucket, and visitors with an open conversation+ would get 429s. The separate bucket still caps how many lookups one widget token can cause.+ """++ new_session_scope = "widget_team_new_session"+ new_session_rate = "600/minute"+ session_lookup_scope = "widget_team_session_lookup"+ session_lookup_rate = "60/minute"++ _counter_wait: float | None = None++ def allow_request(self, request: Request, view: APIView) -> bool:+ team = request.auth+ widget_session_id = _anonymous_widget_session_id(request)+ if (+ isinstance(team, Team)+ and widget_session_id is not None+ and not get_cached_session_has_tickets(team.id, widget_session_id)+ ):+ # Every ticket lookup takes room in one of two capped counters, so overflow traffic cannot+ # cause unbounded queries. The small lookup counter lets a returning session with an expired+ # cache entry reach the poll bucket while first page views fill the new-session counter.+ if self._counter_has_room(request, view, self.new_session_scope, self.new_session_rate):+ if not _session_has_tickets(team, widget_session_id):+ return self._counter_take(request, view, self.new_session_scope, self.new_session_rate)+ elif not (+ self._counter_take(request, view, self.session_lookup_scope, self.session_lookup_rate)+ and _session_has_tickets(team, widget_session_id)+ ):+ return False+ self._counter_wait = None+ return super().allow_request(request, view)++ def wait(self) -> float | None:+ return self._counter_wait if self._counter_wait is not None else super().wait()++ # The new-session counter must stay cheap when it is full, because a flood of first page views+ # sends every request through it. A fixed-window counter costs one GET or INCR at any rate, while+ # SimpleRateThrottle reads and rewrites a list with one timestamp for each request in the window.+ def _counter_window(self, request: Request, view: APIView, scope: str, rate: str) -> _CounterWindow:+ num_requests, duration = self.parse_rate(rate)+ assert num_requests is not None and duration is not None+ window_start = int(self.timer() // duration * duration)+ poll_key = self.get_cache_key(request, view)+ ident = poll_key.removeprefix(self.cache_format % {"scope": self.scope, "ident": ""})+ return _CounterWindow(+ key=f"{self.cache_format % {'scope': scope, 'ident': ident}}:{window_start}",+ limit=num_requests,+ duration=duration,+ seconds_left=window_start + duration - self.timer(),+ )++ def _counter_has_room(self, request: Request, view: APIView, scope: str, rate: str) -> bool:+ window = self._counter_window(request, view, scope, rate)+ if (self.cache.get(window.key) or 0) < window.limit:+ return True+ self._counter_wait = window.seconds_left+ return False++ def _counter_take(self, request: Request, view: APIView, scope: str, rate: str) -> bool:+ window = self._counter_window(request, view, scope, rate)+ self.cache.add(window.key, 0, window.duration)+ try:+ count = self.cache.incr(window.key)+ except ValueError:+ # The window expired between the add and the incr, so this request starts the next one.+ self.cache.set(window.key, 1, window.duration)+ count = 1+ if count <= window.limit:+ return True+ self._counter_wait = window.seconds_left+ return False++class WidgetMessageView(APIView):"""POST /api/conversations/v1/widget/message@@ -462,6 +578,7 @@ class WidgetMessageView(APIView):session_context=session_context,identity_verified=verified_distinct_id is not None,)+ set_cached_session_has_tickets(team.id, widget_session_id)try:report_team_action(team, "support ticket created", {"channel_source": ticket.channel_source})@@ -645,7 +762,7 @@ class WidgetTicketsView(APIView):authentication_classes = [WidgetAuthentication]permission_classes = [AllowAny]- throttle_classes = WIDGET_POLL_THROTTLES+ throttle_classes = (WidgetUserBurstThrottle, WidgetTicketsTeamThrottle)def get(self, request: Request) -> Response:"""List tickets for a widget_session_id."""@@ -733,6 +850,9 @@ class WidgetTicketsView(APIView):response_data = {"count": total_count, "results": ticket_list}+ if verified_distinct_id is None and cache_key_id is not None and total_count > 0:+ set_cached_session_has_tickets(team.id, cache_key_id)+# Cache first page (skip empty results to avoid stale cache after restore/migration)if cacheable_key is not None and total_count > 0:set_cached_tickets(team.id, cacheable_key, response_data, status_filter)diff --git a/products/conversations/backend/cache.py b/products/conversations/backend/cache.pyindex f630320770b5..5144cd41edb8 100644--- a/products/conversations/backend/cache.py+++ b/products/conversations/backend/cache.py@@ -67,6 +67,36 @@ def set_cached_messages(team_id: int, ticket_id: str, response_data: dict, afterlogger.warning("conversations_cache_set_error", key=key)+# Widget Session Has Tickets+# Only a positive result is cached, because a session keeps its tickets once it has them.++SESSION_HAS_TICKETS_CACHE_TTL = 24 * 3600+++def get_session_has_tickets_cache_key(team_id: int, widget_session_id: str) -> str:+ session_digest = hashlib.sha256(widget_session_id.encode()).hexdigest()+ return _make_cache_key("session_has_tickets", str(team_id), session_digest)+++def get_cached_session_has_tickets(team_id: int, widget_session_id: str) -> bool:+ try:+ return cache.get(get_session_has_tickets_cache_key(team_id, widget_session_id)) is True+ except Exception:+ logger.warning("conversations_cache_get_error", team_id=team_id, cache_type="session_has_tickets")+ return False+++def set_cached_session_has_tickets(team_id: int, widget_session_id: str) -> None:+ try:+ cache.set(+ get_session_has_tickets_cache_key(team_id, widget_session_id),+ True,+ timeout=SESSION_HAS_TICKETS_CACHE_TTL,+ )+ except Exception:+ logger.warning("conversations_cache_set_error", team_id=team_id, cache_type="session_has_tickets")++# Widget Tickets List Cache