Tokio Application in Rust

Tokio Application in Rust

Introduction

Tokio application in rust is an asynchronous runtime and network application framework. This is developed for fast development and highly scalable deployments of clients and servers in the Rust programming language. It offers the building blocks required for writing network applications. It provides the flexibility to target a wide range of systems. Those systems are from large servers with dozens of cores to minor embedded devices.

In this article, we will understand the Tokio application in the Rust programming language.

Description

Tokio application is an event-driven platform for writing asynchronous applications with the Rust programming language. It gives a few major components at a high level:

  • A multi-threaded runtime for implementing asynchronous code.
  • An asynchronous version of the standard library.
  • A great and larger ecosystem of libraries.

Benefits of Tokio

Rapid

  • Tokio is rapid, built on top of the Rust programming language.
  • This is designed in the spirit of Rust with the goal that we should not be able to enhance efficiency by writing equivalent code by hand.
  • Tokio is scalable and built on top of the async or await language feature.
  • There’s a limit to how fast we may handle a connection due to latency when dealing with networking.
  • Therefore, the only direction to scale is to control many connections at once.
  • Rising the number of concurrent operations becomes incredibly cheap with the async or await language feature.
  • That enables us to scale to a large number of concurrent tasks.

Trusted

  • Tokio application is built using Rust.
  • Rust is a language that empowers everyone to build trusted and efficient software.
  • In different studies have found that roughly ~70 percent of high severity security bugs are the result of memory unsafety.
  • By using Rust removes this whole class of bugs in the applications.
  • Tokio similarly emphasizes greatly giving consistent behavior with no surprises.
  • Tokio’s main target is to permit users to deploy predictable software.
  • That would do the same day in and day out with reliable response times and no unpredictable latency spikes.

Simple and Easy

  • The complexity of writing asynchronous applications has been decreased substantially with Rust’s async or await feature.
  • Writing applications is easy to do with Tokio’s utilities and vibrant ecosystem.
  • Tokyo application adopts the standard library’s naming convention when it makes sense.
  • This enables easily converting code written with only the standard library to code written with Tokyo.
  • The ability to provide the right code easily is unparalleled with the strong type system of Rust.

Easily modified

  • Tokio application gives different variations of the runtime.
  • All things from a multi-threaded
  • Work-stealing runtime to a light-weight
  • Single-threaded runtime

Every one of these runtimes comes with multiple knobs to enable users to tune them to their requirements.

Service Trait is a cornerstone of Tokio’s building blocks. It made it possible for writing composable and reusable components by standardizing the interface that clients and servers use.

pub trait Service: Send + 'static {
    type Req: Send + 'static;

    type Resp: Send + 'static;

    type Error: Send + 'static;

    type Fut: Future<Item = Self::Resp, Error = Self::Error>;

    fn call(&self, req: Self::Req) -> Self::Fut;
}
  • A simple trait, though it unlocks a world of possibility.
  • This symmetric and uniform API decreased the server or client into a function.
  • Therefore, it allows middleware or filters, depending on the lingo.
  • The Service is an asynchronous function from Request to Response.
  • Asynchronicity is managed by the Futures crate.
  • The component, middleware, acts together as a client and a server.
  • It intercepts a request, modifies it, and passes it on to an upstream Service value.

Timeouts use as an example

pub struct Timeout<T> {
    upstream: T,
    delay: Duration,
    timer: Timer,
}

impl<T> Timeout<T> {
    pub fn new(upstream: T, delay: Duration) -> Timeout<T> {
        Timeout {
            upstream: upstream,
            delay: delay,
            timer: Timer::default(),
        }
    }
}

impl<T> Service for Timeout<T>
    where T: Service,
          T::Error: From<Expired>,
{
    type Req = T::Req;
    type Resp = T::Resp;
    type Error = T::Error;
    type Fut = Box<Future<Item = Self::Resp, Error = Self::Error>>;

    fn call(&self, req: Self::Req) -> Self::Fut {
        // Get a future representing the timeout and map it to the Service error type
        let timeout = self.timer.timeout(self.delay)
            .and_then(|timeout| Err(Self::Error::from(timeout)));

        // Call the upstream service, passing along the request
        self.upstream.call(req)
            // Wait for either the upstream response or the timeout to happen
            .select(timeout)
            // Map the result of the select back to the expected Response type
            .map(|(v, _)| v)
            .map_err(|(e, _)| e)
            .boxed()
    }
}
  • Above shown Timeout middleware only has to be written once.
  • Then it may be inserted as part of any network client for any protocol, as displayed below.
let client = Timeout::new(
    http::Client::new(),
    Duration::from_secs(60));

client.call(http::Request::get("https://www.rust-lang.org"))
    .and_then(|response| {
        // Process the response
        Ok(())
    }).forget(); // Process the future
  • After that the interesting thing is that the really same middleware could be used in a server as well, see below.
http::Server::new()
    .serve(Timeout::new(MyService, Duration::from_secs(60)));

Do not use Tokio when:

  • Fasting up CPU-bound computations by running them in parallel on various threads.
  • We should be using rayon if the only thing the application does is run computations in parallel.
  • Reading a number of files.
  • Tokio offers no benefit here compared to an ordinary thread pool.
  • This is due to operating systems usually do not give asynchronous file APIs.
  • Sending a single web request.
  • At the time where Tokio provides us a benefit is when we need to do many things at the same time.
  • We should prefer the blocking version of that library when we need to use a library intended for asynchronous Rust such as reqwest.
  • We don’t require to do a lot of things at once, as it would make the project simpler.

For more details visit: https://www.technologiesinindustry4.com

Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 422

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top