Tokio 1.0 released, Rust asynchronous programming framework

The stable version of Tokio 1.0 has been released. Tokio is Rust's asynchronous runtime, which can be used to write fast and reliable network applications. Tokio also provides APIs for TCP, UDP, timers, multi-threading, work-stealing scheduling, etc.

The Tokio team said that although Tokio has been evolving since its birth four years ago, the real significant change occurred a year ago, because Rust officially supported async/await at that time.

Sample code

Basic TCP echo server written by Tokio:

use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut listener = TcpListener::bind("127.0.0.1:8080").await?;

    loop {
        let (mut socket, _) = listener.accept().await?;

        tokio::spawn(async move {
            let mut buf = [0; 1024];

            // In a loop, read data from the socket and write the data back.
            loop {
                let n = match socket.read(&mut buf).await {
                    // socket closed
                    Ok(n) if n == 0 => return,
                    Ok(n) => n,
                    Err(e) => {
                        eprintln!("failed to read from socket; err = {:?}", e);
                        return;
                    }
                };

                // Write the data back
                if let Err(e) = socket.write_all(&buf[0..n]).await {
                    eprintln!("failed to write to socket; err = {:?}", e);
                    return;
                }
            }
        });
    }
}

In the announcement, the Tokio team also introduced well-known Tokio users and cases. For example, Discord  uses Tokio to reduce Tail Latency by 5 times; Fly.io  uses Tokio to easily meet performance requirements; AWS's  Lambda  team also uses Tokio to achieve more reliable and flexible services.

1.0 As an important version update, the Tokio team has guaranteed its stability and promised to provide a stable infrastructure for the Rust ecosystem. The Tokio team stated that there are currently no plans for Tokio 2.0 and promised not to release Tokio 2.0 for at least 3 years. They plan to provide at least 5 years of maintenance support for 1.0.

Of course, stability does not mean that Tokio is stagnant. The Tokio team introduced their future work: pushing the Stream trait into the Rust standard library, implementing the io_uring interface, integrating tracing, and improving Tokio's overall ecology. The ecology here is what the Tokio team calls the Tokio stack. For example, Tokio provides runtime and asynchronous APIs for standard primitives such as sockets and timers, but network applications usually use higher-level protocols such as HTTP and gRPC. Therefore, Tokio stack will provide HTTP Hyper and gRPC Tonic to meet the demand.

Finally, the Tokio team stated that with the launch of Tokio, they will focus on developing Tower, which is a set of reusable components for building reliable clients and servers.

See https://tokio.rs/blog/2020-12-tokio-1-0 for details 

Guess you like

Origin www.oschina.net/news/124525/tokio-1-0-released