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" version = "0.1.0"
dependencies = [ dependencies = [
"allo-isolate", "allo-isolate",
"async-std",
"backtrace", "backtrace",
"cfg-if 1.0.0",
"ffi-support", "ffi-support",
"futures", "futures-util",
"hostname", "hostname",
"jni", "jni",
"lazy_static", "lazy_static",
@ -5128,6 +5130,8 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"tokio", "tokio",
"tokio-stream",
"tokio-util 0.6.10",
"tracing", "tracing",
"tracing-opentelemetry", "tracing-opentelemetry",
"tracing-subscriber", "tracing-subscriber",

View File

@ -6,6 +6,11 @@ edition = "2021"
[lib] [lib]
crate-type = ["cdylib", "staticlib", "rlib"] 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] [dependencies]
tracing = { version = "^0", features = ["log", "attributes"] } tracing = { version = "^0", features = ["log", "attributes"] }
tracing-subscriber = "^0" tracing-subscriber = "^0"
@ -13,20 +18,24 @@ parking_lot = "^0"
backtrace = "^0" backtrace = "^0"
serde_json = "^1" serde_json = "^1"
serde = "^1" serde = "^1"
futures = "^0" futures-util = { version = "^0", default_features = false, features = ["alloc"] }
cfg-if = "^1"
# Dependencies for native builds only # Dependencies for native builds only
# Linux, Windows, Mac, iOS, Android # Linux, Windows, Mac, iOS, Android
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
veilid-core = { path="../../veilid-core", features = [ "rt-tokio" ] } veilid-core = { path="../../veilid-core" }
tokio = { version = "^1", features = ["full"] } 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" allo-isolate = "^0"
ffi-support = "^0" ffi-support = "^0"
lazy_static = "^1" lazy_static = "^1"
tracing-opentelemetry = "^0"
opentelemetry = { version = "^0", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "^0" }
opentelemetry-semantic-conventions = "^0"
hostname = "^0" hostname = "^0"
# Dependencies for WASM builds only # Dependencies for WASM builds only

View File

@ -1,6 +1,7 @@
use crate::dart_isolate_wrapper::*; use crate::dart_isolate_wrapper::*;
use crate::tools::*;
use allo_isolate::*; use allo_isolate::*;
use async_std::sync::Mutex as AsyncMutex; use cfg_if::*;
use ffi_support::*; use ffi_support::*;
use lazy_static::*; use lazy_static::*;
use opentelemetry::sdk::*; use opentelemetry::sdk::*;
@ -184,14 +185,24 @@ pub extern "C" fn configure_veilid_platform(platform_config: FfiStr) {
platform_config.logging.otlp.level.to_tracing_level().into(); platform_config.logging.otlp.level.to_tracing_level().into();
let grpc_endpoint = platform_config.logging.otlp.grpc_endpoint.clone(); 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 = let tracer =
opentelemetry_otlp::new_pipeline() opentelemetry_otlp::new_pipeline()
.tracing() .tracing()
.with_exporter( .with_exporter(exporter)
opentelemetry_otlp::new_exporter()
.grpcio()
.with_endpoint(grpc_endpoint),
)
.with_trace_config(opentelemetry::sdk::trace::config().with_resource( .with_trace_config(opentelemetry::sdk::trace::config().with_resource(
Resource::new(vec![KeyValue::new( Resource::new(vec![KeyValue::new(
opentelemetry_semantic_conventions::resource::SERVICE_NAME, opentelemetry_semantic_conventions::resource::SERVICE_NAME,
@ -200,13 +211,12 @@ pub extern "C" fn configure_veilid_platform(platform_config: FfiStr) {
platform_config.logging.otlp.service_name, platform_config.logging.otlp.service_name,
hostname::get() hostname::get()
.map(|s| s.to_string_lossy().into_owned()) .map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|_| "unknown".to_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)) .map_err(|e| format!("failed to install OpenTelemetry tracer: {}", e))
.expect("failed to initalize ffi platform"); .unwrap();
let ignore_list = ignore_list.clone(); let ignore_list = ignore_list.clone();
Some( 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) { pub extern "C" fn startup_veilid_core(port: i64, config: FfiStr) {
let config = config.into_opt_string(); let config = config.into_opt_string();
let stream = DartIsolateStream::new(port); let stream = DartIsolateStream::new(port);
async_std::task::spawn(async move { spawn(async move {
let config_json = match config { let config_json = match config {
Some(v) => v, Some(v) => v,
None => { None => {

View File

@ -1,3 +1,4 @@
use crate::tools::*;
pub use allo_isolate::ffi::DartCObject; pub use allo_isolate::ffi::DartCObject;
pub use allo_isolate::IntoDart; pub use allo_isolate::IntoDart;
use allo_isolate::Isolate; use allo_isolate::Isolate;
@ -35,7 +36,7 @@ impl DartIsolateWrapper {
T: IntoDart + Debug, T: IntoDart + Debug,
E: Serialize + Debug, E: Serialize + Debug,
{ {
async_std::task::spawn(async move { spawn(async move {
self.result(future.await); self.result(future.await);
}); });
} }
@ -46,7 +47,7 @@ impl DartIsolateWrapper {
T: Serialize + Debug, T: Serialize + Debug,
E: Serialize + Debug, E: Serialize + Debug,
{ {
async_std::task::spawn(async move { spawn(async move {
self.result_json(future.await); self.result_json(future.await);
}); });
} }

View File

@ -1,5 +1,6 @@
mod dart_ffi; mod dart_ffi;
mod dart_isolate_wrapper; mod dart_isolate_wrapper;
mod tools;
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
use jni::{objects::JClass, objects::JObject, JNIEnv}; 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"] crate-type = ["cdylib", "rlib"]
[dependencies] [dependencies]
wasm-bindgen = { version = "^0", features = ["serde-serialize"] } veilid-core = { path = "../veilid-core" }
console_error_panic_hook = "^0"
wee_alloc = "^0"
tracing = { version = "^0", features = ["log", "attributes"] } tracing = { version = "^0", features = ["log", "attributes"] }
tracing-wasm = "^0" tracing-wasm = "^0"
tracing-subscriber = "^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" cfg-if = "^1"
wasm-bindgen-futures = "^0" wasm-bindgen-futures = "^0"
js-sys = "^0" js-sys = "^0"