Home > Blockchain >  Rust async streams blocking workers
Rust async streams blocking workers

Time:01-07

I'm trying to implement an rocket.rs route that allows me to notify a client from the server.

Here is the minimal example I'm testing with:

use rocket::response::stream::Event;
use std::time::Duration;
use rocket::response::stream::EventStream;

#[macro_use] extern crate rocket;

#[get("/test/<id>")]
async fn test_route(id: String) -> EventStream![] {
    EventStream! {
        let mut interval = rocket::tokio::time::interval(Duration::from_secs(1));
        loop {
            yield Event::data(id.clone());
            interval.tick().await;
        }
    }
}

#[launch]
fn rocket() -> _ {
    rocket::build()
    .mount("/", routes![test_route])
}

During testing I noticed that the server would only respond to up to 6 (which is weird because I have 8 8 cores which should result in 24 workers) simultaneous requests. All requests after this limit are just loading infinitly without getting any response until I kill another request.

Here is the output of the server:

Configured for debug.
   >> address: 127.0.0.1
   >> port: 8000
   >> workers: 24
   >> ident: Rocket
   >> limits: bytes = 8KiB, data-form = 2MiB, file = 1MiB, form = 32KiB, json = 1MiB, msgpack = 1MiB, string = 8KiB
   >> temp dir: C:\Users\bened\AppData\Local\Temp\
   >> http/2: true
   >> keep-alive: 5s
   >> tls: disabled
   >> shutdown: ctrlc = true, force = true, grace = 2s, mercy = 3s
   >> log level: normal
   >> cli colors: true
Routes:
   >> (test_route) GET /test/<id>
Fairings:
   >> Shield (liftoff, response, singleton)
Shield:
   >> Permissions-Policy: interest-cohort=()
   >> X-Frame-Options: SAMEORIGIN
   >> X-Content-Type-Options: nosniff
Rocket has launched from http://127.0.0.1:8000
GET /test/echo text/html:
   >> Matched: (test_route) GET /test/<id>
   >> Outcome: Success
GET /test/echo text/html:
   >> Matched: (test_route) GET /test/<id>
   >> Outcome: Success
GET /test/echo text/html:
   >> Matched: (test_route) GET /test/<id>
   >> Outcome: Success
GET /test/echo text/html:
   >> Matched: (test_route) GET /test/<id>
   >> Outcome: Success
GET /test/echo text/html:
   >> Matched: (test_route) GET /test/<id>
   >> Outcome: Success
GET /test/echo text/html:
   >> Matched: (test_route) GET /test/<id>
   >> Outcome: Success

Is this simply a limit of rocket or is there an error in my implementation? I expected the async streams to be able to serve an infinite (or configurable) amount of simultaneous requests.

CodePudding user response:

Turns out it wasn't a limitation of rocket or my implementation.

I tested how many requests it would answer simultaneously by simply opening the route in google chrome. I just tested it with multiple instances of chrome and got 6 simultaneous request for each instance. So the limit was introduced by google chrome not rocket / my implementation.

  • Related