0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-09 13:49:37 -04:00

feat(ext/net): Remove timeout option from Deno.Connect()

This commit is contained in:
DanieleAurilio 2024-12-02 23:42:19 +01:00
parent 03e758a542
commit 68d0e565f1
4 changed files with 12 additions and 53 deletions

View file

@ -596,7 +596,6 @@ async function connect(args) {
hostname: args.hostname ?? "127.0.0.1",
port,
},
args.timeout,
cancelRid,
);
localAddr.transport = "tcp";

View file

@ -290,7 +290,6 @@ declare namespace Deno {
* @default {"127.0.0.1"} */
hostname?: string;
transport?: "tcp";
timeout?: number;
signal?: AbortSignal | null;
}

View file

@ -36,7 +36,6 @@ use socket2::Socket;
use socket2::Type;
use std::borrow::Cow;
use std::cell::RefCell;
use std::future::Future;
use std::net::Ipv4Addr;
use std::net::Ipv6Addr;
use std::net::SocketAddr;
@ -44,7 +43,6 @@ use std::rc::Rc;
use std::str::FromStr;
use tokio::net::TcpStream;
use tokio::net::UdpSocket;
use tokio::time::timeout;
#[derive(Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
@ -67,28 +65,6 @@ impl From<SocketAddr> for IpAddr {
}
}
pub trait TcpStreamTimeout {
fn connect_timeout(
addr: &SocketAddr,
tcp_timeout: Option<u64>,
) -> impl Future<Output = Result<TcpStream, std::io::Error>>;
}
impl TcpStreamTimeout for TcpStream {
async fn connect_timeout(
addr: &SocketAddr,
tcp_timeout: Option<u64>,
) -> Result<TcpStream, std::io::Error> {
if tcp_timeout.is_none() {
TcpStream::connect(addr).await
} else {
let timeout_duration =
std::time::Duration::from_millis(tcp_timeout.unwrap());
timeout(timeout_duration, TcpStream::connect(addr)).await?
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum NetError {
#[error("Listener has been closed")]
@ -372,20 +348,18 @@ pub async fn op_net_set_multi_ttl_udp(
pub async fn op_net_connect_tcp<NP>(
state: Rc<RefCell<OpState>>,
#[serde] addr: IpAddr,
#[serde] timeout: Option<u64>,
#[smi] resource_abort_id: Option<ResourceId>,
) -> Result<(ResourceId, IpAddr, IpAddr), NetError>
where
NP: NetPermissions + 'static,
{
op_net_connect_tcp_inner::<NP>(state, addr, timeout, resource_abort_id).await
op_net_connect_tcp_inner::<NP>(state, addr, resource_abort_id).await
}
#[inline]
pub async fn op_net_connect_tcp_inner<NP>(
state: Rc<RefCell<OpState>>,
addr: IpAddr,
timeout: Option<u64>,
resource_abort_id: Option<ResourceId>,
) -> Result<(ResourceId, IpAddr, IpAddr), NetError>
where
@ -412,13 +386,17 @@ where
});
let tcp_stream_result = if let Some(cancel_handle) = &cancel_handle {
TcpStream::connect_timeout(&addr, timeout)
.or_cancel(cancel_handle)
.await?
TcpStream::connect(&addr).or_cancel(cancel_handle).await?
} else {
TcpStream::connect_timeout(&addr, timeout).await
TcpStream::connect(&addr).await
};
if let Some(cancel_rid) = resource_abort_id {
if let Ok(res) = state.borrow_mut().resource_table.take_any(cancel_rid) {
res.close();
}
}
let tcp_stream = match tcp_stream_result {
Ok(tcp_stream) => tcp_stream,
Err(e) => return Err(NetError::Io(e)),
@ -1175,10 +1153,9 @@ mod tests {
port: server_addr[1].parse().unwrap(),
};
let mut connect_fut = op_net_connect_tcp_inner::<TestPermission>(
conn_state, ip_addr, None, None,
)
.boxed_local();
let mut connect_fut =
op_net_connect_tcp_inner::<TestPermission>(conn_state, ip_addr, None)
.boxed_local();
let mut rid = None;
tokio::select! {

View file

@ -1277,22 +1277,6 @@ Deno.test({
}
});
Deno.test(
{ permissions: { net: true } },
async function netTcpSetTimeout() {
try {
const conn = await Deno.connect({
hostname: "example.com",
port: 50000,
timeout: 1000,
});
conn.close();
} catch (error) {
assert(error instanceof Deno.errors.TimedOut);
}
},
);
Deno.test(
{ permissions: { net: true } },
async function netTcpWithAbortSignal() {