-
Notifications
You must be signed in to change notification settings - Fork 107
Client and server timeout support #182
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
Open
cpu
wants to merge
5
commits into
rustls:main
Choose a base branch
from
cpu:cpu-lts-timeout_dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9ba831
feat: added tls handshake timeout
lucastemb 969aa3e
server: add TlsAcceptor/FallibleAccept timeout support
cpu bc4bf5e
common/timeout: switch HandshakeFuture to Instant-based deadlines
cpu 3cdbe0d
common/timeout: expose Timeout for cross-module reuse
cpu 044e824
server: extend handshake timeout to LazyConfigAcceptor + into_stream
cpu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| use std::future::Future; | ||
| use std::io; | ||
| use std::ops::{Deref, DerefMut}; | ||
| use std::pin::Pin; | ||
| use std::task::{Context, Poll}; | ||
| use std::time::Duration; | ||
|
|
||
| use rustls::{ConnectionCommon, SideData}; | ||
| use tokio::io::{AsyncRead, AsyncWrite}; | ||
| use tokio::time::{Instant, Sleep, sleep_until}; | ||
|
|
||
| use crate::common::{IoSession, MidHandshake}; | ||
|
|
||
| /// A `MidHandshake` future bundled with an optional deadline. | ||
| pub(crate) struct HandshakeFuture<IS: IoSession> { | ||
| inner: MidHandshake<IS>, | ||
| timeout: Option<Timeout>, | ||
| } | ||
|
|
||
| impl<IS: IoSession> HandshakeFuture<IS> { | ||
| /// Construct with a relative `Duration` timeout. | ||
| /// | ||
| /// The deadline is fixed to `Instant::now() + duration` immediately, so the | ||
| /// clock starts ticking when this is called, not when the future is first polled. | ||
| pub(crate) fn new(inner: MidHandshake<IS>, timeout: Option<Duration>) -> Self { | ||
| Self { | ||
| inner, | ||
| timeout: timeout.map(Timeout::from_duration), | ||
| } | ||
| } | ||
|
|
||
| /// Construct with an absolute `Instant` deadline. | ||
| /// | ||
| /// Used when an earlier phase (e.g. `LazyConfigAcceptor`) already established the | ||
| /// deadline and the post-ClientHello phase needs to inherit it. | ||
| pub(crate) fn from_deadline(inner: MidHandshake<IS>, deadline: Option<Instant>) -> Self { | ||
| Self { | ||
| inner, | ||
| timeout: deadline.map(Timeout::from_deadline), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn handshake(&self) -> &MidHandshake<IS> { | ||
| &self.inner | ||
| } | ||
|
|
||
| pub(crate) fn handshake_mut(&mut self) -> &mut MidHandshake<IS> { | ||
| &mut self.inner | ||
| } | ||
| } | ||
|
|
||
| impl<IS, SD> HandshakeFuture<IS> | ||
| where | ||
| IS: IoSession + Unpin, | ||
| IS::Io: AsyncRead + AsyncWrite + Unpin, | ||
| IS::Session: DerefMut + Deref<Target = ConnectionCommon<SD>> + Unpin, | ||
| SD: SideData, | ||
| { | ||
| pub(crate) fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Result<IS, (io::Error, IS::Io)>> { | ||
| if let Poll::Ready(result) = Pin::new(&mut self.inner).poll(cx) { | ||
| return Poll::Ready(result); | ||
| } | ||
|
|
||
| let timeout = match &mut self.timeout { | ||
| Some(timeout) => timeout, | ||
| None => return Poll::Pending, | ||
| }; | ||
| if timeout.poll_deadline(cx).is_pending() { | ||
| return Poll::Pending; | ||
| } | ||
|
|
||
| match self.inner.take_io() { | ||
| Some(io) => Poll::Ready(Err(( | ||
| io::Error::new(io::ErrorKind::TimedOut, "TLS handshake timed out"), | ||
| io, | ||
| ))), | ||
| // The inner handshake just returned `Pending` above, so it must | ||
| // still hold its IO because `take_io()` only returns `None` for the | ||
| // `End` state, which `MidHandshake::poll` never leaves behind | ||
| // when returning `Pending`. | ||
| None => unreachable!("handshake returned Pending but has no IO"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// An absolute deadline paired with a lazily-initialized `Sleep` future. | ||
| pub(crate) struct Timeout { | ||
| deadline: Instant, | ||
| sleep: Option<Pin<Box<Sleep>>>, | ||
| } | ||
|
|
||
| impl Timeout { | ||
| /// Construct from an absolute deadline. | ||
| pub(crate) fn from_deadline(deadline: Instant) -> Self { | ||
| Self { | ||
| deadline, | ||
| sleep: None, | ||
| } | ||
| } | ||
|
|
||
| /// Construct from a relative duration. | ||
| /// | ||
| /// The deadline is fixed to `Instant::now() + duration` immediately. | ||
| pub(crate) fn from_duration(duration: Duration) -> Self { | ||
| Self::from_deadline(Instant::now() + duration) | ||
| } | ||
|
|
||
| /// Poll the deadline. | ||
| /// | ||
| /// Returns `Poll::Ready(())` once the deadline has elapsed; `Poll::Pending` | ||
| /// otherwise. Lazily initializes the inner `Sleep` on first call. | ||
| pub(crate) fn poll_deadline(&mut self, cx: &mut Context<'_>) -> Poll<()> { | ||
| let sleep = self | ||
| .sleep | ||
| .get_or_insert_with(|| Box::pin(sleep_until(self.deadline))); | ||
| sleep.as_mut().poll(cx) | ||
| } | ||
|
|
||
| /// Return the absolute deadline. | ||
| pub(crate) fn deadline(&self) -> Instant { | ||
| self.deadline | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why couldn't we use a
tokio::time::Timeout<MindHandshake<IS>>instead of defining this wrapper ourselves?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that initially after reviewing 178 and having the same thought, but it didn't seem like a good fit to me after I tried it a bit.
Timeoutunless we do something hacky like always allocating aTimeoutwith max duration.Timeoutis!UnpinbutMidHandshakeand everything else is currentlyUnpin, so we need toPin<Box<...>>it at the cost of a heap allocation and more nesting complexity.Connect::get_mut()with aPin<Box<Timeout<...>>>or theFallibleConnectimplementation where we need to recover the IO without usingunsafe.I'm not particularly adept at async rust trickery, but achieving this in a semver compatible way using
tokio::time::Timeoutwithout any new deps orunsafedidn't seem tractable, and the implementation as done in this branch (and originally in 178) doesn't seem particularly complex in comparison.If you think I'm missing something maybe we could land this approach and then refactor it down to a
tokio::time::Timeoutas follow-up?