Monoio基于 io-uring 的高性能 Rust Runtime
Monoio 是基于 io-uring 的 thread-per-core 模型高性能 Rust Runtime,旨在为高性能网络中间件等场景提供必要的运行时。
功能上目前支持了部分网络 IO 和计时器;也支持跨线程异步通信。
性能
下面是基于 Monoio 实现一个简单的 echo 服务。运行起来之后可以通过 nc 127.0.0.1 50002
进行连接。
/// A echo example.
///
/// Run the example and `nc 127.0.0.1 50002` in another shell.
/// All your input will be echoed out.
use monoio::io::{AsyncReadRent, AsyncWriteRentExt};
use monoio::net::{TcpListener, TcpStream};
#[monoio::main]
async fn main() {
let listener = TcpListener::bind("127.0.0.1:50002").unwrap();
println!("listening");
loop {
let incoming = listener.accept().await;
match incoming {
Ok((stream, addr)) => {
println!("accepted a connection from {}", addr);
monoio::spawn(echo(stream));
}
Err(e) => {
println!("accepted connection failed: {}", e);
return;
}
}
}
}
async fn echo(stream: TcpStream) -> std::io::Result<()> {
let mut buf: Vec<u8> = Vec::with_capacity(8 * 1024);
loop {
// read
let (res, _buf) = stream.read(buf).await;
buf = _buf;
let res: usize = res?;
if res == 0 {
return Ok(());
}
// write all
let (res, _buf) = stream.write_all(buf).await;
buf = _buf;
res?;
// clear
buf.clear();
}
}
评论