Skip to content

Commit b0820d2

Browse files
authored
Reduce critical section in rate limiter code (#365)
1 parent c31e440 commit b0820d2

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

crates/shared/src/rate_limiter.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ impl RateLimiter {
182182
task: impl Future<Output = T>,
183183
requires_back_off: impl Fn(&T) -> bool,
184184
) -> Result<T, RateLimiterError> {
185-
let times_rate_limited = match self
185+
let times_rate_limited = self
186186
.strategy()
187-
.times_rate_limited(Instant::now(), &self.name)
188-
{
187+
.times_rate_limited(Instant::now(), &self.name);
188+
let times_rate_limited = match times_rate_limited {
189189
None => {
190190
tracing::warn!(?self.name, "dropping task because API is currently rate limited");
191191
return Err(RateLimiterError::RateLimited);
@@ -196,10 +196,10 @@ impl RateLimiter {
196196
let result = task.await;
197197

198198
if requires_back_off(&result) {
199-
if let Some(new_back_off) = self
199+
let new_back_off = self
200200
.strategy()
201-
.response_rate_limited(times_rate_limited, &self.name)
202-
{
201+
.response_rate_limited(times_rate_limited, &self.name);
202+
if let Some(new_back_off) = new_back_off {
203203
tracing::warn!(?self.name, ?new_back_off, "extended rate limiting");
204204
}
205205
} else {

0 commit comments

Comments
 (0)