-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: Provide context-specific timeout messages
If we time out while connecting, don't tell the user we timed out reading a response.
- Loading branch information
Showing
3 changed files
with
25 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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,18 @@ | ||
//! Timeout utilities, mostly used during connecting. | ||
use std::io; | ||
use std::time::{Duration, Instant}; | ||
|
||
/// If the deadline is in the future, return the remaining time until | ||
/// then. Otherwise return a TimedOut error. | ||
pub fn time_until_deadline<S: Into<String>>(deadline: Instant, error: S) -> io::Result<Duration> { | ||
let now = Instant::now(); | ||
match deadline.checked_duration_since(now) { | ||
None => Err(io_err_timeout(error.into())), | ||
Some(duration) => Ok(duration), | ||
} | ||
} | ||
|
||
pub fn io_err_timeout(error: String) -> io::Error { | ||
io::Error::new(io::ErrorKind::TimedOut, error) | ||
} |