flutter tokio

This commit is contained in:
John Smith 2022-06-29 13:39:54 -04:00
parent e49f7a89c0
commit 8fbc985e9b
7 changed files with 89 additions and 29 deletions

6
Cargo.lock generated
View File

@ -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",

View File

@ -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

View File

@ -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 => {

View File

@ -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);
});
}

View File

@ -1,5 +1,6 @@
mod dart_ffi;
mod dart_isolate_wrapper;
mod tools;
#[cfg(target_os = "android")]
use jni::{objects::JClass, objects::JObject, JNIEnv};

View File

@ -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<F: Future<Output = T> + Send + 'static, T: Send + 'static>(f: F) -> JoinHandle<T> {
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<F: Future<Output = T> + Send + 'static, T: Send + 'static>(f: F) -> JoinHandle<T> {
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();
}
}
}

View File

@ -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"