Skip to content

Commit

Permalink
Implement connection retries
Browse files Browse the repository at this point in the history
  • Loading branch information
dagwieers committed Oct 8, 2018
1 parent 1e0b20c commit f8a5e35
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions pypsrp/wsman.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def __init__(self, server, max_envelope_size=153600, operation_timeout=20,
path="wsman", auth="negotiate", cert_validation=True,
connection_timeout=30, encryption='auto', proxy=None,
no_proxy=False, locale='en-US', data_locale=None,
reconnection_retries=4, reconnection_backoff=2.0,
**kwargs):
"""
Class that handles WSMan transport over HTTP. This exposes a method per
Expand Down Expand Up @@ -194,6 +195,11 @@ def __init__(self, server, max_envelope_size=153600, operation_timeout=20,
building the server SPN
negotiate_service: Override the service used when building the
server SPN, default='WSMAN'
:param int reconnection_retries: Number of retries on connection
problems
:param float reconnection_backoff: Number of seconds to backoff in
between reconnection attempts (first sleeps X, then sleeps 2*X,
4*X, 8*X, ...)
"""
log.info("Initialising WSMan class with maximum envelope size of %d "
"and operation timeout of %s"
Expand All @@ -206,7 +212,8 @@ def __init__(self, server, max_envelope_size=153600, operation_timeout=20,
self.transport = _TransportHTTP(server, port, username, password, ssl,
path, auth, cert_validation,
connection_timeout, encryption, proxy,
no_proxy, **kwargs)
no_proxy, reconnection_retries,
reconnection_backoff, **kwargs)
self.max_envelope_size = max_envelope_size
self.operation_timeout = operation_timeout

Expand Down Expand Up @@ -621,7 +628,8 @@ class _TransportHTTP(object):
def __init__(self, server, port=None, username=None, password=None,
ssl=True, path="wsman", auth="negotiate",
cert_validation=True, connection_timeout=30,
encryption='auto', proxy=None, no_proxy=False, **kwargs):
encryption='auto', proxy=None, no_proxy=False,
reconnection_retries=4, reconnection_backoff=2.0, **kwargs):
self.server = server
self.port = port if port is not None else (5986 if ssl else 5985)
self.username = username
Expand All @@ -636,6 +644,8 @@ def __init__(self, server, port=None, username=None, password=None,
self.auth = auth
self.cert_validation = cert_validation
self.connection_timeout = connection_timeout
self.reconnection_retries = reconnection_retries
self.reconnection_backoff = reconnection_backoff

# determine the message encryption logic
if encryption not in ["auto", "always", "never"]:
Expand Down Expand Up @@ -773,6 +783,22 @@ def _build_session(self):
elif self.no_proxy:
session.proxies = orig_proxy

# Retry on connection errors, with a backoff factor
retries = requests.packages.urllib3.util.retry.Retry(
total=self.reconnection_retries,
connect=self.reconnection_retries,
status=self.reconnection_retries,
read=0,
backoff_factor=self.reconnection_backoff,
status_forcelist=[413, 425, 429, 503],
)
session.mount('http://', requests.adapters.HTTPAdapter(
max_retries=retries)
)
session.mount('https://', requests.adapters.HTTPAdapter(
max_retries=retries)
)

# set cert validation config
session.verify = self.cert_validation

Expand Down

0 comments on commit f8a5e35

Please sign in to comment.