Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use once cell to store registry #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,11 @@ struct FailPointRegistry {
registry: RwLock<Registry>,
}

use once_cell::sync::Lazy;
use once_cell::sync::{Lazy, OnceCell};

static REGISTRY: Lazy<FailPointRegistry> = Lazy::new(FailPointRegistry::default);
static SCENARIO: Lazy<Mutex<&'static FailPointRegistry>> = Lazy::new(|| Mutex::new(&REGISTRY));
static REGISTRY: OnceCell<FailPointRegistry> = OnceCell::new();
static SCENARIO: Lazy<Mutex<&'static FailPointRegistry>> =
Lazy::new(|| Mutex::new(REGISTRY.get_or_init(Default::default)));

/// Test scenario with configured fail points.
#[derive(Debug)]
Expand Down Expand Up @@ -624,7 +625,11 @@ pub const fn has_failpoints() -> bool {
///
/// Return a vector of `(name, actions)` pairs.
pub fn list() -> Vec<(String, String)> {
let registry = REGISTRY.registry.read().unwrap();
let registry = if let Some(r) = REGISTRY.get() {
r.registry.read().unwrap()
} else {
return Vec::new();
};
registry
.iter()
.map(|(name, fp)| (name.to_string(), fp.actions_str.read().unwrap().clone()))
Expand All @@ -633,8 +638,13 @@ pub fn list() -> Vec<(String, String)> {

#[doc(hidden)]
pub fn eval<R, F: FnOnce(Option<String>) -> R>(name: &str, f: F) -> Option<R> {
let registry = if let Some(r) = REGISTRY.get() {
&r.registry
} else {
return None;
};
let p = {
let registry = REGISTRY.registry.read().unwrap();
let registry = registry.read().unwrap();
match registry.get(name) {
None => return None,
Some(p) => p.clone(),
Expand Down Expand Up @@ -674,7 +684,11 @@ pub fn eval<R, F: FnOnce(Option<String>) -> R>(name: &str, f: F) -> Option<R> {
/// A call to `cfg` with a particular fail point name overwrites any existing actions for
/// that fail point, including those set via the `FAILPOINTS` environment variable.
pub fn cfg<S: Into<String>>(name: S, actions: &str) -> Result<(), String> {
let mut registry = REGISTRY.registry.write().unwrap();
let mut registry = REGISTRY
.get_or_init(Default::default)
.registry
.write()
.unwrap();
set(&mut registry, name.into(), actions)
}

Expand All @@ -687,7 +701,11 @@ where
S: Into<String>,
F: Fn() + Send + Sync + 'static,
{
let mut registry = REGISTRY.registry.write().unwrap();
let mut registry = REGISTRY
.get_or_init(Default::default)
.registry
.write()
.unwrap();
let p = registry
.entry(name.into())
.or_insert_with(|| Arc::new(FailPoint::new()));
Expand All @@ -701,7 +719,11 @@ where
///
/// If the fail point doesn't exist, nothing will happen.
pub fn remove<S: AsRef<str>>(name: S) {
let mut registry = REGISTRY.registry.write().unwrap();
let mut registry = if let Some(r) = REGISTRY.get() {
r.registry.write().unwrap()
} else {
return;
};
if let Some(p) = registry.remove(name.as_ref()) {
// wake up all pause failpoint.
p.set_actions("", vec![]);
Expand Down