fix: retry transient Tencent quote failures

This commit is contained in:
王鹏
2026-07-08 22:09:17 +08:00
parent f7ef52ef64
commit 1e8858ac04
2 changed files with 34 additions and 4 deletions

View File

@@ -50,10 +50,12 @@ class TencentQuoteProvider:
*,
endpoint: str = TENCENT_ENDPOINT,
timeout: float = 5.0,
retries: int = 1,
fetcher: Callable[[str, float], bytes] | None = None,
):
self.endpoint = endpoint
self.timeout = timeout
self.retries = retries
self._fetcher = fetcher or _default_fetcher
def fetch_quotes(self, instruments: Iterable[Instrument]) -> dict[str, QuoteSnapshot]:
@@ -62,12 +64,18 @@ class TencentQuoteProvider:
return {}
query = ",".join(symbols)
url = f"{self.endpoint}{query}"
try:
raw = self._fetcher(url, self.timeout)
except (OSError, HTTPError, URLError) as exc:
raise QuoteFetchError(f"Tencent quote request failed: {exc}") from exc
raw = self._fetch_with_retry(url)
return {quote.code: quote for quote in parse_tencent_response(raw)}
def _fetch_with_retry(self, url: str) -> bytes:
last_error: Exception | None = None
for _ in range(self.retries + 1):
try:
return self._fetcher(url, self.timeout)
except (OSError, HTTPError, URLError) as exc:
last_error = exc
raise QuoteFetchError(f"Tencent quote request failed: {last_error}") from last_error
def _default_fetcher(url: str, timeout: float) -> bytes:
with urlopen(url, timeout=timeout) as response: