-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils: introduce per-thread tokio current thread Runtime
Introduce thread local variable of tokio current thread Runtime object. Signed-off-by: Jiang Liu <[email protected]>
- Loading branch information
Showing
7 changed files
with
46 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
# Changelog | ||
## [Unreleased] | ||
|
||
## [v0.3.1] | ||
- Add async io helpers | ||
|
||
## [v0.3] | ||
|
||
### Added | ||
|
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,33 @@ | ||
// Copyright (C) 2022 Alibaba Cloud. All rights reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
use tokio::runtime::{Builder, Runtime}; | ||
|
||
std::thread_local! { | ||
static CURRENT_THREAD_RT: Runtime = Builder::new_current_thread() | ||
.enable_all() | ||
.build() | ||
.expect("utils: failed to create tokio runtime for current thread"); | ||
} | ||
|
||
/// Run the callback with a tokio current thread runtime instance. | ||
pub fn with_runtime<F, R>(f: F) -> R | ||
where | ||
F: FnOnce(&Runtime) -> R, | ||
{ | ||
CURRENT_THREAD_RT.with(f) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_with_runtime() { | ||
let res = with_runtime(|rt| rt.block_on(async { 1 })); | ||
assert_eq!(res, 1); | ||
|
||
let res = with_runtime(|rt| rt.block_on(async { 3 })); | ||
assert_eq!(res, 3); | ||
} | ||
} |
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