refactor: make klines_last_refresh private

This commit is contained in:
Matthias
2026-04-25 12:49:28 +02:00
parent c34cd6a7dd
commit 093cc74774
2 changed files with 10 additions and 10 deletions
+4 -4
View File
@@ -28,7 +28,7 @@ class ExchangeWS:
self._klines_watching: set[PairWithTimeframe] = set()
self._klines_scheduled: set[PairWithTimeframe] = set()
self.klines_last_refresh: dict[PairWithTimeframe, float] = {}
self._klines_last_refresh: dict[PairWithTimeframe, float] = {}
self._klines_last_request: dict[PairWithTimeframe, float] = {}
self._thread = Thread(name="ccxt_ws", target=self._start_forever)
self._thread.start()
@@ -104,7 +104,7 @@ class ExchangeWS:
"""
with self._state_lock:
self._ccxt_object.ohlcvs.get(paircomb[0], {}).pop(paircomb[1], None)
self.klines_last_refresh.pop(paircomb, None)
self._klines_last_refresh.pop(paircomb, None)
@retrier(retries=3)
def ohlcvs(self, pair: str, timeframe: str) -> list[list]:
@@ -128,7 +128,7 @@ class ExchangeWS:
"""
ohlcvs = self.ohlcvs(pair, timeframe)
with self._state_lock:
last_refresh = self.klines_last_refresh.get((pair, timeframe, candle_type), 0)
last_refresh = self._klines_last_refresh.get((pair, timeframe, candle_type), 0)
return ohlcvs, last_refresh
def cleanup_expired(self) -> None:
@@ -227,7 +227,7 @@ class ExchangeWS:
start = dt_ts()
data = await self._ccxt_object.watch_ohlcv(pair, timeframe)
with self._state_lock:
self.klines_last_refresh[(pair, timeframe, candle_type)] = dt_ts()
self._klines_last_refresh[(pair, timeframe, candle_type)] = dt_ts()
logger.debug(
f"watch done {pair}, {timeframe}, data {len(data)} "
f"in {(dt_ts() - start) / 1000:.3f}s"
+6 -6
View File
@@ -26,7 +26,7 @@ def test_exchangews_init(mocker):
assert exchange_ws._background_tasks == set()
assert exchange_ws._klines_watching == set()
assert exchange_ws._klines_scheduled == set()
assert exchange_ws.klines_last_refresh == {}
assert exchange_ws._klines_last_refresh == {}
assert exchange_ws._klines_last_request == {}
# Cleanup
exchange_ws.cleanup()
@@ -258,7 +258,7 @@ async def test_exchangews_get_ohlcv(mocker, caplog):
mocker.patch("freqtrade.exchange.exchange_ws.ExchangeWS._start_forever", MagicMock())
exchange_ws = ExchangeWS(config, ccxt_object)
exchange_ws.klines_last_refresh = {
exchange_ws._klines_last_refresh = {
("ETH/USDT", "1m", CandleType.SPOT): 1635840120000,
("ETH/USDT", "5m", CandleType.SPOT): 1635840600000,
}
@@ -287,7 +287,7 @@ async def test_exchangews_get_ohlcv(mocker, caplog):
# Change "received" times to be before the candle starts.
# This should trigger the "time sync" warning.
exchange_ws.klines_last_refresh = {
exchange_ws._klines_last_refresh = {
("ETH/USDT", "1m", CandleType.SPOT): 1635840110000,
("ETH/USDT", "5m", CandleType.SPOT): 1635840600000,
}
@@ -323,7 +323,7 @@ async def test_exchangews_get_ohlcv_missing_refresh_date(mocker, caplog):
mocker.patch("freqtrade.exchange.exchange_ws.ExchangeWS._start_forever", MagicMock())
exchange_ws = ExchangeWS(config, ccxt_object)
exchange_ws.klines_last_refresh = {}
exchange_ws._klines_last_refresh = {}
# No refresh-date entry should not raise KeyError.
resp = await exchange_ws.get_ohlcv("ETH/USDT", "1m", CandleType.SPOT, 1635840120000)
@@ -355,7 +355,7 @@ def test_exchangews_continuous_stopped_task_exception(mocker, caplog):
paircomb = ("ETH/USDT", "1m", CandleType.SPOT)
exchange_ws._klines_scheduled.add(paircomb)
exchange_ws.klines_last_refresh[paircomb] = 1
exchange_ws._klines_last_refresh[paircomb] = 1
task = MagicMock()
task.cancelled.return_value = False
@@ -378,7 +378,7 @@ def test_exchangews_continuous_stopped_task_exception(mocker, caplog):
assert task not in exchange_ws._background_tasks
assert paircomb not in exchange_ws._klines_scheduled
assert paircomb not in exchange_ws.klines_last_refresh
assert paircomb not in exchange_ws._klines_last_refresh
assert ccxt_object.ohlcvs["ETH/USDT"].get("1m") is None
assert run_threadsafe.call_count == 1
assert log_has_re("Unhandled exception in watch task callback for ETH/USDT, 1m", caplog)