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

Update to Tokio 1.0 #628

Merged
merged 8 commits into from
Jul 7, 2021
18 changes: 11 additions & 7 deletions server-utils/src/suspendable_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct SuspendableStream<S> {
next_delay: Duration,
initial_delay: Duration,
max_delay: Duration,
timeout: Option<Instant>,
suspended_until: Option<Instant>,
}

impl<S> SuspendableStream<S> {
Expand All @@ -29,7 +29,7 @@ impl<S> SuspendableStream<S> {
next_delay: Duration::from_millis(20),
initial_delay: Duration::from_millis(10),
max_delay: Duration::from_secs(5),
timeout: None,
suspended_until: None,
}
}
}
Expand All @@ -42,13 +42,17 @@ where

fn poll_next(mut self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Option<Self::Item>> {
loop {
if let Some(timeout) = &self.timeout {
let timeout = tokio::time::Instant::from_std(*timeout);
let sleep = tokio::time::sleep_until(timeout);
// If we encountered a connection error before then we suspend
// polling from the underlying stream for a bit
if let Some(deadline) = &mut self.suspended_until {
let deadline = tokio::time::Instant::from_std(*deadline);
let sleep = tokio::time::sleep_until(deadline);
futures::pin_mut!(sleep);
match sleep.poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(()) => {}
Poll::Ready(()) => {
self.suspended_until = None;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we're ready then let's reset the suspension flag not to execute that code path everytime

}
}
}

Expand Down Expand Up @@ -79,7 +83,7 @@ where
};
debug!("Error accepting connection: {}", err);
debug!("The server will stop accepting connections for {:?}", self.next_delay);
self.timeout = Some(Instant::now() + self.next_delay);
self.suspended_until = Some(Instant::now() + self.next_delay);
}
}
}
Expand Down