Skip to content

Commit

Permalink
Make the globalmetadaprovider the single owner for each custom provid…
Browse files Browse the repository at this point in the history
…er (#134)
  • Loading branch information
patnebe authored Jan 15, 2025
1 parent 466503a commit f57f194
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions lightswitch-metadata/src/metadata_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub trait TaskMetadataProvider {
task_key: TaskKey,
) -> Result<Vec<MetadataLabel>, TaskMetadataProviderError>;
}
pub type ThreadSafeTaskMetadataProvider = Arc<Mutex<Box<dyn TaskMetadataProvider + Send>>>;

#[derive(Debug, Error)]
pub enum SystemMetadataProviderError {
Expand All @@ -48,13 +47,11 @@ pub trait SystemMetadataProvider {
fn get_metadata(&self) -> Result<Vec<MetadataLabel>, SystemMetadataProviderError>;
}

pub type ThreadSafeSystemMetadataProvider = Arc<Mutex<Box<dyn SystemMetadataProvider + Send>>>;

pub struct GlobalMetadataProvider {
pid_label_cache: LruCache</*pid*/ i32, Vec<MetadataLabel>>,
default_system_metadata: SystemMetadata,
custom_system_metadata_providers: Vec<ThreadSafeSystemMetadataProvider>,
custom_task_metadata_providers: Vec<ThreadSafeTaskMetadataProvider>,
custom_system_metadata_providers: Vec<Box<dyn SystemMetadataProvider + Send>>,
custom_task_metadata_providers: Vec<Box<dyn TaskMetadataProvider + Send>>,
}

pub type ThreadSafeGlobalMetadataProvider = Arc<Mutex<GlobalMetadataProvider>>;
Expand All @@ -77,14 +74,14 @@ impl GlobalMetadataProvider {

pub fn register_task_metadata_providers(
&mut self,
providers: Vec<ThreadSafeTaskMetadataProvider>,
providers: Vec<Box<dyn TaskMetadataProvider + Send>>,
) {
self.custom_task_metadata_providers.extend(providers);
}

pub fn register_system_metadata_providers(
&mut self,
providers: Vec<ThreadSafeSystemMetadataProvider>,
providers: Vec<Box<dyn SystemMetadataProvider + Send>>,
) {
self.custom_system_metadata_providers.extend(providers);
}
Expand All @@ -97,7 +94,7 @@ impl GlobalMetadataProvider {
.unwrap_or_default();

for provider in &self.custom_system_metadata_providers {
match provider.lock().unwrap().get_metadata() {
match provider.get_metadata() {
Ok(custom_system_labels) => {
labels.extend(custom_system_labels.into_iter());
}
Expand All @@ -108,7 +105,7 @@ impl GlobalMetadataProvider {
}

for provider in &self.custom_task_metadata_providers {
match provider.lock().unwrap().get_metadata(task_key) {
match provider.get_metadata(task_key) {
Ok(custom_task_labels) => {
labels.extend(custom_task_labels.into_iter());
}
Expand Down

0 comments on commit f57f194

Please sign in to comment.