Skip to content

Commit a66c555

Browse files
committed
adding retry logic for all requests.exceptions
adding an exponential backoff with jitter on POST calls reducing the number of retries in some of the tests to comply with the backoff feature. pass through all requests.exceptions after the permitted number of retries.
1 parent c9a1b86 commit a66c555

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

influxdb/client.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from __future__ import unicode_literals
88

99
from sys import version_info
10+
import time
11+
import random
1012

1113
import json
1214
import socket
@@ -237,6 +239,7 @@ def request(self, url, method='GET', params=None, data=None,
237239
_try = 0
238240
while retry:
239241
try:
242+
_error = False
240243
response = self._session.request(
241244
method=method,
242245
url=url,
@@ -249,20 +252,27 @@ def request(self, url, method='GET', params=None, data=None,
249252
timeout=self._timeout
250253
)
251254
break
252-
except requests.exceptions.ConnectionError:
255+
except (requests.exceptions.ConnectionError,
256+
requests.exceptions.HTTPError,
257+
requests.exceptions.Timeout) as _e:
258+
_error = _e
253259
_try += 1
254260
if self._retries != 0:
255261
retry = _try < self._retries
256-
257-
else:
258-
raise requests.exceptions.ConnectionError
259-
260-
if 500 <= response.status_code < 600:
261-
raise InfluxDBServerError(response.content)
262-
elif response.status_code == expected_response_code:
263-
return response
262+
if method == "POST":
263+
time.sleep((2 ** _try) * random.random() / 100.0)
264+
if _error:
265+
raise(_error)
264266
else:
265-
raise InfluxDBClientError(response.content, response.status_code)
267+
# if there's not an error, there must have been a successful
268+
# response
269+
if 500 <= response.status_code < 600:
270+
raise InfluxDBServerError(response.content)
271+
elif response.status_code == expected_response_code:
272+
return response
273+
else:
274+
raise InfluxDBClientError(response.content,
275+
response.status_code)
266276

267277
def write(self, data, params=None, expected_response_code=204,
268278
protocol='json'):

influxdb/tests/client_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ def connection_error(self, *args, **kwargs):
686686

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

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

700700
if self.i < 4:
701-
raise requests.exceptions.ConnectionError
701+
raise requests.exceptions.HTTPError
702702
else:
703703
r = requests.Response()
704704
r.status_code = 200
@@ -708,7 +708,7 @@ def connection_error(self, *args, **kwargs):
708708

709709
cli = InfluxDBClient(database='db')
710710

711-
with self.assertRaises(requests.exceptions.ConnectionError):
711+
with self.assertRaises(requests.exceptions.HTTPError):
712712
cli.write_points(self.dummy_points)
713713

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

735-
retries = random.randint(1, 100)
735+
retries = random.randint(1, 5)
736736
mock_request.side_effect = CustomMock(retries).connection_error
737737

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

762-
retries = random.randint(1, 100)
762+
retries = random.randint(1, 5)
763763
mock_request.side_effect = CustomMock(retries).connection_error
764764

765765
cli = InfluxDBClient(database='db', retries=retries)

0 commit comments

Comments
 (0)