From b6dd1dc1c354c8b7c843bfebd358780626ee0d46 Mon Sep 17 00:00:00 2001 From: Jorge Hermo Date: Sun, 5 Jan 2025 11:30:01 +0100 Subject: [PATCH] feat: wrap OperatorRegistry in Arc Mutex --- core/src/types/operator/registry.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/src/types/operator/registry.rs b/core/src/types/operator/registry.rs index f85257a6a10..a7f1c441030 100644 --- a/core/src/types/operator/registry.rs +++ b/core/src/types/operator/registry.rs @@ -17,6 +17,7 @@ use std::cell::LazyCell; use std::collections::HashMap; +use std::sync::{Arc, Mutex}; use http::Uri; @@ -35,21 +36,24 @@ pub type OperatorFactory = fn(&str, HashMap) -> Result // TODO: create an static registry? or a global() method of OperatorRegistry that lazily initializes the registry? // Register only services in `Scheme::enabled()` +#[derive(Clone, Debug)] pub struct OperatorRegistry { - // TODO: add Arc> to make it cheap to clone + thread safe? or is it not needed? - registry: HashMap, + registry: Arc>>, } impl OperatorRegistry { pub fn new() -> Self { Self { - registry: HashMap::new(), + registry: Default::default(), } } pub fn register(&mut self, scheme: &str, factory: OperatorFactory) { // TODO: should we receive a `&str` or a `String`? we are cloning it anyway - self.registry.insert(scheme.to_string(), factory); + self.registry + .lock() + .expect("poisoned lock") + .insert(scheme.to_string(), factory); } pub fn parse(