Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Enable retries in the std.py backend. #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions consul/std.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import requests
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter

from consul import base

Expand All @@ -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):
Expand Down Expand Up @@ -61,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):
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)