-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1055 from neon-bindings/kv/async-runtime
feat(neon): Add tokio async runtime support
- Loading branch information
Showing
17 changed files
with
515 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[alias] | ||
# Neon defines mutually exclusive feature flags which prevents using `cargo clippy --all-features` | ||
# The following aliases simplify linting the entire workspace | ||
neon-check = " check --all --all-targets --features napi-experimental,futures,external-buffers,serde" | ||
neon-clippy = "clippy --all --all-targets --features napi-experimental,futures,external-buffers,serde -- -A clippy::missing_safety_doc" | ||
neon-test = " test --all --features=doc-dependencies,doc-comment,napi-experimental,futures,external-buffers,serde" | ||
neon-doc = " rustdoc -p neon --features=doc-dependencies,napi-experimental,futures,external-buffers,sys,serde -- --cfg docsrs" | ||
neon-check = " check --all --all-targets --features napi-experimental,external-buffers,serde,tokio" | ||
neon-clippy = "clippy --all --all-targets --features napi-experimental,external-buffers,serde,tokio -- -A clippy::missing_safety_doc" | ||
neon-test = " test --all --features=doc-dependencies,doc-comment,napi-experimental,external-buffers,serde,tokio" | ||
neon-doc = " rustdoc -p neon --features=doc-dependencies,napi-experimental,external-buffers,sys,serde,tokio -- --cfg docsrs" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::{future::Future, pin::Pin}; | ||
|
||
use crate::{context::Cx, thread::LocalKey}; | ||
|
||
#[cfg(feature = "tokio-rt")] | ||
pub(crate) mod tokio; | ||
|
||
type BoxFuture = Pin<Box<dyn Future<Output = ()> + Send + 'static>>; | ||
|
||
pub(crate) static RUNTIME: LocalKey<Box<dyn Runtime>> = LocalKey::new(); | ||
|
||
pub trait Runtime: Send + Sync + 'static { | ||
fn spawn(&self, fut: BoxFuture); | ||
} | ||
|
||
/// Register a [`Future`] executor runtime globally to the addon. | ||
/// | ||
/// Returns `Ok(())` if a global executor has not been set and `Err(runtime)` if it has. | ||
/// | ||
/// If the `tokio` feature flag is enabled and the addon does not provide a | ||
/// [`#[neon::main]`](crate::main) function, a multithreaded tokio runtime will be | ||
/// automatically registered. | ||
/// | ||
/// **Note**: Each instance of the addon will have its own runtime. It is recommended | ||
/// to initialize the async runtime once in a process global and share it across instances. | ||
/// | ||
/// ``` | ||
/// # #[cfg(feature = "tokio-rt-multi-thread")] | ||
/// # fn example() { | ||
/// # use neon::prelude::*; | ||
/// use once_cell::sync::OnceCell; | ||
/// use tokio::runtime::Runtime; | ||
/// | ||
/// static RUNTIME: OnceCell<Runtime> = OnceCell::new(); | ||
/// | ||
/// #[neon::main] | ||
/// fn main(mut cx: ModuleContext) -> NeonResult<()> { | ||
/// let runtime = RUNTIME | ||
/// .get_or_try_init(Runtime::new) | ||
/// .or_else(|err| cx.throw_error(err.to_string()))?; | ||
/// | ||
/// let _ = neon::set_global_executor(&mut cx, runtime); | ||
/// | ||
/// Ok(()) | ||
/// } | ||
/// # } | ||
/// ``` | ||
pub fn set_global_executor<R>(cx: &mut Cx, runtime: R) -> Result<(), R> | ||
where | ||
R: Runtime, | ||
{ | ||
if RUNTIME.get(cx).is_some() { | ||
return Err(runtime); | ||
} | ||
|
||
RUNTIME.get_or_init(cx, || Box::new(runtime)); | ||
|
||
Ok(()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::sync::Arc; | ||
|
||
use super::{BoxFuture, Runtime}; | ||
|
||
impl Runtime for tokio::runtime::Runtime { | ||
fn spawn(&self, fut: BoxFuture) { | ||
spawn(self.handle(), fut); | ||
} | ||
} | ||
|
||
impl Runtime for Arc<tokio::runtime::Runtime> { | ||
fn spawn(&self, fut: BoxFuture) { | ||
spawn(self.handle(), fut); | ||
} | ||
} | ||
|
||
impl Runtime for &'static tokio::runtime::Runtime { | ||
fn spawn(&self, fut: BoxFuture) { | ||
spawn(self.handle(), fut); | ||
} | ||
} | ||
|
||
impl Runtime for tokio::runtime::Handle { | ||
fn spawn(&self, fut: BoxFuture) { | ||
spawn(self, fut); | ||
} | ||
} | ||
|
||
impl Runtime for &'static tokio::runtime::Handle { | ||
fn spawn(&self, fut: BoxFuture) { | ||
spawn(self, fut); | ||
} | ||
} | ||
|
||
fn spawn(handle: &tokio::runtime::Handle, fut: BoxFuture) { | ||
#[allow(clippy::let_underscore_future)] | ||
let _ = handle.spawn(fut); | ||
} | ||
|
||
#[cfg(feature = "tokio-rt-multi-thread")] | ||
pub(crate) fn init(cx: &mut crate::context::ModuleContext) -> crate::result::NeonResult<()> { | ||
use once_cell::sync::OnceCell; | ||
use tokio::runtime::{Builder, Runtime}; | ||
|
||
use crate::context::Context; | ||
|
||
static RUNTIME: OnceCell<Runtime> = OnceCell::new(); | ||
|
||
super::RUNTIME.get_or_try_init(cx, |cx| { | ||
let runtime = RUNTIME | ||
.get_or_try_init(|| { | ||
#[cfg(feature = "tokio-rt-multi-thread")] | ||
let mut builder = Builder::new_multi_thread(); | ||
|
||
#[cfg(not(feature = "tokio-rt-multi-thread"))] | ||
let mut builder = Builder::new_current_thread(); | ||
|
||
builder.enable_all().build() | ||
}) | ||
.or_else(|err| cx.throw_error(err.to_string()))?; | ||
|
||
Ok(Box::new(runtime)) | ||
})?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.