From 8fbc985e9b17c5fccd35c74303f6d70f3179bb7b Mon Sep 17 00:00:00 2001 From: John Smith Date: Wed, 29 Jun 2022 13:39:54 -0400 Subject: [PATCH] flutter tokio --- Cargo.lock | 6 ++- veilid-flutter/rust/Cargo.toml | 23 +++++++---- veilid-flutter/rust/src/dart_ffi.rs | 40 ++++++++++++------- .../rust/src/dart_isolate_wrapper.rs | 5 ++- veilid-flutter/rust/src/lib.rs | 1 + veilid-flutter/rust/src/tools.rs | 33 +++++++++++++++ veilid-wasm/Cargo.toml | 10 +++-- 7 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 veilid-flutter/rust/src/tools.rs diff --git a/Cargo.lock b/Cargo.lock index badd91ae..278ac9c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5115,9 +5115,11 @@ name = "veilid-flutter" version = "0.1.0" dependencies = [ "allo-isolate", + "async-std", "backtrace", + "cfg-if 1.0.0", "ffi-support", - "futures", + "futures-util", "hostname", "jni", "lazy_static", @@ -5128,6 +5130,8 @@ dependencies = [ "serde", "serde_json", "tokio", + "tokio-stream", + "tokio-util 0.6.10", "tracing", "tracing-opentelemetry", "tracing-subscriber", diff --git a/veilid-flutter/rust/Cargo.toml b/veilid-flutter/rust/Cargo.toml index 1c87f745..deeea4d6 100644 --- a/veilid-flutter/rust/Cargo.toml +++ b/veilid-flutter/rust/Cargo.toml @@ -6,6 +6,11 @@ edition = "2021" [lib] crate-type = ["cdylib", "staticlib", "rlib"] +[features] +default = [ "rt-tokio" ] +rt-async-std = [ "veilid-core/rt-async-std", "async-std", "opentelemetry/rt-async-std", "opentelemetry-otlp/grpc-sys"] +rt-tokio = [ "veilid-core/rt-tokio", "tokio", "tokio-stream", "tokio-util", "opentelemetry/rt-tokio"] + [dependencies] tracing = { version = "^0", features = ["log", "attributes"] } tracing-subscriber = "^0" @@ -13,20 +18,24 @@ parking_lot = "^0" backtrace = "^0" serde_json = "^1" serde = "^1" -futures = "^0" +futures-util = { version = "^0", default_features = false, features = ["alloc"] } +cfg-if = "^1" # Dependencies for native builds only # Linux, Windows, Mac, iOS, Android [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -veilid-core = { path="../../veilid-core", features = [ "rt-tokio" ] } -tokio = { version = "^1", features = ["full"] } +veilid-core = { path="../../veilid-core" } +tracing-opentelemetry = "^0" +opentelemetry = { version = "^0" } +opentelemetry-otlp = { version = "^0" } +opentelemetry-semantic-conventions = "^0" +async-std = { version = "^1", features = ["unstable"], optional = true } +tokio = { version = "^1", features = ["full"], optional = true } +tokio-stream = { version = "^0", features = ["net"], optional = true } +tokio-util = { version = "^0", features = ["compat"], optional = true} allo-isolate = "^0" ffi-support = "^0" lazy_static = "^1" -tracing-opentelemetry = "^0" -opentelemetry = { version = "^0", features = ["rt-tokio"] } -opentelemetry-otlp = { version = "^0" } -opentelemetry-semantic-conventions = "^0" hostname = "^0" # Dependencies for WASM builds only diff --git a/veilid-flutter/rust/src/dart_ffi.rs b/veilid-flutter/rust/src/dart_ffi.rs index 80142870..5f07aa06 100644 --- a/veilid-flutter/rust/src/dart_ffi.rs +++ b/veilid-flutter/rust/src/dart_ffi.rs @@ -1,6 +1,7 @@ use crate::dart_isolate_wrapper::*; +use crate::tools::*; use allo_isolate::*; -use async_std::sync::Mutex as AsyncMutex; +use cfg_if::*; use ffi_support::*; use lazy_static::*; use opentelemetry::sdk::*; @@ -184,29 +185,38 @@ pub extern "C" fn configure_veilid_platform(platform_config: FfiStr) { platform_config.logging.otlp.level.to_tracing_level().into(); let grpc_endpoint = platform_config.logging.otlp.grpc_endpoint.clone(); + cfg_if! { + if #[cfg(feature="rt-async-std")] { + let exporter = opentelemetry_otlp::new_exporter() + .grpcio() + .with_endpoint(grpc_endpoint); + let batch = opentelemetry::runtime::AsyncStd; + } else if #[cfg(feature="rt-tokio")] { + let exporter = opentelemetry_otlp::new_exporter() + .tonic() + .with_endpoint(format!("http://{}", grpc_endpoint)); + let batch = opentelemetry::runtime::Tokio; + } + } + let tracer = opentelemetry_otlp::new_pipeline() .tracing() - .with_exporter( - opentelemetry_otlp::new_exporter() - .grpcio() - .with_endpoint(grpc_endpoint), - ) + .with_exporter(exporter) .with_trace_config(opentelemetry::sdk::trace::config().with_resource( Resource::new(vec![KeyValue::new( opentelemetry_semantic_conventions::resource::SERVICE_NAME, format!( - "{}:{}", - platform_config.logging.otlp.service_name, - hostname::get() - .map(|s| s.to_string_lossy().into_owned()) - .unwrap_or_else(|_| "unknown".to_owned()) - ), + "{}:{}", + platform_config.logging.otlp.service_name, + hostname::get() + .map(|s| s.to_string_lossy().into_owned()) + .unwrap_or_else(|_| "unknown".to_owned())), )]), )) - .install_batch(opentelemetry::runtime::AsyncStd) + .install_batch(batch) .map_err(|e| format!("failed to install OpenTelemetry tracer: {}", e)) - .expect("failed to initalize ffi platform"); + .unwrap(); let ignore_list = ignore_list.clone(); Some( @@ -235,7 +245,7 @@ pub extern "C" fn configure_veilid_platform(platform_config: FfiStr) { pub extern "C" fn startup_veilid_core(port: i64, config: FfiStr) { let config = config.into_opt_string(); let stream = DartIsolateStream::new(port); - async_std::task::spawn(async move { + spawn(async move { let config_json = match config { Some(v) => v, None => { diff --git a/veilid-flutter/rust/src/dart_isolate_wrapper.rs b/veilid-flutter/rust/src/dart_isolate_wrapper.rs index cbd01466..a3a92566 100644 --- a/veilid-flutter/rust/src/dart_isolate_wrapper.rs +++ b/veilid-flutter/rust/src/dart_isolate_wrapper.rs @@ -1,3 +1,4 @@ +use crate::tools::*; pub use allo_isolate::ffi::DartCObject; pub use allo_isolate::IntoDart; use allo_isolate::Isolate; @@ -35,7 +36,7 @@ impl DartIsolateWrapper { T: IntoDart + Debug, E: Serialize + Debug, { - async_std::task::spawn(async move { + spawn(async move { self.result(future.await); }); } @@ -46,7 +47,7 @@ impl DartIsolateWrapper { T: Serialize + Debug, E: Serialize + Debug, { - async_std::task::spawn(async move { + spawn(async move { self.result_json(future.await); }); } diff --git a/veilid-flutter/rust/src/lib.rs b/veilid-flutter/rust/src/lib.rs index f67c8499..30a72cc6 100644 --- a/veilid-flutter/rust/src/lib.rs +++ b/veilid-flutter/rust/src/lib.rs @@ -1,5 +1,6 @@ mod dart_ffi; mod dart_isolate_wrapper; +mod tools; #[cfg(target_os = "android")] use jni::{objects::JClass, objects::JObject, JNIEnv}; diff --git a/veilid-flutter/rust/src/tools.rs b/veilid-flutter/rust/src/tools.rs new file mode 100644 index 00000000..ccac253b --- /dev/null +++ b/veilid-flutter/rust/src/tools.rs @@ -0,0 +1,33 @@ +use cfg_if::*; +use core::future::Future; + +cfg_if! { + if #[cfg(feature="rt-async-std")] { + pub use async_std::task::JoinHandle; + pub use async_std::net::TcpStream; + pub use async_std::future::TimeoutError; + pub use async_std::sync::Mutex as AsyncMutex; + + pub fn spawn + Send + 'static, T: Send + 'static>(f: F) -> JoinHandle { + async_std::task::spawn(f) + } + + pub use async_std::task::sleep; + pub use async_std::future::timeout; + } else if #[cfg(feature="rt-tokio")] { + pub use tokio::task::JoinHandle; + pub use tokio::net::TcpStream; + pub use tokio::time::error::Elapsed as TimeoutError; + pub use tokio::sync::Mutex as AsyncMutex; + pub fn spawn + Send + 'static, T: Send + 'static>(f: F) -> JoinHandle { + GLOBAL_RUNTIME.spawn(f) + } + + pub use tokio::time::sleep; + pub use tokio::time::timeout; + lazy_static::lazy_static! { + static ref GLOBAL_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Runtime::new().unwrap(); + } + + } +} diff --git a/veilid-wasm/Cargo.toml b/veilid-wasm/Cargo.toml index 6bac02a7..c5cd2426 100644 --- a/veilid-wasm/Cargo.toml +++ b/veilid-wasm/Cargo.toml @@ -9,13 +9,15 @@ license = "LGPL-2.0-or-later OR MPL-2.0 OR (MIT AND BSD-3-Clause)" crate-type = ["cdylib", "rlib"] [dependencies] -wasm-bindgen = { version = "^0", features = ["serde-serialize"] } -console_error_panic_hook = "^0" -wee_alloc = "^0" +veilid-core = { path = "../veilid-core" } + tracing = { version = "^0", features = ["log", "attributes"] } tracing-wasm = "^0" tracing-subscriber = "^0" -veilid-core = { path = "../veilid-core" } + +wasm-bindgen = { version = "^0", features = ["serde-serialize"] } +console_error_panic_hook = "^0" +wee_alloc = "^0" cfg-if = "^1" wasm-bindgen-futures = "^0" js-sys = "^0"