-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
62 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,54 @@ | ||
use core::task; | ||
use http::Uri; | ||
use hyper_util::{client::legacy::connect::HttpConnector, rt::TokioIo}; | ||
use std::{future::Future, pin::Pin, task::Poll, time::Duration}; | ||
use tokio::net::TcpStream; | ||
|
||
#[derive(Clone)] | ||
pub struct TimeoutHttpConnector { | ||
pub timeout: Duration, | ||
pub connector: HttpConnector, | ||
} | ||
|
||
impl Default for TimeoutHttpConnector { | ||
fn default() -> Self { | ||
TimeoutHttpConnector { | ||
timeout: Duration::from_secs(10), | ||
connector: HttpConnector::new(), | ||
} | ||
} | ||
} | ||
|
||
impl tower_service::Service<Uri> for TimeoutHttpConnector { | ||
type Response = TokioIo<TcpStream>; | ||
type Error = TimeoutHttpConnectorError; | ||
type Future = Pin<Box<dyn Future<Output = Result<TokioIo<TcpStream>, Self::Error>> + Send>>; | ||
|
||
fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.connector | ||
.poll_ready(cx) | ||
.map_err(|e| TimeoutHttpConnectorError::Boxed(Box::new(e))) | ||
} | ||
|
||
fn call(&mut self, dst: Uri) -> Self::Future { | ||
let fut = self.connector.call(dst); | ||
let timeout = self.timeout; | ||
Box::pin(async move { | ||
let result = tokio::time::timeout(timeout, fut).await; | ||
match result { | ||
Ok(Ok(io)) => Ok(io), | ||
Ok(Err(e)) => Err(TimeoutHttpConnectorError::Boxed(Box::new(e))), | ||
Err(_) => Err(TimeoutHttpConnectorError::Timeout), | ||
} | ||
}) | ||
} | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum TimeoutHttpConnectorError { | ||
#[error("Timeout")] | ||
Timeout, | ||
|
||
#[error("Non-timeout error: {0}")] | ||
Boxed(#[from] Box<dyn std::error::Error + Send + Sync>), | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod body; | ||
pub mod connector; | ||
mod graceful_shutdown; | ||
pub mod https_redirect; | ||
pub mod proxy; | ||
|
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