Skip to content

Commit 194ef13

Browse files
committed
Allow user-configurable number of retries when submitting request to database
1 parent 11c71f2 commit 194ef13

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

influxdb/client.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class InfluxDBClient(object):
5353
:param timeout: number of seconds Requests will wait for your client to
5454
establish a connection, defaults to None
5555
:type timeout: int
56+
:param retries: number of retries your client will try before aborting,
57+
defaults to 3. 0 indicates try until success
58+
:type retries: int
5659
:param use_udp: use UDP to connect to InfluxDB, defaults to False
5760
:type use_udp: bool
5861
:param udp_port: UDP port to connect to InfluxDB, defaults to 4444
@@ -70,6 +73,7 @@ def __init__(self,
7073
ssl=False,
7174
verify_ssl=False,
7275
timeout=None,
76+
retries=3,
7377
use_udp=False,
7478
udp_port=4444,
7579
proxies=None,
@@ -81,6 +85,7 @@ def __init__(self,
8185
self._password = password
8286
self._database = database
8387
self._timeout = timeout
88+
self._retries = retries
8489

8590
self._verify_ssl = verify_ssl
8691

@@ -225,9 +230,10 @@ def request(self, url, method='GET', params=None, data=None,
225230
if isinstance(data, (dict, list)):
226231
data = json.dumps(data)
227232

228-
# Try to send the request a maximum of three times. (see #103)
229-
# TODO (aviau): Make this configurable.
230-
for i in range(0, 3):
233+
# Try to send the request more than once by default (see #103)
234+
retry = True
235+
_try = 0
236+
while retry:
231237
try:
232238
response = self._session.request(
233239
method=method,
@@ -242,10 +248,9 @@ def request(self, url, method='GET', params=None, data=None,
242248
)
243249
break
244250
except requests.exceptions.ConnectionError as e:
245-
if i < 2:
246-
continue
247-
else:
248-
raise e
251+
_try += 1
252+
if self._retries != 0:
253+
retry = _try < self._retries
249254

250255
if 500 <= response.status_code < 600:
251256
raise InfluxDBServerError(response.content)

influxdb/influxdb08/client.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class InfluxDBClient(object):
5555
:param verify_ssl: verify SSL certificates for HTTPS requests, defaults is
5656
False
5757
:type verify_ssl: boolean
58+
:param retries: number of retries your client will try before aborting,
59+
defaults to 3. 0 indicates try until success
60+
:type retries: int
5861
:param timeout: number of seconds Requests will wait for your client to
5962
establish a connection, defaults to None
6063
:type timeout: int
@@ -73,6 +76,7 @@ def __init__(self,
7376
ssl=False,
7477
verify_ssl=False,
7578
timeout=None,
79+
retries=3,
7680
use_udp=False,
7781
udp_port=4444):
7882
"""
@@ -84,6 +88,7 @@ def __init__(self,
8488
self._password = password
8589
self._database = database
8690
self._timeout = timeout
91+
self._retries = retries
8792

8893
self._verify_ssl = verify_ssl
8994

@@ -228,10 +233,11 @@ def request(self, url, method='GET', params=None, data=None,
228233
if data is not None and not isinstance(data, str):
229234
data = json.dumps(data)
230235

231-
# Try to send the request a maximum of three times. (see #103)
232-
# TODO (aviau): Make this configurable.
233-
for i in range(0, 3):
234-
try:
236+
retry = True
237+
_try = 0
238+
# Try to send the request more than once by default (see #103)
239+
while retry:
240+
try:
235241
response = session.request(
236242
method=method,
237243
url=url,
@@ -244,10 +250,9 @@ def request(self, url, method='GET', params=None, data=None,
244250
break
245251
except (requests.exceptions.ConnectionError,
246252
requests.exceptions.Timeout) as e:
247-
if i < 2:
248-
continue
249-
else:
250-
raise e
253+
_try += 1
254+
if self._retries != 0:
255+
retry = _try < self._retries
251256

252257
if response.status_code == expected_response_code:
253258
return response

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy