pub struct ComponentRegistry { /* private fields */ }Expand description
The central component registry.
Cloning a ComponentRegistry is cheap: it is backed by Arc-shared
inner state. All clones observe the same set of registered components.
Implementations§
Source§impl ComponentRegistry
impl ComponentRegistry
Sourcepub fn with_shutdown(shutdown: ShutdownToken) -> Self
pub fn with_shutdown(shutdown: ShutdownToken) -> Self
Construct a registry that hands shutdown to every component on
init. Used by the future ManagedRuntime to wire a single root
shutdown into every component.
Sourcepub fn shutdown(&self) -> ShutdownToken
pub fn shutdown(&self) -> ShutdownToken
Borrow the root shutdown token.
Sourcepub fn register_factory(
&self,
descriptor: ComponentDescriptor,
factory: Box<dyn ComponentFactory>,
) -> Result<(), RegistryError>
pub fn register_factory( &self, descriptor: ComponentDescriptor, factory: Box<dyn ComponentFactory>, ) -> Result<(), RegistryError>
Register a factory. Invalidates the cached topological order.
§Errors
RegistryError::AlreadyRegisteredif the name is taken.
Sourcepub fn register_typed<C: Component>(
&self,
instance_name: impl Into<String>,
config: Value,
) -> Result<(), RegistryError>
pub fn register_typed<C: Component>( &self, instance_name: impl Into<String>, config: Value, ) -> Result<(), RegistryError>
Register a typed component using its Component::NAME constant
and Component::depends_on metadata.
Sourcepub fn unregister(
&self,
name: &str,
) -> Result<ComponentDescriptor, RegistryError>
pub fn unregister( &self, name: &str, ) -> Result<ComponentDescriptor, RegistryError>
Unregister a component. Returns the previous descriptor, or
RegistryError::NotFound if the name was not registered.
Sourcepub fn names(&self) -> Vec<String>
pub fn names(&self) -> Vec<String>
Returns the user-assigned names of all registered components, in topological init order.
Sourcepub fn get<C: Component>(&self, name: &str) -> Result<Arc<C>, RegistryError>
pub fn get<C: Component>(&self, name: &str) -> Result<Arc<C>, RegistryError>
Look up an initialized component by name, downcasting to a
concrete Arc<C>.
§Errors
RegistryError::NotFoundif the name is not registered.RegistryError::TypeMismatchif the stored instance is not of typeC.RegistryError::AlreadyInitializedis not raised here; a registered component without an instance simply returnsNotFound.
Sourcepub fn is_initialized(&self, name: &str) -> bool
pub fn is_initialized(&self, name: &str) -> bool
Returns true if a component with the given name has been
successfully initialized.
Sourcepub fn state_of(&self, name: &str) -> Option<ComponentState>
pub fn state_of(&self, name: &str) -> Option<ComponentState>
Snapshot of the current state of a component, or None if the
name is not registered.
Sourcepub async fn init_all(&self) -> Result<(), RegistryError>
pub async fn init_all(&self) -> Result<(), RegistryError>
Initialize all registered components in topological order.
On error from any single component, the registry records the
failure and continues with components that do not depend on the
failed one. The returned error is the first error encountered
(the rest are recorded in ComponentState::Failed).
After a successful init_all, subsequent init_all calls are
no-ops for already-initialized components. To re-init, unregister
the component and re-register it.
Sourcepub async fn start_all(&self) -> Result<(), RegistryError>
pub async fn start_all(&self) -> Result<(), RegistryError>
Start all initialized components in topo order. Components that
are not in ComponentState::Initialized (e.g. failed, already
running) are skipped.
Sourcepub async fn stop_all(&self) -> Result<(), RegistryError>
pub async fn stop_all(&self) -> Result<(), RegistryError>
Stop all running components in reverse topo order. Continues even on per-component failure so a stuck component does not prevent the rest of the system from draining.
Sourcepub async fn replace_instance(
&self,
name: &str,
new_instance: Box<dyn AnyComponent>,
) -> Result<Arc<dyn AnyComponent>, RegistryError>
pub async fn replace_instance( &self, name: &str, new_instance: Box<dyn AnyComponent>, ) -> Result<Arc<dyn AnyComponent>, RegistryError>
Atomically replace a running component with a new instance, following the drain-aware hot-swap protocol.
The protocol proceeds in five steps:
- Verify the named component is in
ComponentState::Running. - Call
AnyComponent::pre_replaceon the old instance, giving it a chance to reject new traffic or flush buffers. - Call
AnyComponent::starton the new instance. If this fails, the old instance remains in place. - Swap the instance in the registry map. Existing
Arcclones held by other tasks remain valid and keep the old instance alive until they are dropped (natural drain). - Call
AnyComponent::post_replaceon the old instance (best-effort; errors are reported but do not roll back).
Returns the old AnyComponent so the caller may await
explicit drain or call stop when appropriate.
§Errors
RegistryError::NotFoundif no component withnameexists.RegistryError::Reloadif the component is not inRunningstate, or ifpre_replace/startfails.
Sourcepub async fn health(&self) -> HashMap<String, HealthStatus>
pub async fn health(&self) -> HashMap<String, HealthStatus>
Aggregate health of every initialized component. Calls
AnyComponent::health for each in topo order and collects the
results.
Sourcepub fn recompute_topo(&self) -> Result<Vec<String>, RegistryError>
pub fn recompute_topo(&self) -> Result<Vec<String>, RegistryError>
Recompute the topological init order from the registered descriptors. Caches the result until the next registration.