Skip to content

Commit

Permalink
limit: Fix rate limit not reseting state correctly (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco authored Apr 15, 2020
1 parent 7e55b7f commit 340e71a
Showing 1 changed file with 5 additions and 7 deletions.
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.");
}
}
}

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

0 comments on commit 340e71a

Please sign in to comment.