-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: add 'singlethreaded' raft mode (#878)
* Feature: add 'singlethreaded' raft mode - 'singlethreaded' compile-time feature gate. The new feature gate forces the raft instance to be used by a single thread by not implementing `Send` for certain data structures and substituting calls to `tokio::spawn` with `tokio::spawn_local` when using the `tokio` asynchronous runtime. - Re-export `add_async_trait` for application to define a `!Send` async trait. - Fix: #862
- Loading branch information
Showing
21 changed files
with
167 additions
and
57 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "macros" | ||
|
||
version = { workspace = true } | ||
edition = { workspace = true } | ||
authors = { workspace = true } | ||
categories = { workspace = true } | ||
description = { workspace = true } | ||
documentation = { workspace = true } | ||
homepage = { workspace = true } | ||
keywords = { workspace = true } | ||
license = { workspace = true } | ||
repository = { workspace = true } | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[features] | ||
|
||
# Passes `?Send` to `async_trait` to force affected tasks to be spawned in the current thread. | ||
singlethreaded = [] | ||
|
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,21 @@ | ||
use proc_macro::TokenStream; | ||
|
||
/// This macro either emits `#[async_trait::async_trait]` or `#[asnc_trait::async_trait(?Send)]` | ||
/// based on the activated feature set. | ||
/// | ||
/// This assumes that the `[async_trait](https://crates.io/crates/async-trait)` crate is imported | ||
/// as `async_trait`. If the `singlethreaded` feature is enabled, `?Send` is passed to | ||
/// `async_trait`, thereby forcing the affected asynchronous trait functions and methods to be run | ||
/// in the same thread. | ||
#[proc_macro_attribute] | ||
pub fn add_async_trait(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
if cfg!(feature = "singlethreaded") { | ||
let mut output = "#[async_trait::async_trait(?Send)]".parse::<TokenStream>().unwrap(); | ||
output.extend(item); | ||
output | ||
} else { | ||
let mut output = "#[async_trait::async_trait]".parse::<TokenStream>().unwrap(); | ||
output.extend(item); | ||
output | ||
} | ||
} |
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
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
Oops, something went wrong.