rust 禁用阻塞工作进程的异步流

z5btuh9x  于 2023-01-05  发布在  其他
关注(0)|答案(1)|浏览(159)

我正在尝试实现一个rocket.rs路由,它允许我从服务器通知客户机。
下面是我正在测试的最小示例:

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])
}

在测试过程中,我注意到服务器最多只能响应6个(这很奇怪,因为我有8+8个内核,应该有24个工作线程)同时请求。超过这个限制的所有请求都将无限加载,直到我杀死另一个请求才得到任何响应。
下面是服务器的输出:

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

这仅仅是火箭的极限还是我的实现中有错误?我期望异步流能够服务于无限(或可配置)数量的同时请求。

kgsdhlau

kgsdhlau1#

原来这不是火箭或我的实现的限制。
我测试了它能同时响应多少个请求,只需要在google chrome中打开路由,我用chrome的多个示例测试,每个示例有6个同时请求,所以这个限制是google chrome引入的,而不是rocket /我的实现。

相关问题