Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to tokio #128

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ stats = ["zenoh/stats"]

[dependencies]
anyhow = "1.0.69"
async-std = "=1.12.0"
async-stream = "0.3.5"
futures = "0.3.26"
git-version = "0.3.5"
lazy_static = "1.4.0"
serde = "1.0.154"
serde_json = "1.0.114"
tide = "0.16.0"
tokio = { version = "1.35.1", default-features = false } # Default features are disabled due to some crates' requirements
tokio-stream = "0.1.15"
tracing = "0.1"
zenoh = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "dev/1.0.0", features = [
"unstable", "internal", "plugins"
Expand Down
18 changes: 18 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ use serde::{
};

const DEFAULT_HTTP_INTERFACE: &str = "0.0.0.0";
pub const DEFAULT_WORK_THREAD_NUM: usize = 2;
pub const DEFAULT_MAX_BLOCK_THREAD_NUM: usize = 50;

#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub(crate) struct Config {
#[serde(deserialize_with = "deserialize_http_port")]
pub(crate) http_port: String,
#[serde(default = "default_work_thread_num")]
pub work_thread_num: usize,
#[serde(default = "default_max_block_thread_num")]
pub max_block_thread_num: usize,
__required__: Option<bool>,
#[serde(default, deserialize_with = "deserialize_path")]
__path__: Option<Vec<String>>,
Expand All @@ -39,6 +45,14 @@ where
deserializer.deserialize_any(HttpPortVisitor)
}

fn default_work_thread_num() -> usize {
DEFAULT_WORK_THREAD_NUM
}

fn default_max_block_thread_num() -> usize {
DEFAULT_MAX_BLOCK_THREAD_NUM
}

struct HttpPortVisitor;

