diff --git a/Cargo.lock b/Cargo.lock index 1d8772636..1bd724cd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5965,6 +5965,7 @@ dependencies = [ "futures", "hyper", "libloading", + "num_cpus", "pipe", "poem", "portpicker", diff --git a/cargo-shuttle/src/init.rs b/cargo-shuttle/src/init.rs index 524864bea..ea09f305a 100644 --- a/cargo-shuttle/src/init.rs +++ b/cargo-shuttle/src/init.rs @@ -85,18 +85,19 @@ impl ShuttleInit for ShuttleInitActixWeb { fn get_boilerplate_code_for_framework(&self) -> &'static str { indoc! {r#" - use actix_web::web::{resource, ServiceConfig}; + use actix_web::{get, web::ServiceConfig}; use shuttle_service::ShuttleActixWeb; + #[get("/hello")] async fn hello_world() -> &'static str { "Hello World!" } #[shuttle_service::main] async fn actix_web( - ) -> ShuttleActixWeb { + ) -> ShuttleActixWeb { Ok(move |cfg: &mut ServiceConfig| { - cfg.service(resource("/hello").to(hello_world)); + cfg.service(hello_world); }) }"#} } diff --git a/service/Cargo.toml b/service/Cargo.toml index 73e6f7a0f..0fdc04f69 100644 --- a/service/Cargo.toml +++ b/service/Cargo.toml @@ -23,6 +23,7 @@ crossbeam-channel = "0.5.6" futures = { version = "0.3.25", features = ["std"] } hyper = { version = "0.14.23", features = ["server", "tcp", "http1"], optional = true } libloading = { version = "0.7.4", optional = true } +num_cpus = { version = "1.14.0", optional = true } pipe = "0.4.0" poem = { version = "1.3.49", optional = true } rocket = { version = "0.5.0-rc.2", optional = true } @@ -65,7 +66,7 @@ default = ["codegen"] codegen = ["shuttle-codegen"] loader = ["cargo", "libloading"] -web-actix-web = ["actix-web"] +web-actix-web = ["actix-web", "num_cpus"] web-axum = ["axum", "sync_wrapper"] web-rocket = ["rocket"] web-thruster = ["thruster"] diff --git a/service/src/lib.rs b/service/src/lib.rs index 572484339..a8ecd95a1 100644 --- a/service/src/lib.rs +++ b/service/src/lib.rs @@ -557,10 +557,14 @@ impl Service for sync_wrapper::SyncWrapper { #[async_trait] impl Service for F where - F: FnOnce(&mut actix_web::web::ServiceConfig) + Sync + Send + Copy + Clone + 'static, + F: FnOnce(&mut actix_web::web::ServiceConfig) + Sync + Send + Clone + 'static, { - async fn bind(self: Box, addr: SocketAddr) -> Result<(), Error> { - let srv = actix_web::HttpServer::new(move || actix_web::App::new().configure(*self)) + async fn bind(mut self: Box, addr: SocketAddr) -> Result<(), Error> { + // Start a worker for each cpu, but no more than 8. + let worker_count = num_cpus::get().max(8); + + let srv = actix_web::HttpServer::new(move || actix_web::App::new().configure(self.clone())) + .workers(worker_count) .bind(addr)? .run(); srv.await.map_err(error::CustomError::new)?;