-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reuse the current runtime if possible.
Signed-off-by: ChenYing Kuo <[email protected]>
- Loading branch information
Showing
2 changed files
with
26 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
use std::{collections::HashMap, fs::DirBuilder, io::prelude::*, path::PathBuf}; | ||
use std::{collections::HashMap, fs::DirBuilder, future::Future, io::prelude::*, path::PathBuf}; | ||
|
||
use async_trait::async_trait; | ||
use tempfile::tempfile_in; | ||
|
@@ -38,13 +38,28 @@ use files_mgt::*; | |
const WORKER_THREAD_NUM: usize = 2; | ||
const MAX_BLOCK_THREAD_NUM: usize = 50; | ||
lazy_static::lazy_static! { | ||
pub static ref TOKIO_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread() | ||
// 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(WORKER_THREAD_NUM) | ||
.max_blocking_threads(MAX_BLOCK_THREAD_NUM) | ||
.enable_all() | ||
.build() | ||
.expect("Unable to create runtime"); | ||
} | ||
#[inline(always)] | ||
fn blockon_runtime<F: Future>(task: F) -> F::Output { | ||
// 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 | ||
tokio::task::block_in_place(|| rt.block_on(task)) | ||
} | ||
Err(_) => { | ||
// Unable to get the current runtime (dynamic plugins), spawn on the global runtime | ||
tokio::task::block_in_place(|| TOKIO_RUNTIME.block_on(task)) | ||
} | ||
} | ||
} | ||
|
||
/// The environement variable used to configure the root of all storages managed by this FileSystemBackend. | ||
pub const SCOPE_ENV_VAR: &str = "ZENOH_BACKEND_FS_ROOT"; | ||
|