Enable optional timeouts for http get or post requests
This fixes our client from hanging on posts and gets Change-Id: I3ec01aa9562558847d784cbf4dce944507bee664
This commit is contained in:
parent
417eadc326
commit
5c63c639ce
@ -54,10 +54,12 @@ def get_system_ca_file():
|
|||||||
|
|
||||||
class HTTPClient(object):
|
class HTTPClient(object):
|
||||||
|
|
||||||
def __init__(self, endpoint, **kwargs):
|
def __init__(self, endpoint, write_timeout=None, read_timeout=None, **kwargs):
|
||||||
if endpoint.endswith('/'):
|
if endpoint.endswith('/'):
|
||||||
endpoint = endpoint[:-1]
|
endpoint = endpoint[:-1]
|
||||||
self.endpoint = endpoint
|
self.endpoint = endpoint
|
||||||
|
self.write_timeout = write_timeout
|
||||||
|
self.read_timeout = read_timeout
|
||||||
self.auth_url = kwargs.get('auth_url')
|
self.auth_url = kwargs.get('auth_url')
|
||||||
self.auth_token = kwargs.get('token')
|
self.auth_token = kwargs.get('token')
|
||||||
self.username = kwargs.get('username')
|
self.username = kwargs.get('username')
|
||||||
@ -200,10 +202,16 @@ class HTTPClient(object):
|
|||||||
allow_redirects = False
|
allow_redirects = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
timeout = None
|
||||||
|
if method in ['POST', 'DELETE', 'PUT', 'PATCH']:
|
||||||
|
timeout = self.write_timeout
|
||||||
|
elif method is 'GET':
|
||||||
|
timeout = self.read_timeout
|
||||||
resp = requests.request(
|
resp = requests.request(
|
||||||
method,
|
method,
|
||||||
self.endpoint_url + url,
|
self.endpoint_url + url,
|
||||||
allow_redirects=allow_redirects,
|
allow_redirects=allow_redirects,
|
||||||
|
timeout=timeout,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
except socket.gaierror as e:
|
except socket.gaierror as e:
|
||||||
message = ("Error finding address for %(url)s: %(e)s" %
|
message = ("Error finding address for %(url)s: %(e)s" %
|
||||||
@ -214,6 +222,11 @@ class HTTPClient(object):
|
|||||||
message = ("Error communicating with %(endpoint)s %(e)s" %
|
message = ("Error communicating with %(endpoint)s %(e)s" %
|
||||||
{'endpoint': endpoint, 'e': e})
|
{'endpoint': endpoint, 'e': e})
|
||||||
raise exc.CommunicationError(message=message)
|
raise exc.CommunicationError(message=message)
|
||||||
|
except requests.Timeout as e:
|
||||||
|
endpoint = self.endpoint
|
||||||
|
message = ("Error %(method)s timeout request to %(endpoint)s %(e)s" %
|
||||||
|
{'method': method, 'endpoint': endpoint, 'e': e})
|
||||||
|
raise exc.RequestTimeoutError(message=message)
|
||||||
|
|
||||||
self.log_http_response(resp)
|
self.log_http_response(resp)
|
||||||
|
|
||||||
|
@ -49,6 +49,11 @@ class CommunicationError(BaseException):
|
|||||||
"""Unable to communicate with server."""
|
"""Unable to communicate with server."""
|
||||||
|
|
||||||
|
|
||||||
|
class RequestTimeoutError(BaseException):
|
||||||
|
|
||||||
|
"""Timeout making a POST, GET, PATCH, DELETE, or PUT request to the server."""
|
||||||
|
|
||||||
|
|
||||||
class HTTPException(BaseException):
|
class HTTPException(BaseException):
|
||||||
|
|
||||||
"""Base exception for all HTTP-derived exceptions."""
|
"""Base exception for all HTTP-derived exceptions."""
|
||||||
|
Loading…
Reference in New Issue
Block a user