Skip to content

Commit

Permalink
Address various review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Aug 28, 2022
1 parent 9453ba7 commit a64b080
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 33 deletions.
2 changes: 0 additions & 2 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions examples/simple_ssr/Cargo.toml
Expand Up @@ -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"]
Expand Down
25 changes: 1 addition & 24 deletions examples/simple_ssr/src/bin/simple_ssr_server.rs
Expand Up @@ -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;
Expand All @@ -27,17 +26,6 @@ struct Executor {
inner: Runtime,
}

impl<F> hyper::rt::Executor<F> 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,
Expand All @@ -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
Expand All @@ -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;
}
6 changes: 6 additions & 0 deletions examples/ssr_router/src/bin/ssr_router_server.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
23 changes: 20 additions & 3 deletions 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.
Expand Down Expand Up @@ -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::*;
Expand All @@ -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]
Expand All @@ -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);
}
}
21 changes: 19 additions & 2 deletions packages/yew/src/platform/rt_tokio/mod.rs
Expand Up @@ -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::*;

Expand All @@ -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]
Expand All @@ -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, ());
}
}

0 comments on commit a64b080

Please sign in to comment.