Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

limit: Fix rate limit not reseting state correctly #438

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions tower-limit/src/rate/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{error::Error, future::ResponseFuture, Rate};
use futures::{try_ready, Future, Poll};
use futures::{Async, Future, Poll};
use tokio_timer::{clock, Delay};
use tower_service::Service;

Expand Down Expand Up @@ -65,7 +65,9 @@ where
match self.state {
State::Ready { .. } => return self.inner.poll_ready().map_err(Into::into),
State::Limited(ref mut sleep) => {
try_ready!(sleep.poll());
if let Async::NotReady = sleep.poll()? {
tracing::trace!("rate limited exceeded; sleeping.");
Copy link
Member Author

@LucioFranco LucioFranco Apr 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukesteensen I like this better, what do you think? This should be the only case where we apply back pressure in this layer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little late on this review, but I think this actually broke the whole thing 😄

Before, try_ready! would return NotReady if we still needed to sleep. Now we're just checking if we should still be sleeping, logging that we should, and continuing on and marking ourselves Ready.

}
}
}

Expand All @@ -85,17 +87,13 @@ where
// If the period has elapsed, reset it.
if now >= until {
until = now + self.rate.per();
let rem = self.rate.num();

self.state = State::Ready { until, rem }
rem = self.rate.num();
}

if rem > 1 {
rem -= 1;
self.state = State::Ready { until, rem };
} else {
// The service is disabled until further notice
tracing::trace!("rate limit exceeded, disabling service");
let sleep = Delay::new(until);
self.state = State::Limited(sleep);
}
Expand Down