Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions deno/http_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ use http_body_util::BodyExt;

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::OnceLock;
use std::thread::ThreadId;
use std::time::Duration;
use std::time::SystemTime;
use thiserror::Error;

static FETCH_TIMEOUT: OnceLock<Option<Duration>> = OnceLock::new();

fn fetch_timeout() -> Option<Duration> {
*FETCH_TIMEOUT.get_or_init(|| {
std::env::var("DENO_FETCH_TIMEOUT_SECS")
.ok()
.and_then(|v| v.parse::<u64>().ok())
.map(Duration::from_secs)
})
}

// TODO(ry) HTTP headers are not unique key, value pairs. There may be more than
// one header line with the same key. This should be changed to something like
// Vec<(String, String)>
Expand Down Expand Up @@ -391,13 +403,27 @@ impl HttpClient {
let accepts_val = HeaderValue::from_str(&accept)?;
request.headers_mut().insert(ACCEPT, accepts_val);
}
let response = match self.client.clone().send(request).await {
Ok(resp) => resp,
Err(err) => {
if err.is_connect_error() {
return Ok(FetchOnceResult::RequestError(err.to_string()));
let response = {
let send_fut = self.client.clone().send(request);
let resp = if let Some(dur) = fetch_timeout() {
tokio::time::timeout(dur, send_fut).await.map_err(|_| {
anyhow::anyhow!(
"Fetch '{}' timed out after {}s",
args.url,
dur.as_secs()
)
})?
} else {
send_fut.await
};
match resp {
Ok(r) => r,
Err(err) => {
if err.is_connect_error() {
return Ok(FetchOnceResult::RequestError(err.to_string()));
}
return Err(err.into());
}
return Err(err.into());
}
};

Expand Down Expand Up @@ -450,7 +476,19 @@ impl HttpClient {
return Err(err);
}

let body = get_response_body_with_progress(response).await?;
let body = if let Some(dur) = fetch_timeout() {
tokio::time::timeout(dur, get_response_body_with_progress(response))
.await
.map_err(|_| {
anyhow::anyhow!(
"Fetch '{}' body timed out after {}s",
args.url,
dur.as_secs()
)
})??
} else {
get_response_body_with_progress(response).await?
};

Ok(FetchOnceResult::Code(body, result_headers))
}
Expand Down
14 changes: 14 additions & 0 deletions vendor/deno_fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@ use std::path::PathBuf;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::OnceLock;
use std::task::Context;
use std::task::Poll;

static TCP_KEEPALIVE: OnceLock<std::time::Duration> = OnceLock::new();

fn tcp_keepalive() -> std::time::Duration {
*TCP_KEEPALIVE.get_or_init(|| {
let secs = std::env::var("DENO_TCP_KEEPALIVE_SECS")
.ok()
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(30);
std::time::Duration::from_secs(secs)
})
}

use deno_core::futures::stream::Peekable;
use deno_core::futures::Future;
use deno_core::futures::FutureExt;
Expand Down Expand Up @@ -1016,6 +1029,7 @@ pub fn create_http_client(
let mut http_connector =
HttpConnector::new_with_resolver(options.dns_resolver.clone());
http_connector.enforce_http(false);
http_connector.set_keepalive(Some(tcp_keepalive()));

let user_agent = user_agent.parse::<HeaderValue>().map_err(|_| {
HttpClientCreateError::InvalidUserAgent(user_agent.to_string())
Expand Down
Loading