mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-23 20:02:35 +00:00
Fixes #1531. All Direct Download providers share the one search budget (`RELEASE_SEARCH_TIMEOUT`, 300s). Anna's Archive runs first, and when its bypass keeps failing the retries use up the whole budget. OceanofPDF then sees an expired deadline, skips its queries, and the search raises Anna's Archive's error even though OceanofPDF never got a turn. I didn't split the budget evenly, since the deadline module itself says a cold-start Anna's Archive solve can legitimately take 60 to 120s and a 150s or 37s slice would break setups that work today. Instead `search_deadline.unit_deadline(waiting=n)` opens a child deadline for one unit of work. It gets the parent's remaining time minus 30s for each unit still waiting, never less than 30s and never more than the parent has left. A single provider still gets everything, a fast provider's unused time rolls on to the next, and a stuck one stops at its own limit with the reserve intact. It's used in the provider loop in `DirectDownloadSource.search`, and in the source loop in `/api/releases` since it's the same shape and four lines. With several sources enabled the first one now gets 30s less per source behind it, which is still well above a cold solve for the usual two or three. Tests: a stuck first provider no longer starves the second (fails on main, passes here), a lone provider gets the full budget, and a child never outlives its parent. Two existing tests in `test_releases_api_budget.py` tripped the deadline in force from inside the source, which is now the child, so they use a short request budget instead. ruff, basedpyright on the changed files and the non-e2e suite are clean. OceanofPDF is inactive on my own instance (no mirror URL set), so I haven't reproduced this live. It's verified by reading the code and by the tests. I had Claude do the pass on this one.
78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
"""A stuck first provider must not starve the second (issue #1531).
|
|
|
|
All providers share one search budget. Anna's Archive runs first, and its bypass retries
|
|
could spend the whole of it, so OceanofPDF saw an expired budget and never searched.
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from shelfmark.core import search_deadline
|
|
from shelfmark.metadata_providers import BookMetadata
|
|
from shelfmark.release_sources.direct_download import registry
|
|
from shelfmark.release_sources.direct_download.source import DirectDownloadSource
|
|
|
|
|
|
class _Provider:
|
|
def __init__(self, name, behaviour):
|
|
self.name = name
|
|
self.display_name = name
|
|
self._behaviour = behaviour
|
|
self.expired_on_entry = None
|
|
|
|
def is_enabled(self):
|
|
return True
|
|
|
|
def search(self, book, plan, *, expand_search=False, content_type="ebook"):
|
|
self.expired_on_entry = search_deadline.expired()
|
|
return self._behaviour(self)
|
|
|
|
|
|
def _stuck(_provider):
|
|
"""Wait on the cancel flag the way a bypass does, then fail like Anna's Archive."""
|
|
event = search_deadline.cancel_event()
|
|
assert event is not None
|
|
event.wait(timeout=5)
|
|
raise RuntimeError("bypass failed")
|
|
|
|
|
|
def _search(providers, monkeypatch):
|
|
monkeypatch.setattr(registry, "enabled_providers", lambda p=None: tuple(p))
|
|
monkeypatch.setattr(registry, "get_unavailable_reason", lambda p=None: None)
|
|
monkeypatch.setattr(
|
|
"shelfmark.release_sources.direct_download.source._browse_record_to_release",
|
|
lambda record: record,
|
|
)
|
|
source = DirectDownloadSource.__new__(DirectDownloadSource)
|
|
source._providers = providers
|
|
book = BookMetadata(provider="x", provider_id="1", title="T", authors=["A"])
|
|
return source.search(book, None) # pyright: ignore[reportArgumentType]
|
|
|
|
|
|
def test_a_stuck_first_provider_leaves_the_second_a_chance(monkeypatch):
|
|
monkeypatch.setattr(search_deadline, "DEFAULT_UNIT_RESERVE_SECONDS", 0.3)
|
|
original = search_deadline.unit_deadline
|
|
|
|
def short_reserve(waiting=0, *, reserve=0.5):
|
|
return original(waiting, reserve=reserve)
|
|
|
|
monkeypatch.setattr(search_deadline, "unit_deadline", short_reserve)
|
|
first = _Provider("aa", _stuck)
|
|
second = _Provider("oceanofpdf", lambda _p: ["found"])
|
|
with search_deadline.search_deadline(1.5):
|
|
releases = _search([first, second], monkeypatch)
|
|
assert second.expired_on_entry is False
|
|
assert releases == ["found"]
|
|
|
|
|
|
def test_a_single_provider_still_gets_the_whole_budget(monkeypatch):
|
|
seen = {}
|
|
|
|
def record(_p):
|
|
seen["budget"] = search_deadline.current().budget_seconds
|
|
return ["found"]
|
|
|
|
only = _Provider("aa", record)
|
|
with search_deadline.search_deadline(300) as parent:
|
|
_search([only], monkeypatch)
|
|
assert seen["budget"] == pytest.approx(parent.budget_seconds, abs=1)
|