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

adding retry logic for all requests.exceptions #508

Merged
merged 3 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
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
30 changes: 20 additions & 10 deletions influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from __future__ import unicode_literals

from sys import version_info
import time
import random

import json
import socket
Expand Down Expand Up @@ -237,6 +239,7 @@ def request(self, url, method='GET', params=None, data=None,
_try = 0
while retry:
try:
_error = False
response = self._session.request(
method=method,
url=url,
Expand All @@ -249,20 +252,27 @@ def request(self, url, method='GET', params=None, data=None,
timeout=self._timeout
)
break
except requests.exceptions.ConnectionError:
except (requests.exceptions.ConnectionError,
requests.exceptions.HTTPError,
requests.exceptions.Timeout) as _e:
_error = _e
_try += 1
if self._retries != 0:
retry = _try < self._retries

else:
raise requests.exceptions.ConnectionError

if 500 <= response.status_code < 600:
raise InfluxDBServerError(response.content)
elif response.status_code == expected_response_code:
return response
if method == "POST":
time.sleep((2 ** _try) * random.random() / 100.0)
if _error:
raise(_error)
else:
raise InfluxDBClientError(response.content, response.status_code)
# if there's not an error, there must have been a successful
# response
if 500 <= response.status_code < 600:
raise InfluxDBServerError(response.content)
elif response.status_code == expected_response_code:
return response
else:
raise InfluxDBClientError(response.content,
response.status_code)

def write(self, data, params=None, expected_response_code=204,
protocol='json'):
Expand Down
10 changes: 5 additions & 5 deletions influxdb/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ def connection_error(self, *args, **kwargs):

@mock.patch('requests.Session.request')
def test_request_retry_raises(self, mock_request):
"""Test that three connection errors will not be handled."""
"""Test that three requests errors will not be handled."""
class CustomMock(object):
"""Create custom mock object for test."""

Expand All @@ -698,7 +698,7 @@ def connection_error(self, *args, **kwargs):
self.i += 1

if self.i < 4:
raise requests.exceptions.ConnectionError
raise requests.exceptions.HTTPError
else:
r = requests.Response()
r.status_code = 200
Expand All @@ -708,7 +708,7 @@ def connection_error(self, *args, **kwargs):

cli = InfluxDBClient(database='db')

with self.assertRaises(requests.exceptions.ConnectionError):
with self.assertRaises(requests.exceptions.HTTPError):
cli.write_points(self.dummy_points)

@mock.patch('requests.Session.request')
Expand All @@ -732,7 +732,7 @@ def connection_error(self, *args, **kwargs):
r.status_code = 204
return r

retries = random.randint(1, 100)
retries = random.randint(1, 5)
mock_request.side_effect = CustomMock(retries).connection_error

cli = InfluxDBClient(database='db', retries=retries)
Expand All @@ -759,7 +759,7 @@ def connection_error(self, *args, **kwargs):
r.status_code = 200
return r

retries = random.randint(1, 100)
retries = random.randint(1, 5)
mock_request.side_effect = CustomMock(retries).connection_error

cli = InfluxDBClient(database='db', retries=retries)
Expand Down