Skip to content

Commit 5472d41

Browse files
author
aviau
committed
Now catch requests connection errors
1 parent 6ec779d commit 5472d41

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

influxdb/client.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import socket
77
import requests
8+
import requests.exceptions
89
import warnings
910

1011
from influxdb import chunked_json
@@ -158,15 +159,25 @@ def request(self, url, method='GET', params=None, data=None,
158159
if data is not None and not isinstance(data, str):
159160
data = json.dumps(data)
160161

161-
response = session.request(
162-
method=method,
163-
url=url,
164-
params=params,
165-
data=data,
166-
headers=self._headers,
167-
verify=self._verify_ssl,
168-
timeout=self._timeout
169-
)
162+
# Try to send the request a maximum of three times. (see #103)
163+
# TODO (aviau): Make this configurable.
164+
for i in range(0, 3):
165+
try:
166+
response = session.request(
167+
method=method,
168+
url=url,
169+
params=params,
170+
data=data,
171+
headers=self._headers,
172+
verify=self._verify_ssl,
173+
timeout=self._timeout
174+
)
175+
break
176+
except requests.exceptions.ConnectionError as e:
177+
if i < 2:
178+
continue
179+
else:
180+
raise e
170181

171182
if response.status_code == expected_response_code:
172183
return response

tests/influxdb/client_test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
"""
55
import json
66
import requests
7+
import requests.exceptions
78
import socket
89
import unittest
910
import requests_mock
1011
from nose.tools import raises
1112
from mock import patch
1213
import warnings
14+
import mock
1315

1416
from influxdb import InfluxDBClient
1517
from influxdb.client import session
@@ -640,3 +642,51 @@ def test_delete_database_user(self):
640642
def test_update_permission(self):
641643
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
642644
cli.update_permission('admin', [])
645+
646+
@mock.patch('requests.Session.request')
647+
def test_request_retry(self, mock_request):
648+
"""Tests that two connection errors will be handled"""
649+
650+
class CustomMock(object):
651+
i = 0
652+
653+
def connection_error(self, *args, **kwargs):
654+
self.i += 1
655+
656+
if self.i < 3:
657+
raise requests.exceptions.ConnectionError
658+
else:
659+
r = requests.Response()
660+
r.status_code = 200
661+
return r
662+
663+
mock_request.side_effect = CustomMock().connection_error
664+
665+
cli = InfluxDBClient(database='db')
666+
cli.write_points(
667+
self.dummy_points
668+
)
669+
670+
@mock.patch('requests.Session.request')
671+
def test_request_retry_raises(self, mock_request):
672+
"""Tests that three connection errors will not be handled"""
673+
674+
class CustomMock(object):
675+
i = 0
676+
677+
def connection_error(self, *args, **kwargs):
678+
self.i += 1
679+
680+
if self.i < 4:
681+
raise requests.exceptions.ConnectionError
682+
else:
683+
r = requests.Response()
684+
r.status_code = 200
685+
return r
686+
687+
mock_request.side_effect = CustomMock().connection_error
688+
689+
cli = InfluxDBClient(database='db')
690+
691+
with self.assertRaises(requests.exceptions.ConnectionError):
692+
cli.write_points(self.dummy_points)

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