Skip to content

Commit

Permalink
utils: introduce per-thread tokio current thread Runtime
Browse files Browse the repository at this point in the history
Introduce thread local variable of tokio current thread Runtime object.

Signed-off-by: Jiang Liu <[email protected]>
  • Loading branch information
jiangliu committed Jun 16, 2022
1 parent 10ba8a5 commit cd61cdd
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions storage/src/cache/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use nydus_utils::metrics::{BlobcacheMetrics, Metric};
use tokio::runtime::Runtime;

use nydus_api::http::BlobPrefetchConfig;
use nydus_utils::async_helper::with_runtime;
use nydus_utils::mpmc::Channel;

use crate::cache::{BlobCache, BlobIoRange};
Expand Down Expand Up @@ -199,11 +200,9 @@ impl AsyncWorkerMgr {
.prefetch_workers
.fetch_add(1, Ordering::Relaxed);

let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("storage: failed to create tokio runtime for current thread");
rt.block_on(Self::handle_prefetch_requests(mgr2.clone(), &rt));
with_runtime(|rt| {
rt.block_on(Self::handle_prefetch_requests(mgr2.clone(), &rt));
});

mgr2.metrics
.prefetch_workers
Expand Down
3 changes: 3 additions & 0 deletions utils/CHANGELOG.md
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
Expand Down
2 changes: 1 addition & 1 deletion utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nydus-utils"
version = "0.3.0"
version = "0.3.1"
description = "Compression/encryption/digest utilities for Nydus Image Service"
authors = ["The Nydus Developers"]
license = "Apache-2.0 OR BSD-3-Clause"
Expand Down
4 changes: 3 additions & 1 deletion utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

A collection of utilities to support [Nydus Image Service](https://nydus.dev/).
It provides:
- LZ4 and zstd compression algorithms
- Asynchronous Multi-Producer Multi-Consumer channel
- Blake3 and SHA256 message digest algorithms
- LZ4 and zstd compression algorithms
- `InodeBitmap`: a bitmap implementation to manage inode numbers
- Per-thread async runtime of type tokio current thread Runtime.
- exec() helper
- metric helpers

Expand Down
33 changes: 33 additions & 0 deletions utils/src/async_helper.rs
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);
}
}
1 change: 1 addition & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub use self::exec::*;
pub use self::inode_bitmap::InodeBitmap;
pub use self::types::*;

pub mod async_helper;
pub mod compress;
pub mod digest;
pub mod exec;
Expand Down

0 comments on commit cd61cdd

Please sign in to comment.