Skip to content

armgit / mwmbl

Public Imported from GitHub
0

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.py
index bf894ee..f76159d 100644
--- a/mwmbl/tinysearchengine/rank.py
+++ b/mwmbl/tinysearchengine/rank.py
@@ -15,6 +15,7 @@ import numpy as np
import requests
from django.conf import settings
from django.core.cache import cache
+from django_redis.exceptions import ConnectionInterrupted
from requests.adapters import HTTPAdapter, Retry
from 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 = 600
def _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 False
def _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.py
index dfe816f..6ab7984 100644
--- a/test/test_rank.py
+++ b/test/test_rank.py
@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch
import pytest
from django.core.cache import cache
from django.test import override_settings
+from django_redis.exceptions import ConnectionInterrupted
from mwmbl.tinysearchengine import rank
from 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_RETRY
assert 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
# ---------------------------------------------------------------------------