[go: up one dir, main page]

Children

Struct Children 

Source
pub struct Children { /* private fields */ }
Expand description

A children group that will contain a defined number of elements (set with with_redundancy or 1 by default) all running a future (returned by the closure that is set with with_exec).

When an element of the group stops or panics, all the elements will be stopped as well and the group’s supervisor will receive a notice that a stop or panic occurred (note that if a panic occurred, the supervisor will restart the children group and eventually some of its other children, depending on its SupervisionStrategy).

§Example

let children_ref: ChildrenRef = Bastion::children(|children| {
    // Configure the children group...
    children.with_exec(|ctx: BastionContext| {
        async move {
            // Send and receive messages...
            let opt_msg: Option<SignedMessage> = ctx.try_recv().await;
            // ...and return `Ok(())` or `Err(())` when you are done...
            Ok(())

            // Note that if `Err(())` was returned, the supervisor would
            // restart the children group.
        }
    })
    // ...and return it.
}).expect("Couldn't create the children group.");

Implementations§

Source§

impl Children

Source

pub fn id(&self) -> &BastionId

Returns this children group’s identifier.

Note that the children group’s identifier is reset when it is restarted.

§Example
Bastion::children(|children| {
    let children_id: &BastionId = children.id();
    // ...
}).expect("Couldn't create the children group.");
Source

pub fn with_name(self, name: impl Into<String>) -> Self

Sets the name of this children group.

Source

pub fn with_exec<I, F>(self, init: I) -> Self
where I: Fn(BastionContext) -> F + Send + 'static, F: Future<Output = Result<(), ()>> + Send + 'static,

Sets the closure taking a BastionContext and returning a Future that will be used by every element of this children group.

When a new element is started, it will be assigned a new context, pass it to the init closure and poll the returned future until it stops, panics or another element of the group stops or panics.

The returned future’s output should be Result<(), ()>.

§Arguments
  • init - The closure taking a BastionContext and returning a Future that will be used by every element of this children group.
§Example
Bastion::children(|children| {
    children.with_exec(|ctx| {
        async move {
            // Send and receive messages...
            let opt_msg: Option<SignedMessage> = ctx.try_recv().await;
            // ...and return `Ok(())` or `Err(())` when you are done...
            Ok(())

            // Note that if `Err(())` was returned, the supervisor would
            // restart the children group.
        }
    })
}).expect("Couldn't create the children group.");
Source

pub fn with_redundancy(self, redundancy: usize) -> Self

Sets the number of elements this children group will contain. Each element will call the closure passed in with_exec and run the returned future until it stops, panics or another element in the group stops or panics.

The default number of elements a children group contains is 1.

§Arguments
  • redundancy - The number of elements this group will contain.
§Example
Bastion::children(|children| {
    // Note that "1" is the default number of elements.
    children.with_redundancy(1)
}).expect("Couldn't create the children group.");
Source

pub fn with_dispatcher(self, dispatcher: Dispatcher) -> Self

Appends each supervised element to the declared dispatcher.

By default supervised elements aren’t added to any of dispatcher.

§Arguments
§Example
Bastion::children(|children| {
    children
        .with_dispatcher(
            Dispatcher::with_type(DispatcherType::Named("CustomGroup".to_string()))
        )
}).expect("Couldn't create the children group.");
Source

pub fn with_distributor(self, distributor: Distributor) -> Self

Appends a distributor to the children.

By default supervised elements aren’t added to any distributor.

§Arguments
  • distributor - An instance of struct that implements the RecipientHandler trait.
§Example
Bastion::children(|children| {
    children
        .with_distributor(Distributor::named("my distributor"))
}).expect("Couldn't create the children group.");
Source

pub fn with_resizer(self, resizer: OptimalSizeExploringResizer) -> Self

Sets a custom resizer for the Children.

This method is available only with the scaling feature flag.

§Arguments
  • resizer - An instance of the [Resizer] struct.
§Example
Bastion::children(|children| {
    children
        .with_redundancy(1)
        .with_resizer(
            OptimalSizeExploringResizer::default()
                .with_lower_bound(10)
                .with_upper_bound(UpperBound::Limit(100))
        )
}).expect("Couldn't create the children group.");
Source

pub fn with_callbacks(self, callbacks: Callbacks) -> Self

Sets the callbacks that will get called at this children group’s different lifecycle events.

See Callbacks’s documentation for more information about the different callbacks available.

§Arguments
  • callbacks - The callbacks that will get called for this children group.
§Example
Bastion::children(|children| {
    let callbacks = Callbacks::new()
        .with_before_start(|| println!("Children group started."))
        .with_after_stop(|| println!("Children group stopped."));

    children
        .with_callbacks(callbacks)
        .with_exec(|ctx| {
            // -- Children group started.
            async move {
                // ...
            }
            // -- Children group stopped.
        })
}).expect("Couldn't create the children group.");
Source

pub fn with_heartbeat_tick(self, interval: Duration) -> Self

Overrides the default time interval for heartbeat onto the user defined.

§Arguments
§Example
Bastion::children(|children| {
children
    .with_heartbeat_tick(Duration::from_secs(5))
    .with_exec(|ctx| {
        // -- Children group started.
        async move {
            // ...
        }
        // -- Children group stopped.
    })
}).expect("Couldn't create the children group.");

Trait Implementations§

Source§

impl Debug for Children

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> AsAny for T
where T: Any,

Source§

fn as_any(&mut self) -> &mut (dyn Any + 'static)

Downcast implemented type to Any.
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<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,