-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: move async-runtime to dir
type_config/async_runtime/
- Loading branch information
1 parent
6ffcd2c
commit b51cb8d
Showing
14 changed files
with
123 additions
and
106 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
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 @@ | ||
//! Collection of implementations of usually used traits defined by Openraft | ||
pub use crate::async_runtime::TokioRuntime; | ||
pub use crate::entry::Entry; | ||
pub use crate::node::BasicNode; | ||
pub use crate::node::EmptyNode; | ||
pub use crate::raft::responder::impls::OneshotResponder; | ||
pub use crate::type_config::async_runtime::impls::TokioRuntime; |
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
84 changes: 84 additions & 0 deletions
84
openraft/src/type_config/async_runtime/impls/tokio_runtime.rs
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,84 @@ | ||
use std::future::Future; | ||
use std::time::Duration; | ||
|
||
use crate::type_config::OneshotSender; | ||
use crate::AsyncRuntime; | ||
use crate::OptionalSend; | ||
use crate::TokioInstant; | ||
|
||
/// `Tokio` is the default asynchronous executor. | ||
#[derive(Debug, Default, PartialEq, Eq)] | ||
pub struct TokioRuntime; | ||
|
||
impl AsyncRuntime for TokioRuntime { | ||
type JoinError = tokio::task::JoinError; | ||
type JoinHandle<T: OptionalSend + 'static> = tokio::task::JoinHandle<T>; | ||
type Sleep = tokio::time::Sleep; | ||
type Instant = TokioInstant; | ||
type TimeoutError = tokio::time::error::Elapsed; | ||
type Timeout<R, T: Future<Output = R> + OptionalSend> = tokio::time::Timeout<T>; | ||
type ThreadLocalRng = rand::rngs::ThreadRng; | ||
type OneshotSender<T: OptionalSend> = tokio::sync::oneshot::Sender<T>; | ||
type OneshotReceiver<T: OptionalSend> = tokio::sync::oneshot::Receiver<T>; | ||
type OneshotReceiverError = tokio::sync::oneshot::error::RecvError; | ||
|
||
#[inline] | ||
fn spawn<T>(future: T) -> Self::JoinHandle<T::Output> | ||
where | ||
T: Future + OptionalSend + 'static, | ||
T::Output: OptionalSend + 'static, | ||
{ | ||
#[cfg(feature = "singlethreaded")] | ||
{ | ||
tokio::task::spawn_local(future) | ||
} | ||
#[cfg(not(feature = "singlethreaded"))] | ||
{ | ||
tokio::task::spawn(future) | ||
} | ||
} | ||
|
||
#[inline] | ||
fn sleep(duration: Duration) -> Self::Sleep { | ||
tokio::time::sleep(duration) | ||
} | ||
|
||
#[inline] | ||
fn sleep_until(deadline: Self::Instant) -> Self::Sleep { | ||
tokio::time::sleep_until(deadline) | ||
} | ||
|
||
#[inline] | ||
fn timeout<R, F: Future<Output = R> + OptionalSend>(duration: Duration, future: F) -> Self::Timeout<R, F> { | ||
tokio::time::timeout(duration, future) | ||
} | ||
|
||
#[inline] | ||
fn timeout_at<R, F: Future<Output = R> + OptionalSend>(deadline: Self::Instant, future: F) -> Self::Timeout<R, F> { | ||
tokio::time::timeout_at(deadline, future) | ||
} | ||
|
||
#[inline] | ||
fn is_panic(join_error: &Self::JoinError) -> bool { | ||
join_error.is_panic() | ||
} | ||
|
||
#[inline] | ||
fn thread_rng() -> Self::ThreadLocalRng { | ||
rand::thread_rng() | ||
} | ||
|
||
#[inline] | ||
fn oneshot<T>() -> (Self::OneshotSender<T>, Self::OneshotReceiver<T>) | ||
where T: OptionalSend { | ||
let (tx, rx) = tokio::sync::oneshot::channel(); | ||
(tx, rx) | ||
} | ||
} | ||
|
||
impl<T> OneshotSender<T> for tokio::sync::oneshot::Sender<T> { | ||
#[inline] | ||
fn send(self, t: T) -> Result<(), T> { | ||
self.send(t) | ||
} | ||
} |
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,11 @@ | ||
pub trait OneshotSender<T> { | ||
/// Attempts to send a value on this channel, returning it back if it could | ||
/// not be sent. | ||
/// | ||
/// This method consumes `self` as only one value may ever be sent on a `oneshot` | ||
/// channel. It is not marked async because sending a message to an `oneshot` | ||
/// channel never requires any form of waiting. Because of this, the `send` | ||
/// method can be used in both synchronous and asynchronous code without | ||
/// problems. | ||
fn send(self, t: T) -> Result<(), T>; | ||
} |
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