impl<'de> Visitor<'de> for HttpPortVisitor {
Expand Down Expand Up @@ -153,6 +167,7 @@ mod tests {
http_port,
__required__,
__path__,
..
} = config.unwrap();

assert_eq!(http_port, format!("{DEFAULT_HTTP_INTERFACE}:8080"));
Expand All @@ -169,6 +184,7 @@ mod tests {
http_port,
__required__,
__path__,
..
} = config.unwrap();

assert_eq!(http_port, format!("{DEFAULT_HTTP_INTERFACE}:8080"));
Expand All @@ -188,6 +204,7 @@ mod tests {
http_port,
__required__,
__path__,
..
} = config.unwrap();

assert_eq!(http_port, format!("{DEFAULT_HTTP_INTERFACE}:8080"));
Expand All @@ -205,6 +222,7 @@ mod tests {
http_port,
__required__,
__path__,
..
} = config.unwrap();

assert_eq!(http_port, format!("{DEFAULT_HTTP_INTERFACE}:8080"));
Expand Down
90 changes: 67 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@
// ZettaScale Zenoh Team, <[email protected]>
//

use std::{borrow::Cow, collections::HashMap, str::FromStr};
use std::{
borrow::Cow,
collections::HashMap,
future::Future,
str::FromStr,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};

use async_std::{prelude::FutureExt, sync::Arc};
use futures::TryStreamExt;
use futures::stream::TryStreamExt;
use tide::{http::Mime, Request, Response, Server, StatusCode};
use tokio::task::JoinHandle;
use tracing::debug;
use zenoh::{
bytes::{Encoding, ZBytes},
Expand All @@ -36,6 +45,36 @@ use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin, PluginCont
mod config;
use config::Config;

lazy_static::lazy_static! {
static ref WORK_THREAD_NUM: AtomicUsize = AtomicUsize::new(config::DEFAULT_WORK_THREAD_NUM);
static ref MAX_BLOCK_THREAD_NUM: AtomicUsize = AtomicUsize::new(config::DEFAULT_MAX_BLOCK_THREAD_NUM);
// The global runtime is used in the dynamic plugins, which we can't get the current runtime
static ref TOKIO_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(WORK_THREAD_NUM.load(Ordering::SeqCst))
.max_blocking_threads(MAX_BLOCK_THREAD_NUM.load(Ordering::SeqCst))
.enable_all()
.build()
.expect("Unable to create runtime");
}
#[inline(always)]
pub(crate) fn spawn_runtime<F>(task: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
// Check whether able to get the current runtime
match tokio::runtime::Handle::try_current() {
Ok(rt) => {
// Able to get the current runtime (standalone binary), spawn on the current runtime
rt.spawn(task)
}
Err(_) => {
// Unable to get the current runtime (dynamic plugins), spawn on the global runtime
TOKIO_RUNTIME.spawn(task)
}
}
}

const DEFAULT_DIRECTORY_INDEX: &str = "index.html";

lazy_static::lazy_static! {
Expand All @@ -61,7 +100,11 @@ impl Plugin for WebServerPlugin {
.ok_or_else(|| zerror!("Plugin `{}`: missing config", name))?;
let conf: Config = serde_json::from_value(plugin_conf.clone())
.map_err(|e| zerror!("Plugin `{}` configuration error: {}", name, e))?;
async_std::task::spawn(run(runtime.clone(), conf));
WORK_THREAD_NUM.store(conf.work_thread_num, Ordering::SeqCst);
MAX_BLOCK_THREAD_NUM.store(conf.max_block_thread_num, Ordering::SeqCst);

spawn_runtime(run(runtime.clone(), conf));

Ok(Box::new(WebServerPlugin))
}
}
Expand Down Expand Up @@ -127,13 +170,9 @@ async fn handle_request(req: Request<Arc<Session>>) -> tide::Result<Response> {
== Some("SUB")
{
tracing::debug!("Subscribe to {} for Multipart stream", selector.key_expr,);
let (sender, receiver) = async_std::channel::bounded(1);
async_std::task::spawn(async move {
tracing::debug!(
"Subscribe to {} for Multipart stream (task {})",
selector.key_expr,
async_std::task::current().id()
);
let (sender, mut receiver) = tokio::sync::mpsc::channel(1);
tokio::task::spawn(async move {
tracing::debug!("Subscribe to {} for Multipart stream", selector.key_expr);
let sub = req
.state()
.declare_subscriber(&selector.key_expr.into_owned())
Expand All @@ -146,28 +185,25 @@ async fn handle_request(req: Request<Arc<Session>>) -> tide::Result<Response> {
buf.extend_from_slice("\n\n".as_bytes());
buf.extend_from_slice(&sample.payload().into::<Cow<[u8]>>());

match sender
.send(Ok(buf))
.timeout(std::time::Duration::new(10, 0))
.await
match tokio::time::timeout(
std::time::Duration::new(10, 0),
sender.send(Ok(buf)),
)
.await
{
Ok(Ok(_)) => {}
Ok(Err(e)) => {
tracing::debug!(
"Multipart error ({})! Unsubscribe and terminate (task {})",
e,
async_std::task::current().id()
"Multipart error ({})! Unsubscribe and terminate",
e
);
if let Err(e) = sub.undeclare().await {
tracing::error!("Error undeclaring subscriber: {}", e);
}
break;
}
Err(_) => {
tracing::debug!(
"Multipart timeout! Unsubscribe and terminate (task {})",
async_std::task::current().id()
);
tracing::debug!("Multipart timeout! Unsubscribe and terminate",);
if let Err(e) = sub.undeclare().await {
tracing::error!("Error undeclaring subscriber: {}", e);
}
Expand All @@ -177,9 +213,17 @@ async fn handle_request(req: Request<Arc<Session>>) -> tide::Result<Response> {
}
});

let receiver = async_stream::stream! {
while let Some(item) = receiver.recv().await {
yield item;
}
};
let mut res = Response::new(StatusCode::Ok);
res.set_content_type("multipart/x-mixed-replace; boundary=\"boundary\"");
res.set_body(tide::Body::from_reader(receiver.into_async_read(), None));
res.set_body(tide::Body::from_reader(
Box::pin(receiver.into_async_read()),
None,
));

Ok(res)
} else {
Expand Down