Skip to main content

ComponentRegistry

Struct ComponentRegistry 

Source
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

Source

pub fn new() -> Self

Construct an empty registry with a fresh shutdown token.

Source

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.

Source

pub fn shutdown(&self) -> ShutdownToken

Borrow the root shutdown token.

Source

pub fn register_factory( &self, descriptor: ComponentDescriptor, factory: Box<dyn ComponentFactory>, ) -> Result<(), RegistryError>

Register a factory. Invalidates the cached topological order.

§Errors
Source

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.

Source

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.

Source

pub fn len(&self) -> usize

Number of registered components.

Source

pub fn is_empty(&self) -> bool

Returns true if no components are registered.

Source

pub fn names(&self) -> Vec<String>

Returns the user-assigned names of all registered components, in topological init order.

Source

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
Source

pub fn is_initialized(&self, name: &str) -> bool

Returns true if a component with the given name has been successfully initialized.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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:

  1. Verify the named component is in ComponentState::Running.
  2. Call AnyComponent::pre_replace on the old instance, giving it a chance to reject new traffic or flush buffers.
  3. Call AnyComponent::start on the new instance. If this fails, the old instance remains in place.
  4. Swap the instance in the registry map. Existing Arc clones held by other tasks remain valid and keep the old instance alive until they are dropped (natural drain).
  5. Call AnyComponent::post_replace on 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
Source

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.

Source

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.

Trait Implementations§

Source§

impl Default for ComponentRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more