Skip to content

Commit 8b25b71

Browse files
Fixed Github Issue 515
1 parent cb3e7ff commit 8b25b71

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

segment/analytics/consumer.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
# lower to leave space for extra data that will be added later, eg. "sentAt".
1515
BATCH_SIZE_LIMIT = 475000
1616

17+
1718
class FatalError(Exception):
1819
def __init__(self, message):
1920
self.message = message
21+
2022
def __str__(self):
2123
msg = "[Segment] {0})"
2224
return msg.format(self.message)
@@ -81,7 +83,7 @@ def upload(self):
8183
# mark items as acknowledged from queue
8284
for _ in batch:
8385
self.queue.task_done()
84-
return success
86+
return success
8587

8688
def next(self):
8789
"""Return the next batch of items to upload."""
@@ -132,14 +134,26 @@ def fatal_exception(exc):
132134
# retry on all other errors (eg. network)
133135
return False
134136

137+
attempt_count = 0
138+
135139
@backoff.on_exception(
136140
backoff.expo,
137141
Exception,
138142
max_tries=self.retries + 1,
139-
giveup=fatal_exception)
143+
giveup=fatal_exception,
144+
on_backoff=lambda details: self.log.debug(
145+
f"Retry attempt {details['tries']}/{self.retries + 1} after {details['elapsed']:.2f}s"
146+
))
140147
def send_request():
141-
post(self.write_key, self.host, gzip=self.gzip,
142-
timeout=self.timeout, batch=batch, proxies=self.proxies,
143-
oauth_manager=self.oauth_manager)
148+
nonlocal attempt_count
149+
attempt_count += 1
150+
try:
151+
return post(self.write_key, self.host, gzip=self.gzip,
152+
timeout=self.timeout, batch=batch, proxies=self.proxies,
153+
oauth_manager=self.oauth_manager)
154+
except Exception as e:
155+
if attempt_count > self.retries:
156+
self.log.error(f"All {self.retries} retries exhausted. Final error: {e}")
157+
raise
144158

145159
send_request()

segment/analytics/request.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ def post(write_key, host=None, gzip=False, timeout=15, proxies=None, oauth_manag
5454
try:
5555
res = _session.post(url, **kwargs)
5656
except Exception as e:
57-
log.error(e)
57+
# log.error(e)
5858
raise e
59-
59+
6060
if res.status_code == 200:
6161
log.debug('data uploaded successfully')
6262
return res

0 commit comments

Comments
 (0)