mirror of
https://github.com/denoland/deno.git
synced 2025-03-04 01:44:26 -05:00
Refactor eager_{read,write,accept}_tcp into separate functions
This commit is contained in:
parent
988ec88dd0
commit
58f0547e09
1 changed files with 84 additions and 66 deletions
118
src/resources.rs
118
src/resources.rs
|
@ -206,25 +206,18 @@ where
|
||||||
Either::A(tokio_io::io::read(resource, buf)).into()
|
Either::A(tokio_io::io::read(resource, buf)).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is an optimization that Tokio should do.
|
|
||||||
// Attempt to call read() on the main thread.
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
pub fn eager_read<T>(resource: Resource, mut buf: T) -> EagerRead<Resource, T>
|
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
|
||||||
where
|
|
||||||
T: AsMut<[u8]>,
|
#[cfg(not(windows))]
|
||||||
{
|
fn eager_read_tcp<T: AsMut<[u8]>>(
|
||||||
let mut table = RESOURCE_TABLE.lock().unwrap();
|
tcp_stream: &TcpStream,
|
||||||
let maybe_repr = table.get_mut(&resource.rid);
|
resource: Resource,
|
||||||
match maybe_repr {
|
mut buf: T,
|
||||||
None => panic!("bad rid"),
|
) -> EagerRead<Resource, T> {
|
||||||
Some(repr) => match repr {
|
|
||||||
Repr::TcpStream(ref mut tcp_stream) => {
|
|
||||||
// Unforunately we can't just call read() on tokio::net::TcpStream
|
// Unforunately we can't just call read() on tokio::net::TcpStream
|
||||||
use std::os::unix::io::AsRawFd;
|
let fd = (*tcp_stream).as_raw_fd();
|
||||||
use std::os::unix::io::FromRawFd;
|
let mut std_tcp_stream = unsafe { std::net::TcpStream::from_raw_fd(fd) };
|
||||||
use std::os::unix::io::IntoRawFd;
|
|
||||||
let mut std_tcp_stream =
|
|
||||||
unsafe { std::net::TcpStream::from_raw_fd(tcp_stream.as_raw_fd()) };
|
|
||||||
let read_result = std_tcp_stream.read(buf.as_mut());
|
let read_result = std_tcp_stream.read(buf.as_mut());
|
||||||
// std_tcp_stream will close when it gets dropped. Thus...
|
// std_tcp_stream will close when it gets dropped. Thus...
|
||||||
let _ = std_tcp_stream.into_raw_fd();
|
let _ = std_tcp_stream.into_raw_fd();
|
||||||
|
@ -238,6 +231,22 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is an optimization that Tokio should do.
|
||||||
|
// Attempt to call read() on the main thread.
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
pub fn eager_read<T>(resource: Resource, buf: T) -> EagerRead<Resource, T>
|
||||||
|
where
|
||||||
|
T: AsMut<[u8]>,
|
||||||
|
{
|
||||||
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
||||||
|
let maybe_repr = table.get_mut(&resource.rid);
|
||||||
|
match maybe_repr {
|
||||||
|
None => panic!("bad rid"),
|
||||||
|
Some(repr) => match repr {
|
||||||
|
Repr::TcpStream(ref mut tcp_stream) => {
|
||||||
|
eager_read_tcp(tcp_stream, resource, buf)
|
||||||
}
|
}
|
||||||
_ => Either::A(tokio_io::io::read(resource, buf)),
|
_ => Either::A(tokio_io::io::read(resource, buf)),
|
||||||
},
|
},
|
||||||
|
@ -255,25 +264,14 @@ where
|
||||||
Either::A(tokio_write::write(resource, buf)).into()
|
Either::A(tokio_write::write(resource, buf)).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is an optimization that Tokio should do.
|
|
||||||
// Attempt to call write() on the main thread.
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
pub fn eager_write<T>(resource: Resource, buf: T) -> EagerWrite<Resource, T>
|
fn eager_write_tcp<T: AsRef<[u8]>>(
|
||||||
where
|
tcp_stream: &TcpStream,
|
||||||
T: AsRef<[u8]>,
|
resource: Resource,
|
||||||
{
|
buf: T,
|
||||||
let mut table = RESOURCE_TABLE.lock().unwrap();
|
) -> EagerWrite<Resource, T> {
|
||||||
let maybe_repr = table.get_mut(&resource.rid);
|
let fd = (*tcp_stream).as_raw_fd();
|
||||||
match maybe_repr {
|
let mut std_tcp_stream = unsafe { std::net::TcpStream::from_raw_fd(fd) };
|
||||||
None => panic!("bad rid"),
|
|
||||||
Some(repr) => match repr {
|
|
||||||
Repr::TcpStream(ref mut tcp_stream) => {
|
|
||||||
// Unforunately we can't just call write() on tokio::net::TcpStream
|
|
||||||
use std::os::unix::io::AsRawFd;
|
|
||||||
use std::os::unix::io::FromRawFd;
|
|
||||||
use std::os::unix::io::IntoRawFd;
|
|
||||||
let mut std_tcp_stream =
|
|
||||||
unsafe { std::net::TcpStream::from_raw_fd(tcp_stream.as_raw_fd()) };
|
|
||||||
let write_result = std_tcp_stream.write(buf.as_ref());
|
let write_result = std_tcp_stream.write(buf.as_ref());
|
||||||
// std_tcp_stream will close when it gets dropped. Thus...
|
// std_tcp_stream will close when it gets dropped. Thus...
|
||||||
let _ = std_tcp_stream.into_raw_fd();
|
let _ = std_tcp_stream.into_raw_fd();
|
||||||
|
@ -287,6 +285,22 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is an optimization that Tokio should do.
|
||||||
|
// Attempt to call write() on the main thread.
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
pub fn eager_write<T>(resource: Resource, buf: T) -> EagerWrite<Resource, T>
|
||||||
|
where
|
||||||
|
T: AsRef<[u8]>,
|
||||||
|
{
|
||||||
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
||||||
|
let maybe_repr = table.get_mut(&resource.rid);
|
||||||
|
match maybe_repr {
|
||||||
|
None => panic!("bad rid"),
|
||||||
|
Some(repr) => match repr {
|
||||||
|
Repr::TcpStream(ref mut tcp_stream) => {
|
||||||
|
eager_write_tcp(tcp_stream, resource, buf)
|
||||||
}
|
}
|
||||||
_ => Either::A(tokio_write::write(resource, buf)),
|
_ => Either::A(tokio_write::write(resource, buf)),
|
||||||
},
|
},
|
||||||
|
@ -303,22 +317,13 @@ pub fn eager_accept(resource: Resource) -> EagerAccept {
|
||||||
Either::A(tokio_util::accept(resource)).into()
|
Either::A(tokio_util::accept(resource)).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is an optimization that Tokio should do.
|
|
||||||
// Attempt to call write() on the main thread.
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
pub fn eager_accept(resource: Resource) -> EagerAccept {
|
fn eager_accept_tcp(
|
||||||
let mut table = RESOURCE_TABLE.lock().unwrap();
|
tcp_listener: &tokio::net::TcpListener,
|
||||||
let maybe_repr = table.get_mut(&resource.rid);
|
resource: Resource,
|
||||||
match maybe_repr {
|
) -> EagerAccept {
|
||||||
None => panic!("bad rid"),
|
let fd = (*tcp_listener).as_raw_fd();
|
||||||
Some(repr) => match repr {
|
let std_listener = unsafe { std::net::TcpListener::from_raw_fd(fd) };
|
||||||
Repr::TcpListener(ref mut listener) => {
|
|
||||||
// Unforunately we can't just call write() on tokio::net::TcpStream
|
|
||||||
use std::os::unix::io::AsRawFd;
|
|
||||||
use std::os::unix::io::FromRawFd;
|
|
||||||
use std::os::unix::io::IntoRawFd;
|
|
||||||
let mut std_listener =
|
|
||||||
unsafe { std::net::TcpListener::from_raw_fd(listener.as_raw_fd()) };
|
|
||||||
let result = std_listener.accept();
|
let result = std_listener.accept();
|
||||||
// std_listener will close when it gets dropped. Thus...
|
// std_listener will close when it gets dropped. Thus...
|
||||||
let _ = std_listener.into_raw_fd();
|
let _ = std_listener.into_raw_fd();
|
||||||
|
@ -339,6 +344,19 @@ pub fn eager_accept(resource: Resource) -> EagerAccept {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is an optimization that Tokio should do.
|
||||||
|
// Attempt to call write() on the main thread.
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
pub fn eager_accept(resource: Resource) -> EagerAccept {
|
||||||
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
||||||
|
let maybe_repr = table.get_mut(&resource.rid);
|
||||||
|
match maybe_repr {
|
||||||
|
None => panic!("bad rid"),
|
||||||
|
Some(repr) => match repr {
|
||||||
|
Repr::TcpListener(ref mut tcp_listener) => {
|
||||||
|
eager_accept_tcp(tcp_listener, resource)
|
||||||
}
|
}
|
||||||
_ => Either::A(tokio_util::accept(resource)),
|
_ => Either::A(tokio_util::accept(resource)),
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue