Skip to content
This repository was archived by the owner on Jul 21, 2021. It is now read-only.

Commit 52df76e

Browse files
LS-5330: Add connection timeout to HTTP
- Also streamlined handling of exceptions to ensure we're capturing the appropriate ones for HTTP requests
1 parent 2a5db5a commit 52df76e

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

lightstep/http_connection.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66

77
from lightstep.collector_pb2 import ReportResponse
88

9-
CONSECUTIVE_ERRORS_BEFORE_RECONNECT = 200
10-
119

1210
class _HTTPConnection(object):
1311
"""Instances of _Connection are used to establish a connection to the
1412
server via HTTP protocol.
1513
"""
16-
def __init__(self, collector_url):
14+
def __init__(self, collector_url, timeout_seconds):
1715
self._collector_url = collector_url
1816
self._lock = threading.Lock()
1917
self.ready = True
20-
self._report_eof_count = 0
21-
self._report_consecutive_errors = 0
18+
self._timeout_seconds = timeout_seconds
2219

2320
def open(self):
2421
"""Establish HTTP connection to the server.
@@ -28,9 +25,6 @@ def open(self):
2825
# May throw an Exception on failure.
2926
def report(self, *args, **kwargs):
3027
"""Report to the server."""
31-
# Notice the annoying case change on the method name. I chose to stay
32-
# consistent with casing in this class vs staying consistent with the
33-
# casing of the pass-through method.
3428
auth = args[0]
3529
report = args[1]
3630
with self._lock:
@@ -39,22 +33,16 @@ def report(self, *args, **kwargs):
3933
headers = {"Content-Type": "application/octet-stream",
4034
"Accept": "application/octet-stream"}
4135

42-
r = requests.post(self._collector_url, headers=headers, data=report.SerializeToString())
36+
r = requests.post(
37+
self._collector_url,
38+
headers=headers,
39+
data=report.SerializeToString(),
40+
timeout=self._timeout_seconds)
4341
resp = ReportResponse()
4442
resp.ParseFromString(r.content)
45-
self._report_consecutive_errors = 0
4643
return resp
47-
except EOFError:
48-
self._report_consecutive_errors += 1
49-
self._report_eof_count += 1
50-
raise Exception('EOFError')
51-
finally:
52-
# In case the client has fallen into an unrecoverable state,
53-
# recreate the data structure if there are continued report
54-
# failures
55-
if self._report_consecutive_errors == CONSECUTIVE_ERRORS_BEFORE_RECONNECT:
56-
self._report_consecutive_errors = 0
57-
self.ready = False
44+
except requests.exceptions.RequestException as err:
45+
raise err
5846

5947
def close(self):
6048
"""Close HTTP connection to the server."""

lightstep/recorder.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def __init__(self,
4545
verbosity=0,
4646
certificate_verification=True,
4747
use_thrift=False,
48-
use_http=True):
48+
use_http=True,
49+
timeout_seconds=30):
4950
self.verbosity = verbosity
5051
# Fail fast on a bad access token
5152
if not isinstance(access_token, str):
@@ -73,6 +74,7 @@ def __init__(self,
7374
collector_host,
7475
collector_port,
7576
self.use_thrift)
77+
self._timeout_seconds = timeout_seconds
7678
self._auth = self.converter.create_auth(access_token)
7779
self._mutex = threading.Lock()
7880
self._span_records = []
@@ -107,7 +109,7 @@ def _maybe_init_flush_thread(self):
107109
if self.use_thrift:
108110
self._flush_connection = _ThriftConnection(self._collector_url)
109111
else:
110-
self._flush_connection = _HTTPConnection(self._collector_url)
112+
self._flush_connection = _HTTPConnection(self._collector_url, self._timeout_seconds)
111113
self._flush_connection.open()
112114
self._flush_thread = threading.Thread(target=self._flush_periodically,
113115
name=constants.FLUSH_THREAD_NAME)

lightstep/tracer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def Tracer(**kwargs):
4949
configuration). Defaults to False (i.e., binary format is enabled).
5050
:param bool use_thrift: Forces the use of Thrift as the transport protocol.
5151
:param bool use_http: Forces the use of Proto over http.
52+
:param float timeout_seconds: Number of seconds allowed for the HTTP report transaction (fractions are permitted)
5253
"""
5354
enable_binary_format = True
5455
if 'disable_binary_format' in kwargs:

0 commit comments

Comments
 (0)