dynamo_runtime/component/
registry.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use super::{Component, Registry, RegistryInner, Result};
5use async_once_cell::OnceCell;
6use std::{
7    collections::HashMap,
8    sync::{Arc, Weak},
9};
10use tokio::sync::Mutex;
11
12impl Default for Registry {
13    fn default() -> Self {
14        Self::new()
15    }
16}
17
18impl Registry {
19    pub fn new() -> Self {
20        Self {
21            inner: Arc::new(Mutex::new(RegistryInner::default())),
22        }
23    }
24}
25
26// impl ComponentRegistry {
27//     pub fn new() -> Self {
28//         Self {
29//             clients: Arc::new(Mutex::new(HashMap::new())),
30//         }
31//     }
32
33//     pub async fn get_or_create(&mut self, component: Component) -> Result<Arc<Client>> {
34//         // Lock the clients HashMap for thread-safe access
35//         let mut guard = self.clients.lock().await;
36
37//         // Check if the component already exists in the registry
38//         if let Some(weak) = guard.get(&component.slug()) {
39//             // Attempt to upgrade the Weak pointer
40//             if let Some(client) = weak.upgrade() {
41//                 return Ok(client);
42//             }
43//         }
44
45//         // Fallback: Create a new Client
46//         let client = component.client().await?;
47
48//         // Insert a Weak reference to the new client into the map
49//         guard.insert(component.slug(), Arc::downgrade(&client));
50
51//         Ok(client)
52//     }
53// }
54
55// #[derive(Clone)]
56// pub struct ServiceRegistry {
57//     clients: Arc<Mutex<HashMap<String, Arc<Service>>>>,
58// }
59
60// impl ServiceRegistry {
61//     pub fn new() -> Self {
62//         Self {
63//             clients: Arc::new(Mutex::new(HashMap::new())),
64//         }
65//     }
66
67//     pub async fn get_or_create(&mut self, component: Component) -> Result<Arc<Client>> {
68//         // Lock the clients HashMap for thread-safe access
69//         let mut guard = self.clients.lock().await;
70
71//         // Check if the component already exists in the registry
72//         if let Some(weak) = guard.get(&component.slug()) {
73//             // Attempt to upgrade the Weak pointer
74//             if let Some(client) = weak.upgrade() {
75//                 return Ok(client);
76//             }
77//         }
78
79//         // Fallback: Create a new Client
80//         let client = component.client().await?;
81
82//         // Insert a Weak reference to the new client into the map
83//         guard.insert(component.slug(), Arc::downgrade(&client));
84
85//         Ok(client)
86//     }
87// }