Skip to content

Commit a3c3671

Browse files
phacopsclaude
andcommitted
ref(ratelimiter): Simplify exception handling flow
Move success return inside try block and remove finally/condition. The error return is now reached only when an exception occurs. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 14618be commit a3c3671

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

snuba/state/rate_limit.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,6 @@ def rate_limit_start_request(
259259
bucket = _get_bucket_key(rate_limit_prefix, rate_limit_params.bucket, shard_i)
260260
pipe.zcount(bucket, "({:f}".format(now), "+inf")
261261

262-
historical = -1
263-
concurrent = -1
264262
try:
265263
results = pipe.execute()
266264
pipe_results = iter(results)
@@ -279,6 +277,9 @@ def rate_limit_start_request(
279277
concurrent = sum(next(pipe_results) for _ in range(rate_limit_shard_factor))
280278
else:
281279
concurrent = 0
280+
281+
per_second = historical / float(state.rate_lookback_s)
282+
return RateLimitStats(rate=per_second, concurrent=concurrent)
282283
except RedisTimeoutError:
283284
# Emit metric for timeout, but don't log since this is expected
284285
# when Redis is slow. We fail open to avoid blocking requests.
@@ -287,14 +288,9 @@ def rate_limit_start_request(
287288
# if something goes wrong, we don't want to block the request,
288289
# set the values such that they pass under any limit
289290
logger.exception(ex)
290-
finally:
291-
if historical == -1:
292-
return RateLimitStats(rate=-1, concurrent=-1)
293-
294-
per_second = historical / float(state.rate_lookback_s)
295291

296-
stats = RateLimitStats(rate=per_second, concurrent=concurrent)
297-
return stats
292+
# Return fail-open stats if an exception occurred
293+
return RateLimitStats(rate=-1, concurrent=-1)
298294

299295

300296
def rate_limit_finish_request(

0 commit comments

Comments
 (0)