Skip to content

Commit 16d829b

Browse files
Merge pull request #519 from segmentio/neelkanth-kaushik/LIBRARIES-2730
Merging Github Issue 515 and Github Issue 516 into single PR
2 parents aaf34b0 + 11e1edb commit 16d829b

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

segment/analytics/consumer.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,26 @@ def fatal_exception(exc):
134134
# retry on all other errors (eg. network)
135135
return False
136136

137+
attempt_count = 0
138+
137139
@backoff.on_exception(
138140
backoff.expo,
139141
Exception,
140142
max_tries=self.retries + 1,
141-
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+
))
142147
def send_request():
143-
post(self.write_key, self.host, gzip=self.gzip,
144-
timeout=self.timeout, batch=batch, proxies=self.proxies,
145-
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 + 1:
156+
self.log.error(f"All {self.retries} retries exhausted. Final error: {e}")
157+
raise
146158

147159
send_request()

segment/analytics/request.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ 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)
5857
raise e
59-
58+
6059
if res.status_code == 200:
6160
log.debug('data uploaded successfully')
6261
return res

0 commit comments

Comments
 (0)