Skip to content

Commit

Permalink
improve timeout handling
Browse files Browse the repository at this point in the history
the timeout kwarg previously was only being used for polling timeout. we
can also pass it as a low kwarg and use it for urllib connection timeout
as well.
  • Loading branch information
mattp- committed Jul 30, 2019
1 parent 1890753 commit 07d6fc2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pepper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,8 @@ def run(self):
api = pepper.Pepper(
self.parse_url(),
debug_http=self.options.debug_http,
ignore_ssl_errors=self.options.ignore_ssl_certificate_errors)
ignore_ssl_errors=self.options.ignore_ssl_certificate_errors,
timeout=self.options.timeout)

self.login(api)

Expand Down
13 changes: 9 additions & 4 deletions pepper/libpepper.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Pepper(object):
u'ms-4': True}]}
'''
def __init__(self, api_url='https://localhost:8000', debug_http=False, ignore_ssl_errors=False):
def __init__(self, api_url='https://localhost:8000', debug_http=False, ignore_ssl_errors=False, timeout=None):
'''
Initialize the class with the URL of the API
Expand All @@ -81,6 +81,7 @@ def __init__(self, api_url='https://localhost:8000', debug_http=False, ignore_ss
self._ssl_verify = not ignore_ssl_errors
self.auth = {}
self.salt_version = None
self.timeout = timeout

def req_stream(self, path):
'''
Expand Down Expand Up @@ -224,11 +225,13 @@ def req(self, path, data=None):

# Send request
try:
con_kwargs = {}
if not (self._ssl_verify):
con = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
f = urlopen(req, context=con)
else:
f = urlopen(req)
con_kwargs['context'] = con
if self.timeout:
con_kwargs['timeout'] = self.timeout
f = urlopen(req, **con_kwargs)
content = f.read().decode('utf-8')
if (self.debug_http):
logger.debug('Response: %s', content)
Expand Down Expand Up @@ -283,6 +286,8 @@ def req_requests(self, path, data=None):
'auth': auth,
'data': json.dumps(data),
}
if self.timeout:
params['timeout'] = self.timeout
logger.debug('postdata {0}'.format(params))
resp = requests.post(**params)
if resp.status_code == 401:
Expand Down

0 comments on commit 07d6fc2

Please sign in to comment.