#Why does Actix Web HTTP server take a closure that returns an `App` instead of using the App itself?

1 messages · Page 1 of 1 (latest)

pine mist
#
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(
        || App::new()
            .service(web::resource("/").to(|| HttpResponse::Ok())))
        .bind("127.0.0.1:59090")?
        .run()
        .await
}

in this code HttpServer takes a closure which it supposedly executes for each worker thread before starting the server. This requires all application data that would be inside the closure to be Send, Sync, Clone, Copy.

My question is, why not just accept App instead of || -> App ? What's the real difference?

white prawn
#

If the closure is executed once per thread, it would create an App per thread, I guess?

#

As opposed to having a single App

pine mist
#

Is there a reason each worker thread needs to execute the App constructors

pine mist
white prawn
#

Checking the source, my guess is that it's because it contains a Vec<Box<dyn AppServiceFactory>>, and dyn Trait can't (easily, see the dyn_clone crate) be cloned

pine mist
#

Ah I see. That could be the reason.