An open source, non-profit web search engine
Keep search working when the circuit breaker's cache is down
Claude29192517303f
diff --git a/mwmbl/tinysearchengine/rank.py b/mwmbl/tinysearchengine/rank.pyindex bf894ee..f76159d 100644--- a/mwmbl/tinysearchengine/rank.py+++ b/mwmbl/tinysearchengine/rank.py@@ -15,6 +15,7 @@ import numpy as npimport requestsfrom django.conf import settingsfrom django.core.cache import cache+from django_redis.exceptions import ConnectionInterruptedfrom requests.adapters import HTTPAdapter, Retryfrom mwmbl.format import get_query_regex@@ -525,7 +526,7 @@ WIKI_FETCH_LIMIT = NUM_WIKI_RESULTS# on (max_wiki_results=5 there). Changing it changes a trained-on feature: retrain.WIKI_TOP_SCORE = 6.0-# Live search does not retry Wikipedia. It used to retry 429s, honouring Retry-After, on+# Live search does not retry Wikipedia, for 429s or 5xx alike. It used to retry 429s, honouring Retry-After, on# the theory that live search never bursts - but it does, and in October 2026 Wikipedia# answered about three calls in four with a 429 and a Retry-After of tens of seconds. Each# such search slept through every retry, over a minute in all, and since sync views share@@ -544,8 +545,11 @@ WIKI_RETRY = Retry(WIKI_TIMEOUT_SECONDS = 5-# A 429 pauses every live Wikipedia call for as long as Wikipedia asks. Held in Redis rather-# than per process, so that one worker's 429 spares the others from collecting their own.+# A 429 pauses every live Wikipedia call for as long as Wikipedia asks. Held in the shared+# cache (Redis in production) rather than per process, so that one worker's 429 spares the+# others from collecting their own. The breaker only saves Wikipedia calls, so a cache that+# is down must not cost a search its results: it reads as closed, as the old per-process+# breaker never depended on Redis.WIKI_CIRCUIT_KEY = "wiki-circuit-open"WIKI_CIRCUIT_DEFAULT_SECONDS = 60# A Retry-After this long is more likely a mistake than a request; don't let one switch@@ -554,11 +558,18 @@ WIKI_CIRCUIT_MAX_SECONDS = 600def _wiki_circuit_open() -> bool:- return cache.get(WIKI_CIRCUIT_KEY) is not None+ try:+ return cache.get(WIKI_CIRCUIT_KEY) is not None+ except ConnectionInterrupted:+ logger.warning("Could not read the Wikipedia circuit breaker", exc_info=True)+ return Falsedef _trip_wiki_circuit(seconds: int):- cache.set(WIKI_CIRCUIT_KEY, 1, timeout=seconds)+ try:+ cache.set(WIKI_CIRCUIT_KEY, 1, timeout=seconds)+ except ConnectionInterrupted:+ logger.warning("Could not trip the Wikipedia circuit breaker", exc_info=True)def _wiki_cooldown_seconds(response: requests.Response) -> int:diff --git a/test/test_rank.py b/test/test_rank.pyindex dfe816f..6ab7984 100644--- a/test/test_rank.py+++ b/test/test_rank.py@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patchimport pytestfrom django.core.cache import cachefrom django.test import override_settings+from django_redis.exceptions import ConnectionInterruptedfrom mwmbl.tinysearchengine import rankfrom mwmbl.tinysearchengine.indexer import Document, DocumentSource@@ -193,10 +194,32 @@ def test_waiting_out_rate_limits_retries_and_ignores_the_circuit():_get_wiki_results_with_session(session, wait_out_rate_limits=True)- session.mount.assert_called_once()+ adapter = session.mount.call_args.args[1]+ assert adapter.max_retries is rank.WIKI_RETRYassert session.get.call_count == 1+def test_a_cache_outage_does_not_cost_a_search_its_wiki_results():+ """The breaker only saves Wikipedia calls; the old per-process one never needed Redis."""+ session = MagicMock()+ session.get.return_value = _wiki_response(200)+ session.get.return_value.json.return_value = {"query": {"search": [{"title": "Python", "snippet": "A language"}]}}+ outage = ConnectionInterrupted(connection=None)++ with patch.object(rank.cache, "get", side_effect=outage), patch.object(rank.cache, "set", side_effect=outage):+ results = _get_wiki_results_with_session(session)++ assert [document.title for document in results] == ["Python"]+++def test_a_cache_outage_while_tripping_the_breaker_is_survived():+ session = MagicMock()+ session.get.return_value = _wiki_response(429, {"Retry-After": "11"})++ with patch.object(rank.cache, "set", side_effect=ConnectionInterrupted(connection=None)):+ assert _get_wiki_results_with_session(session) == []++# ---------------------------------------------------------------------------# Keeping the wiki results the model is served the ones it was trained on# ---------------------------------------------------------------------------