增加排序
This commit is contained in:
+84
-34
@@ -1,17 +1,20 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from .config import settings
|
||||
from .config import ROOT_DIR, settings
|
||||
from .exceptions import BinanceRateLimitedError
|
||||
from .http_client import httpx_client_kwargs
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 418 = IP 被币安临时封禁(请求过快);429 = 触发频率限制
|
||||
_RATE_LIMIT_CODES = {418, 429}
|
||||
_SYMBOLS_CACHE_FILE = ROOT_DIR / "data" / "symbols_cache.json"
|
||||
|
||||
|
||||
class BinanceFuturesClient:
|
||||
@@ -23,6 +26,16 @@ class BinanceFuturesClient:
|
||||
self._last_request_at: float = 0.0
|
||||
self._ban_until: float = 0.0
|
||||
|
||||
def is_rate_limited(self) -> bool:
|
||||
return time.monotonic() < self._ban_until
|
||||
|
||||
def rate_limit_remaining_sec(self) -> int:
|
||||
return max(0, int(self._ban_until - time.monotonic()))
|
||||
|
||||
def _set_ban(self, retry_after: int) -> None:
|
||||
retry_after = max(retry_after, settings.ban_cooldown_sec)
|
||||
self._ban_until = max(self._ban_until, time.monotonic() + retry_after)
|
||||
|
||||
async def _ensure_client(self) -> httpx.AsyncClient:
|
||||
if self._client is None or self._client.is_closed:
|
||||
limits = httpx.Limits(
|
||||
@@ -42,12 +55,15 @@ class BinanceFuturesClient:
|
||||
self._client = None
|
||||
|
||||
async def _throttle(self) -> None:
|
||||
if self.is_rate_limited():
|
||||
remaining = self.rate_limit_remaining_sec()
|
||||
raise BinanceRateLimitedError(remaining, "throttle")
|
||||
async with self._throttle_lock:
|
||||
now = time.monotonic()
|
||||
if now < self._ban_until:
|
||||
wait = self._ban_until - now
|
||||
logger.warning("Binance IP cooldown, sleeping %.0fs", wait)
|
||||
await asyncio.sleep(wait)
|
||||
raise BinanceRateLimitedError(
|
||||
int(self._ban_until - now), "throttle"
|
||||
)
|
||||
gap = settings.request_interval_sec - (now - self._last_request_at)
|
||||
if gap > 0:
|
||||
await asyncio.sleep(gap)
|
||||
@@ -58,7 +74,11 @@ class BinanceFuturesClient:
|
||||
last_err: Exception | None = None
|
||||
|
||||
for attempt in range(1, settings.max_retries + 1):
|
||||
await self._throttle()
|
||||
try:
|
||||
await self._throttle()
|
||||
except BinanceRateLimitedError:
|
||||
raise
|
||||
|
||||
try:
|
||||
client = await self._ensure_client()
|
||||
resp = await client.get(url, params=params or {})
|
||||
@@ -66,58 +86,88 @@ class BinanceFuturesClient:
|
||||
retry_after = int(
|
||||
resp.headers.get("Retry-After", settings.ban_cooldown_sec)
|
||||
)
|
||||
retry_after = max(retry_after, settings.ban_cooldown_sec)
|
||||
self._ban_until = time.monotonic() + retry_after
|
||||
logger.warning(
|
||||
"Binance HTTP %s on %s, cooldown %ss (attempt %d/%d)",
|
||||
self._set_ban(retry_after)
|
||||
logger.error(
|
||||
"Binance HTTP %s on %s — IP 封禁约 %ss,停止重试",
|
||||
resp.status_code,
|
||||
path,
|
||||
retry_after,
|
||||
attempt,
|
||||
settings.max_retries,
|
||||
)
|
||||
last_err = httpx.HTTPStatusError(
|
||||
f"{resp.status_code}",
|
||||
request=resp.request,
|
||||
response=resp,
|
||||
)
|
||||
await asyncio.sleep(retry_after)
|
||||
continue
|
||||
raise BinanceRateLimitedError(retry_after, path)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except BinanceRateLimitedError:
|
||||
raise
|
||||
except httpx.HTTPStatusError as e:
|
||||
last_err = e
|
||||
if e.response.status_code in _RATE_LIMIT_CODES:
|
||||
continue
|
||||
raise
|
||||
raise
|
||||
except (httpx.ConnectError, httpx.ReadTimeout) as e:
|
||||
last_err = e
|
||||
logger.warning("Binance request error %s (attempt %d)", path, attempt)
|
||||
logger.warning("Binance network error %s (attempt %d)", path, attempt)
|
||||
await asyncio.sleep(min(2 * attempt, 10))
|
||||
|
||||
raise last_err or RuntimeError(f"Binance request failed: {path}")
|
||||
|
||||
def _load_symbols_file(self) -> list[str] | None:
|
||||
try:
|
||||
if _SYMBOLS_CACHE_FILE.exists():
|
||||
data = json.loads(_SYMBOLS_CACHE_FILE.read_text(encoding="utf-8"))
|
||||
if isinstance(data, list) and data:
|
||||
return sorted(data)
|
||||
except Exception as e:
|
||||
logger.warning("Load symbols cache file failed: %s", e)
|
||||
return None
|
||||
|
||||
def _save_symbols_file(self, symbols: list[str]) -> None:
|
||||
try:
|
||||
_SYMBOLS_CACHE_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
_SYMBOLS_CACHE_FILE.write_text(
|
||||
json.dumps(symbols, ensure_ascii=False),
|
||||
encoding="utf-8",
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning("Save symbols cache file failed: %s", e)
|
||||
|
||||
async def get_usdt_perpetual_symbols(self) -> list[str]:
|
||||
if self._symbols_cache:
|
||||
return self._symbols_cache
|
||||
info = await self._get("/fapi/v1/exchangeInfo")
|
||||
symbols = []
|
||||
for s in info.get("symbols", []):
|
||||
if (
|
||||
s.get("contractType") == "PERPETUAL"
|
||||
and s.get("quoteAsset") == "USDT"
|
||||
and s.get("status") == "TRADING"
|
||||
):
|
||||
symbols.append(s["symbol"])
|
||||
self._symbols_cache = sorted(symbols)
|
||||
logger.info("Loaded %d USDT perpetual symbols", len(self._symbols_cache))
|
||||
return self._symbols_cache
|
||||
|
||||
if self.is_rate_limited():
|
||||
cached = self._load_symbols_file()
|
||||
if cached:
|
||||
self._symbols_cache = cached
|
||||
logger.info("Using cached symbols file (%d)", len(cached))
|
||||
return cached
|
||||
raise BinanceRateLimitedError(self.rate_limit_remaining_sec(), "symbols")
|
||||
|
||||
try:
|
||||
info = await self._get("/fapi/v1/exchangeInfo")
|
||||
symbols = []
|
||||
for s in info.get("symbols", []):
|
||||
if (
|
||||
s.get("contractType") == "PERPETUAL"
|
||||
and s.get("quoteAsset") == "USDT"
|
||||
and s.get("status") == "TRADING"
|
||||
):
|
||||
symbols.append(s["symbol"])
|
||||
self._symbols_cache = sorted(symbols)
|
||||
self._save_symbols_file(self._symbols_cache)
|
||||
logger.info("Loaded %d USDT perpetual symbols", len(self._symbols_cache))
|
||||
return self._symbols_cache
|
||||
except BinanceRateLimitedError:
|
||||
cached = self._load_symbols_file()
|
||||
if cached:
|
||||
self._symbols_cache = cached
|
||||
logger.info("Rate limited, using symbols file (%d)", len(cached))
|
||||
return cached
|
||||
raise
|
||||
|
||||
def clear_symbol_cache(self) -> None:
|
||||
self._symbols_cache = None
|
||||
|
||||
async def get_24hr_tickers(self) -> list[dict]:
|
||||
"""单次请求获取全市场 24h 行情(用于缩小 K 线拉取范围)。"""
|
||||
data = await self._get("/fapi/v1/ticker/24hr")
|
||||
return data if isinstance(data, list) else []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user