From c7c1e0b26821fdd5622f81fb456f1028d4c9ab09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sat, 24 Mar 2018 15:18:31 +0100 Subject: Add retry facility for api call timeouts Fixes https://git.immae.eu/mantisbt/view.php?id=40 --- ccxt_wrapper.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'ccxt_wrapper.py') diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index d37c306..c500659 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py @@ -1,12 +1,42 @@ from ccxt import * import decimal import time +from retry.api import retry_call +import re def _cw_exchange_sum(self, *args): return sum([arg for arg in args if isinstance(arg, (float, int, decimal.Decimal))]) Exchange.sum = _cw_exchange_sum class poloniexE(poloniex): + RETRIABLE_CALLS = [ + re.compile(r"^return"), + re.compile(r"^cancel"), + re.compile(r"^closeMarginPosition$"), + re.compile(r"^getMarginPosition$"), + ] + + def request(self, path, api='public', method='GET', params={}, headers=None, body=None): + """ + Wrapped to allow retry of non-posting requests" + """ + + origin_request = super(poloniexE, self).request + kwargs = { + "api": api, + "method": method, + "params": params, + "headers": headers, + "body": body + } + + retriable = any(re.match(call, path) for call in self.RETRIABLE_CALLS) + if api == "public" or method == "GET" or retriable: + return retry_call(origin_request, fargs=[path], fkwargs=kwargs, + tries=10, delay=1, exceptions=(RequestTimeout,)) + else: + return origin_request(path, **kwargs) + @staticmethod def nanoseconds(): return int(time.time() * 1000000000) -- cgit v1.2.3 From 445b4a7712fb7fe45e17b6b76356dd3be42dd900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sat, 24 Mar 2018 15:18:56 +0100 Subject: Move request wrapper to ccxt --- ccxt_wrapper.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'ccxt_wrapper.py') diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index c500659..4ed37d9 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py @@ -37,6 +37,21 @@ class poloniexE(poloniex): else: return origin_request(path, **kwargs) + def __init__(self, *args, **kwargs): + super(poloniexE, self).__init__(*args, **kwargs) + + # For requests logging + self.session.origin_request = self.session.request + self.session._parent = self + + def request_wrap(self, *args, **kwargs): + r = self.origin_request(*args, **kwargs) + self._parent._market.report.log_http_request(args[0], + args[1], kwargs["data"], kwargs["headers"], r) + return r + self.session.request = request_wrap.__get__(self.session, + self.session.__class__) + @staticmethod def nanoseconds(): return int(time.time() * 1000000000) -- cgit v1.2.3