diff --git a/examples/Cargo.lock b/examples/Cargo.lock index b8dae574ad3..63e6e5dc422 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -1930,8 +1930,6 @@ dependencies = [ "bytes", "clap", "futures 0.3.21", - "hyper", - "jemallocator", "log", "reqwest", "serde", diff --git a/examples/simple_ssr/Cargo.toml b/examples/simple_ssr/Cargo.toml index fcdf5c4c525..4bb1b3b549e 100644 --- a/examples/simple_ssr/Cargo.toml +++ b/examples/simple_ssr/Cargo.toml @@ -29,8 +29,6 @@ log = "0.4" tokio = { version = "1.15.0", features = ["full"] } warp = "0.3" clap = { version = "3.1.7", features = ["derive"] } -hyper = { version = "0.14", features = ["server", "http1"] } -jemallocator = "0.5" [features] hydration = ["yew/hydration"] diff --git a/examples/simple_ssr/src/bin/simple_ssr_server.rs b/examples/simple_ssr/src/bin/simple_ssr_server.rs index 2530817162b..11ca6da39a4 100644 --- a/examples/simple_ssr/src/bin/simple_ssr_server.rs +++ b/examples/simple_ssr/src/bin/simple_ssr_server.rs @@ -6,7 +6,6 @@ use std::path::PathBuf; use bytes::Bytes; use clap::Parser; use futures::stream::{self, Stream, StreamExt}; -use hyper::server::Server; use simple_ssr::App; use warp::Filter; use yew::platform::Runtime; @@ -27,17 +26,6 @@ struct Executor { inner: Runtime, } -impl hyper::rt::Executor for Executor -where - F: Future + Send + 'static, -{ - fn execute(&self, fut: F) { - self.inner.spawn_pinned(move || async move { - fut.await; - }); - } -} - async fn render( index_html_before: String, index_html_after: String, @@ -55,7 +43,6 @@ async fn render( #[tokio::main] async fn main() { let opts = Opt::parse(); - let exec = Executor::default(); let index_html_s = tokio::fs::read_to_string(opts.dir.join("index.html")) .await @@ -76,15 +63,5 @@ async fn main() { let routes = html.or(warp::fs::dir(opts.dir)); println!("You can view the website at: http://localhost:8080/"); - - let warp_svc = warp::service(routes); - let svc = hyper::service::make_service_fn(move |_| { - let warp_svc = warp_svc.clone(); - async move { Ok::<_, Infallible>(warp_svc) } - }); - Server::bind(&"127.0.0.1:8080".parse().unwrap()) - .executor(exec) - .serve(svc) - .await - .unwrap(); + warp::serve(routes).run(([127, 0, 0, 1], 8080)).await; } diff --git a/examples/ssr_router/src/bin/ssr_router_server.rs b/examples/ssr_router/src/bin/ssr_router_server.rs index cabdfaff077..d594b15efea 100644 --- a/examples/ssr_router/src/bin/ssr_router_server.rs +++ b/examples/ssr_router/src/bin/ssr_router_server.rs @@ -19,6 +19,7 @@ use tower::ServiceExt; use tower_http::services::ServeDir; use yew::platform::Runtime; +// We use jemalloc as it produces better performance. #[global_allocator] static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc; @@ -51,6 +52,11 @@ async fn render( } // An executor to process requests on the Yew runtime. +// +// By spawning requests on the Yew runtime, +// it processes request on the same thread as the rendering task. +// +// This increases performance in some environments (e.g.: in VM). #[derive(Clone, Default)] struct Executor { inner: Runtime, diff --git a/packages/yew/src/platform/rt_tokio/local_worker.rs b/packages/yew/src/platform/rt_tokio/local_worker.rs index e717dc19614..1aa18e478ef 100644 --- a/packages/yew/src/platform/rt_tokio/local_worker.rs +++ b/packages/yew/src/platform/rt_tokio/local_worker.rs @@ -1,7 +1,7 @@ //! We use a local worker implementation that does not produce a JoinHandle for spawn_pinned. //! This avoids the cost to acquire a JoinHandle. //! -//! See https://github.com/tokio-rs/tokio/issues/4819 +//! See: [tokio-rs/tokio#4819](https://github.com/tokio-rs/tokio/issues/4819) //! //! We will not be able to produce a meaningful JoinHandle until WebAssembly targets support //! unwinding. @@ -144,8 +144,11 @@ impl LocalHandle { #[cfg(test)] mod tests { + use std::time::Duration; + use futures::channel::oneshot; use tokio::test; + use tokio::time::timeout; use yew::platform::Runtime; use super::*; @@ -162,7 +165,12 @@ mod tests { .expect("failed to send"); }); - assert!(rx.await.expect("failed to receive")); + let result = timeout(Duration::from_secs(5), rx) + .await + .expect("task timed out") + .expect("failed to receive"); + + assert!(result); } #[test] @@ -185,6 +193,15 @@ mod tests { }) }); - assert_eq!(rx1.await, rx2.await); + let result1 = timeout(Duration::from_secs(5), rx1) + .await + .expect("task timed out") + .expect("failed to receive"); + let result2 = timeout(Duration::from_secs(5), rx2) + .await + .expect("task timed out") + .expect("failed to receive"); + + assert_eq!(result1, result2); } } diff --git a/packages/yew/src/platform/rt_tokio/mod.rs b/packages/yew/src/platform/rt_tokio/mod.rs index 04ac8cad690..bcc23cd8d0b 100644 --- a/packages/yew/src/platform/rt_tokio/mod.rs +++ b/packages/yew/src/platform/rt_tokio/mod.rs @@ -108,8 +108,11 @@ impl Runtime { #[cfg(test)] mod tests { + use std::time::Duration; + use futures::channel::oneshot; use tokio::test; + use tokio::time::timeout; use super::*; @@ -130,8 +133,17 @@ mod tests { .expect("failed to send!"); }); + let result1 = timeout(Duration::from_secs(5), rx1) + .await + .expect("task timed out") + .expect("failed to receive"); + let result2 = timeout(Duration::from_secs(5), rx2) + .await + .expect("task timed out") + .expect("failed to receive"); + // first task and second task are not on the same thread. - assert_ne!(rx1.await, rx2.await); + assert_ne!(result1, result2); } #[test] @@ -152,6 +164,11 @@ mod tests { }); }); - assert_eq!(rx.await, Ok(())); + let result = timeout(Duration::from_secs(5), rx) + .await + .expect("task timed out") + .expect("failed to receive"); + + assert_eq!(result, ()); } }