Skip to content

Commit b98c208

Browse files
author
Nhi Nguyen
committed
Don't retry sending on client errors except 429
1 parent a1b1489 commit b98c208

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Diff for: analytics/consumer.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from threading import Thread
33

44
from analytics.version import VERSION
5-
from analytics.request import post
5+
from analytics.request import post, APIError
66

77
try:
88
from queue import Empty
@@ -80,7 +80,19 @@ def request(self, batch, attempt=0):
8080
"""Attempt to upload the batch and retry before raising an error """
8181
try:
8282
post(self.write_key, self.host, batch=batch)
83-
except:
84-
if attempt > self.retries:
85-
raise
86-
self.request(batch, attempt+1)
83+
except Exception as exc:
84+
def maybe_retry():
85+
if attempt > self.retries:
86+
raise
87+
self.request(batch, attempt+1)
88+
89+
if isinstance(exc, APIError):
90+
if exc.status >= 500 or exc.status == 429:
91+
# retry on server errors and client errors with 429 status code (rate limited)
92+
maybe_retry()
93+
elif exc.status >= 400: # don't retry on other client errors
94+
self.log.error('API error: %s', exc)
95+
else:
96+
self.log.debug('Unexpected APIError: %s', exc)
97+
else: # retry on all other errors (eg. network)
98+
maybe_retry()

Diff for: e2e_test.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)