searcher: skip empty-result instances in failover

Treat an instance that answers 200 with zero results as a soft failure and
fall through to the next instance, instead of short-circuiting on it. This
makes public SearXNG backups actually take over when the primary instance is
up but its upstream engines are rate-limited (200-but-empty). Only return []
when every responding instance is empty; still raise SearchError only when
every instance hard-fails (unreachable/non-200/non-JSON).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
User 2026-07-07 17:04:43 -07:00
parent ae56657d94
commit 0a80d0ef26
2 changed files with 27 additions and 4 deletions

View file

@ -1,5 +1,11 @@
# SearXNG instance pool. MVP: one local instance; add more entries later for # SearXNG instance pool. The searcher tries these in order and uses the first
# rotation/fan-out (the searcher already reads the whole list for failover). # one that returns a NON-EMPTY result set; an instance that answers 200-but-empty
# (its upstream engines are rate-limited) is skipped in favor of the next. It
# raises only when every instance hard-fails (unreachable / non-200 / non-JSON).
#
# Keep the internal instance you control FIRST, then add public backups below.
# Public searx.space instances must have the JSON format ENABLED or they get
# skipped as a hard failure (they return HTML, which fails JSON parsing).
instances: instances:
- name: local - name: local
url: http://searxng:8080 url: http://searxng:8080

View file

@ -46,10 +46,18 @@ def normalize(raw_results: list[dict], instance_name: str, max_results: int) ->
async def search_failover( async def search_failover(
client: httpx.AsyncClient, instances: list[dict], query: str, max_results: int, timeout_s: float client: httpx.AsyncClient, instances: list[dict], query: str, max_results: int, timeout_s: float
) -> list[dict]: ) -> list[dict]:
"""Try instances in order; first success wins. Raises SearchError if all fail.""" """Try instances in order; first instance with non-empty results wins.
An instance that answers 200 with an empty result set is treated as a soft
failure (its upstream engines are likely rate-limited) and we fall through
to the next instance. Only if *every* instance is empty do we return []; we
raise SearchError only when every instance hard-fails (unreachable/non-200/
non-JSON).
"""
if not instances: if not instances:
raise SearchError("no SearXNG instances configured") raise SearchError("no SearXNG instances configured")
errors = [] errors = []
any_responded = False
for inst in instances: for inst in instances:
try: try:
resp = await client.get( resp = await client.get(
@ -60,8 +68,17 @@ async def search_failover(
if resp.status_code != 200: if resp.status_code != 200:
raise SearchError(f"HTTP {resp.status_code}") raise SearchError(f"HTTP {resp.status_code}")
raw = resp.json().get("results", []) raw = resp.json().get("results", [])
return normalize(raw, inst["name"], max_results)
except (httpx.HTTPError, SearchError, ValueError) as exc: except (httpx.HTTPError, SearchError, ValueError) as exc:
log.warning("instance %s failed: %s", inst["name"], exc) log.warning("instance %s failed: %s", inst["name"], exc)
errors.append(f"{inst['name']}: {exc}") errors.append(f"{inst['name']}: {exc}")
continue
any_responded = True
results = normalize(raw, inst["name"], max_results)
if results:
return results
log.warning("instance %s returned 0 results; trying next", inst["name"])
if any_responded:
# Every instance that answered came back empty — a real "no results"
# (or all upstreams rate-limited), not a hard failure.
return []
raise SearchError("; ".join(errors)) raise SearchError("; ".join(errors))