#async-io #reactor #file-io #sockets #future #async

reactors

Performance-focused cross-platform asynchronous IO implementation

4 releases

0.1.4 Mar 15, 2023
0.1.3 Mar 14, 2023
0.1.2 Mar 11, 2023
0.1.0 Mar 4, 2023

#1399 in Asynchronous

26 downloads per month

MIT license

115KB
3K SLoC

reactors

"Crates.io version" "docs.rs docs" "ci" "publish" License: MIT

reactors is a low-level cross-platform asynchronous io wrapper library for rust futures:

  • linux/android epoll backend
  • macos/ios/freebsd kqueue backend
  • windows iocp backend

bench

isolated

Thread Model

reactors does not limit the thread model it uses. In particular, we can create multiple IoReactor objects to share the IO load

use reactors::io::*;
use reactors::io::socket::tcp::TcpAcceptor;
use reactors::io::IoReactor;
use std::thread::spawn;
use futures::TryStreamExt;
use futures::{AsyncReadExt, AsyncWriteExt};
use futures::executor::block_on;
use std::time::Duration;
use reactors::Reactor;

fn main() -> anyhow::Result<()> {
    let acceptor_reactor = IoReactor::default();
    let connection_reactor = IoReactor::default();

    let mut acceptor_reactor_background = acceptor_reactor.clone();
    let mut connection_reactor_background = connection_reactor.clone();

    spawn(move || {
        acceptor_reactor_background.poll_once(Duration::from_millis(100)).unwrap();
    });

    spawn(move || {
        connection_reactor_background.poll_once(Duration::from_millis(100)).unwrap();
    });

    let mut acceptor = TcpAcceptor::new(acceptor_reactor, "127.0.0.1:1813".parse()?, Some(connection_reactor))?;

    block_on(async {
        while let Some((conn, _)) = acceptor.try_next().await.unwrap() {
            let mut reader = conn.to_read_stream(None);
            let mut writer = conn.to_write_stream(None);

            let mut buff = [0u8; 11];

            reader.read_exact(&mut buff).await.unwrap();

            assert_eq!(&buff, b"hello world");

            writer.write_all(&buff).await.unwrap();
        }
    });

    Ok(())
}

Dependencies

~0.8–7.5MB
~56K SLoC