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

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