Skip to content

Commit e3785d2

Browse files
committed
Comment cleanup pass
Against the team convention: say why not what, no archaeology, keep a warning only where it stops someone undoing the thing it guards. - DEFAULT_MAX_RATE_LIMIT_DURATION: states the invariant as an instruction to whoever changes it next, and adds the second consequence of parity -- the cap stops binding, because the remaining budget is always the smaller term. - The drop branch: cut the rhetorical tail; the reason stands without it. - The episode-clearing comment: reordered so the hazard leads and the mechanism supports it, rather than the other way round. - "Same reasoning as above" now names the branch it refers to. - request.py: dropped a line that restated the constant in words, directly above the line that gives its reason. - test_client_defaults_to_the_documented_rate_limit_budget: rewritten in the present tense. It described how the bug had happened; it now states the constraint that makes asserting Consumer's default insufficient, which is what stops the test being "simplified" back. 140 passed, ruff and format clean.
1 parent 7873174 commit e3785d2

3 files changed

Lines changed: 17 additions & 19 deletions

File tree

‎segment/analytics/consumer.py‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class ShutdownInterrupted(Exception):
2525
# Default duration limits (12 hours in seconds)
2626
DEFAULT_MAX_TOTAL_BACKOFF_DURATION = 43200
2727
# Rate-limited attempts are deliberately uncounted, so this duration is the only
28-
# thing bounding them. It is deliberately several times MAX_RETRY_AFTER_SECONDS:
29-
# when the two are equal a single maximal Retry-After consumes the whole budget,
30-
# leaving one attempt and no retry at all.
28+
# thing bounding them. Keep it several times MAX_RETRY_AFTER_SECONDS: at parity a
29+
# single maximal Retry-After consumes the whole budget, leaving one attempt and no
30+
# retry, and the cap stops binding because the remaining budget is always smaller.
3131
DEFAULT_MAX_RATE_LIMIT_DURATION = 1800
3232

3333

@@ -173,11 +173,10 @@ def upload(self):
173173
remaining = self.max_rate_limit_duration - (now - self.rate_limit_start_time)
174174
wait_time = self.rate_limited_until - now
175175
if wait_time > remaining:
176-
# Shortening the wait to fit the budget would send the next request
177-
# inside the window the server asked us to wait out — a request it
178-
# has already said it will not serve — and the budget would then be
179-
# spent, so it would be the last one anyway. Give up here instead of
180-
# spending a request to be told the same thing.
176+
# Shortening the wait to fit sends the next request inside the
177+
# window the server asked us to wait out, which it has already said
178+
# it will not serve, and the budget is spent by then so it would be
179+
# the last attempt either way.
181180
self.log.error(
182181
"Rate limit budget (%ds) cannot accommodate the requested wait; dropping batch.",
183182
self.max_rate_limit_duration,
@@ -222,19 +221,19 @@ def upload(self):
222221
self._requeue(batch)
223222
success = False
224223
else:
225-
# The request completed and carried no rate-limit signal, so the
226-
# episode is over. Leaving the marker set strands it: this consumer
227-
# outlives the batch, upload() returns at the empty-batch guard
228-
# before the budget block, and nothing else clears it — so the next
229-
# batch to arrive after the budget elapses is dropped for a rate
230-
# limit that ended here, without ever being sent.
224+
# The request completed carrying no rate-limit signal, so the
225+
# episode is over. The marker outlives the batch and nothing else
226+
# clears it — upload() returns at the empty-batch guard above,
227+
# before the budget block — so leaving it set means the next batch
228+
# to arrive after the budget elapses is dropped for a rate limit
229+
# that ended here, without ever being sent.
231230
self.clear_rate_limit_state()
232231
self.log.error("error uploading: %s", e)
233232
success = False
234233
if self.on_error:
235234
self.on_error(e, batch)
236235
except Exception as e:
237-
# Same reasoning as above.
236+
# Same reasoning as the non-rate-limited APIError branch above.
238237
self.clear_rate_limit_state()
239238
self.log.error("error uploading: %s", e)
240239
success = False

‎segment/analytics/request.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
_session = sessions.Session()
1717

18-
# Maximum Retry-After delay to respect (5 minutes)
1918
# A guard against an absurd header, not a second budget. Waiting less than the
2019
# server asked for does not make the next attempt more likely to succeed, it just
2120
# sends more requests at something already rate-limiting us; how long we keep

‎segment/analytics/test/test_consumer.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,9 +1204,9 @@ def test_default_rate_limit_budget_exceeds_the_retry_after_cap(self):
12041204
def test_client_defaults_to_the_documented_rate_limit_budget(self):
12051205
"""Pins what a real caller gets, which is not the same as Consumer's default.
12061206
1207-
Client.DefaultConfig carried its own literal and Client passes it into every
1208-
Consumer it builds, so raising only the Consumer default left every real user
1209-
on the old value while this suite stayed green.
1207+
Client passes its own DefaultConfig value into every Consumer it builds, so
1208+
asserting Consumer's default here would stay green while a drifted Client
1209+
default shipped.
12101210
"""
12111211
client = Client("testsecret", send=False)
12121212
try:

0 commit comments

Comments
 (0)