From 74c5136a511ec3740a038a27836b43ea28ea3663 Mon Sep 17 00:00:00 2001 From: gzhytar Date: Sun, 12 Nov 2017 15:02:09 +0100 Subject: [PATCH 1/2] Implementing simple mechanism for retrying failed requests --- krakenex/api.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/krakenex/api.py b/krakenex/api.py index b5177b2..82eef07 100644 --- a/krakenex/api.py +++ b/krakenex/api.py @@ -27,6 +27,7 @@ import hashlib import hmac import base64 +import logging from . import version @@ -109,20 +110,34 @@ def _query(self, urlpath, data, headers=None): :raises: :py:exc:`requests.HTTPError`: if response status not successful """ + logger = logging.getLogger() + if data is None: data = {} if headers is None: headers = {} - + url = self.uri + urlpath - self.response = self.session.post(url, data = data, headers = headers) - - if self.response.status_code not in (200, 201, 202): - self.response.raise_for_status() - - return self.response.json() - + max_retries = 3 + attempt = 1 + + # Retries mechanism for certain HTTP codes. + # Kraken is behind CloudFlare which adds to network requests instability during peaks + # Careful! Sometimes service returns error code but actuallu executes a request + # needs investigation if this can cause a multiple buys/sells (don't think so as there is nonce in each request ) + while attempt<=max_retries: + self.response = self.session.post(url, data = data, headers = headers) + + if self.response.status_code in (200, 201, 202): + return self.response.json() + elif self.response.status_code in (504, 520) and attempt Date: Sun, 12 Nov 2017 15:37:27 +0100 Subject: [PATCH 2/2] Connection: Adding an attribute to configure a number of connection recovery attempts --- krakenex/api.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/krakenex/api.py b/krakenex/api.py index 82eef07..e0b8c0a 100644 --- a/krakenex/api.py +++ b/krakenex/api.py @@ -67,6 +67,8 @@ def __init__(self, key='', secret=''): 'User-Agent': 'krakenex/' + version.__version__ + ' (+' + version.__url__ + ')' }) self.response = None + # How many times we try to recover from bad HTTP connection situation + self.bad_http_connection_retries = 3 return def close(self): @@ -119,22 +121,19 @@ def _query(self, urlpath, data, headers=None): url = self.uri + urlpath - max_retries = 3 - attempt = 1 - # Retries mechanism for certain HTTP codes. # Kraken is behind CloudFlare which adds to network requests instability during peaks # Careful! Sometimes service returns error code but actuallu executes a request # needs investigation if this can cause a multiple buys/sells (don't think so as there is nonce in each request ) - while attempt<=max_retries: + attempt = 1 + while attempt<=self.bad_http_connection_retries: self.response = self.session.post(url, data = data, headers = headers) if self.response.status_code in (200, 201, 202): return self.response.json() - elif self.response.status_code in (504, 520) and attempt