Stage 07.4.3.2 — price polling, event bus and UI throttling

This commit is contained in:
2026-05-02 14:57:43 +03:00
parent 38c8686a9b
commit bd6b40fcb2
9 changed files with 644 additions and 31 deletions

View File

@@ -16,7 +16,7 @@ class MarketPriceSnapshot:
bid_price: float | None
ask_price: float | None
updated_at: str
source: str = "websocket"
source: str = "market-cache"
class MarketPriceCache:
@@ -32,6 +32,7 @@ class MarketPriceCache:
bid_price: float | None = None,
ask_price: float | None = None,
updated_at: str | None = None,
source: str = "market-polling",
) -> None:
settings = load_settings()
@@ -44,6 +45,7 @@ class MarketPriceCache:
bid_price=bid_price,
ask_price=ask_price,
updated_at=updated_at,
source=source,
)
# получить последнюю цену

View File

@@ -0,0 +1,56 @@
# app/src/integrations/exchange/market_data_runner.py
from __future__ import annotations
import asyncio
from typing import Callable
from src.integrations.exchange.service import ExchangeService
class MarketDataRunner:
_task: asyncio.Task | None = None
_interval_seconds: int = 1
_symbol_provider: Callable[[], str | None] | None = None
# запустить быстрый polling рыночной цены
@classmethod
def start(
cls,
*,
symbol_provider: Callable[[], str | None],
interval_seconds: int = 1,
) -> None:
cls._symbol_provider = symbol_provider
cls._interval_seconds = interval_seconds
if cls._task is not None and not cls._task.done():
return
cls._task = asyncio.create_task(cls._worker())
# остановить polling
@classmethod
def stop(cls) -> None:
if cls._task is None:
return
cls._task.cancel()
cls._task = None
# рабочий цикл polling
@classmethod
async def _worker(cls) -> None:
while True:
symbol = cls._symbol_provider() if cls._symbol_provider else None
if symbol:
try:
await asyncio.to_thread(
ExchangeService().refresh_price_cache,
symbol,
)
except Exception:
pass
await asyncio.sleep(cls._interval_seconds)

View File

@@ -29,6 +29,8 @@ from src.integrations.exchange.market_cache import MarketPriceCache
class ExchangeService:
_exchange_symbols_cache: list[ExchangeSymbol] | None = None
def __init__(self) -> None:
self.settings = load_settings()
self.journal = JournalService()
@@ -192,6 +194,35 @@ class ExchangeService:
message=f"Private API OK. Балансов получено: {len(balances)}",
)
# принудительно обновить market cache через REST
def refresh_price_cache(self, symbol: str | None = None) -> TickerPrice:
symbol_to_use = symbol or self.settings.default_symbol
if not self.settings.exchange_enabled:
ticker = mock_ticker_price(symbol_to_use)
MarketPriceCache.set_price(
symbol=ticker.symbol,
price=ticker.price,
updated_at=ticker.updated_at,
source=ticker.source,
)
return ticker
validation = self.validate_symbol(symbol_to_use)
if not validation.is_valid:
raise ExchangeError(validation.message)
ticker = self._get_real_price(validation.normalized_symbol)
MarketPriceCache.set_price(
symbol=ticker.symbol,
price=ticker.price,
updated_at=ticker.updated_at,
source=ticker.source,
)
return ticker
# получить цену инструмента: сначала WebSocket cache, потом REST fallback
def get_price(self, symbol: str | None = None) -> TickerPrice:
symbol_to_use = symbol or self.settings.default_symbol
@@ -336,6 +367,9 @@ class ExchangeService:
if not self.settings.exchange_enabled:
return []
if type(self)._exchange_symbols_cache is not None:
return type(self)._exchange_symbols_cache
client = ExchangeRestClient()
try:
@@ -470,6 +504,8 @@ class ExchangeService:
)
)
type(self)._exchange_symbols_cache = items
return items
def validate_symbol(self, raw_symbol: str) -> SymbolValidationResult: