From c4ebfc50a279b15b436f1ed6d2552b39e87e12b4 Mon Sep 17 00:00:00 2001 From: Shaheed Haque Date: Sat, 20 Feb 2021 18:05:29 +0000 Subject: [PATCH 1/2] Resolves #30. Enable retries in the std.py backend. This PR also: - Replaces the deprecated call to requests.session() with its current counterpart, requests.Session(). - Allows arbitrary connection kwargs to be passed down, not least to allow the new retry behaviour to be overridden. --- consul/std.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/consul/std.py b/consul/std.py index 224b6fe..2a138f0 100644 --- a/consul/std.py +++ b/consul/std.py @@ -1,4 +1,6 @@ import requests +from urllib3.util.retry import Retry +from requests.adapters import HTTPAdapter from consul import base @@ -7,8 +9,16 @@ class HTTPClient(base.HTTPClient): def __init__(self, *args, **kwargs): + """ + Connect using a default retry policy. This can be disabled by setting + retries=None. + """ + retries = kwargs.pop('retries', Retry(total=5, backoff_factor=0.2)) super(HTTPClient, self).__init__(*args, **kwargs) - self.session = requests.session() + self.session = requests.Session() + if retries: + self.session.mount('http://', HTTPAdapter(max_retries=retries)) + self.session.mount('https://', HTTPAdapter(max_retries=retries)) @staticmethod def response(response): @@ -61,5 +71,5 @@ def post(self, callback, path, params=None, headers=None, data=''): class Consul(base.Consul): @staticmethod - def http_connect(host, port, scheme, verify=True, cert=None, timeout=None): - return HTTPClient(host, port, scheme, verify, cert, timeout) + def http_connect(host, port, scheme, verify=True, cert=None, timeout=None, **kwargs): + return HTTPClient(host, port, scheme, verify, cert, timeout, **kwargs) From 8622b26f52667e3fe5a05d13e2df595bf397187e Mon Sep 17 00:00:00 2001 From: Shaheed Haque Date: Sat, 20 Feb 2021 19:42:01 +0000 Subject: [PATCH 2/2] Correct line length to keep flake8 happy. --- consul/std.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/consul/std.py b/consul/std.py index 2a138f0..3fa112f 100644 --- a/consul/std.py +++ b/consul/std.py @@ -71,5 +71,6 @@ def post(self, callback, path, params=None, headers=None, data=''): class Consul(base.Consul): @staticmethod - def http_connect(host, port, scheme, verify=True, cert=None, timeout=None, **kwargs): + def http_connect(host, port, scheme, verify=True, cert=None, timeout=None, + **kwargs): return HTTPClient(host, port, scheme, verify, cert, timeout, **kwargs)