Also retry on connection refused

This is required to completely replace ClientWrapper in nova driver.

Change-Id: Ie98a8929c66ccabfd1a6b60f03ce9d39133ef634
This commit is contained in:
Dmitry Tantsur 2015-07-27 14:32:28 +02:00
parent 5796612d04
commit e4a2fcfdb1
2 changed files with 32 additions and 1 deletions

View File

@ -159,6 +159,10 @@ class VersionNegotiationMixin(object):
raise NotImplementedError() raise NotImplementedError()
_RETRY_EXCEPTIONS = (exc.Conflict, exc.ServiceUnavailable,
exc.ConnectionRefused)
def with_retries(func): def with_retries(func):
"""Wrapper for _http_request adding support for retries.""" """Wrapper for _http_request adding support for retries."""
@functools.wraps(func) @functools.wraps(func)
@ -172,7 +176,7 @@ def with_retries(func):
for attempt in range(1, num_attempts + 1): for attempt in range(1, num_attempts + 1):
try: try:
return func(self, url, method, **kwargs) return func(self, url, method, **kwargs)
except (exc.Conflict, exc.ServiceUnavailable) as error: except _RETRY_EXCEPTIONS as error:
msg = ("Error contacting Ironic server: %(error)s. " msg = ("Error contacting Ironic server: %(error)s. "
"Attempt %(attempt)d of %(total)d" % "Attempt %(attempt)d of %(total)d" %
{'attempt': attempt, {'attempt': attempt,

View File

@ -561,6 +561,20 @@ class RetriesTestCase(utils.BaseTestCase):
self.assertEqual(200, response.status) self.assertEqual(200, response.status)
self.assertEqual(2, mock_getcon.call_count) self.assertEqual(2, mock_getcon.call_count)
@mock.patch.object(http.HTTPClient, 'get_connection', autospec=True)
def test_http_retry_connection_refused(self, mock_getcon):
good_resp = utils.FakeResponse(
{'content-type': 'text/plain'},
six.StringIO("meow"),
version=1,
status=200)
client = http.HTTPClient('http://localhost/')
mock_getcon.side_effect = iter((exc.ConnectionRefused(),
utils.FakeConnection(good_resp)))
response, body_iter = client._http_request('/v1/resources', 'GET')
self.assertEqual(200, response.status)
self.assertEqual(2, mock_getcon.call_count)
@mock.patch.object(http.HTTPClient, 'get_connection', autospec=True) @mock.patch.object(http.HTTPClient, 'get_connection', autospec=True)
def test_http_failed_retry(self, mock_getcon): def test_http_failed_retry(self, mock_getcon):
error_body = _get_error_body() error_body = _get_error_body()
@ -640,6 +654,19 @@ class RetriesTestCase(utils.BaseTestCase):
client.json_request('GET', '/v1/resources') client.json_request('GET', '/v1/resources')
self.assertEqual(2, fake_session.request.call_count) self.assertEqual(2, fake_session.request.call_count)
def test_session_retry_connection_refused(self):
ok_resp = utils.FakeSessionResponse(
{'Content-Type': 'application/json'},
b"OK",
200)
fake_session = mock.Mock(spec=utils.FakeSession)
fake_session.request.side_effect = iter((exc.ConnectionRefused(),
ok_resp))
client = _session_client(session=fake_session)
client.json_request('GET', '/v1/resources')
self.assertEqual(2, fake_session.request.call_count)
def test_session_retry_fail(self): def test_session_retry_fail(self):
error_body = _get_error_body() error_body = _get_error_body()