From fbaf46901edc8747d58dafecd0b6df3680b203cd Mon Sep 17 00:00:00 2001 From: cdimauro Date: Sun, 26 Dec 2021 09:06:26 +0100 Subject: [PATCH] Reduce more KuCoin logs on retrier decorator More logs are reduced, for KuCoin, on the retrier_async decorator: _async_get_candle_history() returned exception retrying _async_get_candle_history() still for Giving up retrying: _async_get_candle_history() Applying DDosProtection backoff delay --- freqtrade/exchange/common.py | 19 +++++++++++++------ tests/exchange/test_exchange.py | 25 ++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/freqtrade/exchange/common.py b/freqtrade/exchange/common.py index d10682a61..68e454c79 100644 --- a/freqtrade/exchange/common.py +++ b/freqtrade/exchange/common.py @@ -9,6 +9,7 @@ from freqtrade.mixins import LoggingMixin logger = logging.getLogger(__name__) logging_mixin = LoggingMixin(logger) +log_once_info = partial(logging_mixin.log_once, logmethod=logger.info) log_once_warning = partial(logging_mixin.log_once, logmethod=logger.warning) @@ -75,16 +76,20 @@ def calculate_backoff(retrycount, max_retries): def retrier_async(f): async def wrapper(*args, **kwargs): count = kwargs.pop('count', API_RETRY_COUNT) + self = args[0] # Extract the exchange instance. + kucoin = self.name == "Kucoin" try: return await f(*args, **kwargs) except TemporaryError as ex: - logger.warning('%s() returned exception: "%s"', f.__name__, ex) + message = f'{f.__name__}() returned exception: "{ex}"' + (log_once_warning if kucoin else logger.warning)(message) if count > 0: - logger.warning('retrying %s() still for %s times', f.__name__, count) + message = f'retrying {f.__name__}() still for {count} times' + (log_once_warning if kucoin else logger.warning)(message) count -= 1 - kwargs.update({'count': count}) + kwargs['count'] = count if isinstance(ex, DDosProtection): - if "kucoin" in str(ex) and "429000" in str(ex): + if kucoin and "429000" in str(ex): # Temporary fix for 429000 error on kucoin # see https://github.com/freqtrade/freqtrade/issues/5700 for details. log_once_warning( @@ -92,11 +97,13 @@ def retrier_async(f): f"{count} tries left before giving up") else: backoff_delay = calculate_backoff(count + 1, API_RETRY_COUNT) - logger.info(f"Applying DDosProtection backoff delay: {backoff_delay}") + message = f"Applying DDosProtection backoff delay: {backoff_delay}" + (log_once_info if kucoin else logger.info)(message) await asyncio.sleep(backoff_delay) return await wrapper(*args, **kwargs) else: - logger.warning('Giving up retrying: %s()', f.__name__) + message = f'Giving up retrying: {f.__name__}()' + (log_once_warning if kucoin else logger.warning)(message) raise ex return wrapper diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 9bd419e24..4e37a6e4a 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -1757,7 +1757,30 @@ async def test__async_kucoin_get_candle_history(default_conf, mocker, caplog): exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kucoin") await exchange._async_get_candle_history( 'ETH/BTC', "5m", (arrow.utcnow().int_timestamp - 2000) * 1000, count=1) - logs_found = sum('Kucoin 429 error, avoid triggering DDosProtection backoff delay' in message + logs_found = sum('_async_get_candle_history() returned exception: "kucoin GET ' in message + for message in caplog.messages) + assert logs_found == 1 + logs_found = sum('retrying _async_get_candle_history() still for ' in message + for message in caplog.messages) + assert logs_found == 1 + logs_found = sum("Kucoin 429 error, avoid triggering DDosProtection backoff delay" in message + for message in caplog.messages) + assert logs_found == 1 + logs_found = sum(message == 'Giving up retrying: _async_get_candle_history()' + for message in caplog.messages) + assert logs_found == 1 + + mocker.patch('freqtrade.exchange.common.calculate_backoff', MagicMock(return_value=0.01)) + for _ in range(3): + with pytest.raises(DDosProtection, match=r'XYZ Too Many Requests'): + api_mock.fetch_ohlcv = MagicMock(side_effect=ccxt.DDoSProtection( + "kucoin GET https://openapi-v2.kucoin.com/api/v1/market/candles?" + "symbol=ETH-BTC&type=5min&startAt=1640268735&endAt=1640418735" + "XYZ Too Many Requests" '{"code":"XYZ000","msg":"Too Many Requests"}')) + exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kucoin") + await exchange._async_get_candle_history( + 'ETH/BTC', "5m", (arrow.utcnow().int_timestamp - 2000) * 1000, count=1) + logs_found = sum('Applying DDosProtection backoff delay: ' in message for message in caplog.messages) assert logs_found == 1