Skip to main content

PendingMsgs

Struct PendingMsgs 

Source
pub struct PendingMsgs<T, M>
where T: Msg<M>, M: Sized + Clone + Tvf,
{ /* private fields */ }
Expand description

ProSA pending message to keep track of the message and trigger a timeout if a message is expire This object is not thread safe, you must use it within the same Tokio thread

use std::time::Duration;
use prosa::event::pending::PendingMsgs;
use tokio::sync::mpsc::Receiver;
use prosa::core::msg::{Msg, RequestMsg, InternalMsg};
use prosa_utils::msg::simple_string_tvf::SimpleStringTvf;

async fn processing(mut queue: Receiver<InternalMsg<SimpleStringTvf>>) {
    let mut pending_msg: PendingMsgs<RequestMsg<SimpleStringTvf>, SimpleStringTvf> = Default::default();
    tokio::select! {
        Some(msg) = queue.recv() => {
            match msg {
                InternalMsg::Request(msg) => {
                    // Push in the pending message, the message will wait a timeout of 200ms
                    pending_msg.push(msg, Duration::from_millis(200));
                },
                InternalMsg::Response(msg) => {
                    let original_request: Option<RequestMsg<SimpleStringTvf>> = pending_msg.pull_msg(msg.get_id());
                    println!("Receive a response: {:?}, from original request {:?}", msg, original_request);
                },
                _ => {},
            }
        },
        Some(msg) = pending_msg.pull(), if !pending_msg.is_empty() => {
            println!("Timeout message {:?}", msg);
            // Do your processing
        },
    }
}

Implementations§

Source§

impl<T, M> PendingMsgs<T, M>
where T: Msg<M>, M: Sized + Clone + Tvf,

Source

pub fn len(&self) -> usize

Returns the number of pending messages, also referred to as its ‘length’.

Source

pub fn capacity(&self) -> usize

Returns the capacity of the internal message map.

Source

pub fn is_empty(&self) -> bool

Returns true if there is no pending message

Source

pub fn with_capacity(capacity: usize) -> Self
where T: Msg<M>, M: Sized + Clone + Tvf,

Method to create a new pending message list with a specific capacity

Source

pub fn push(&mut self, msg: T, timeout: Duration)

Method to push a pending message

Source

pub fn push_with_id(&mut self, id: u64, msg: T, timeout: Duration)

Method to push a pending message with a custom id

Source

pub fn pull_msg(&mut self, msg_id: u64) -> Option<T>

Method to pull a pending message to process it

Source

pub async fn pull(&mut self) -> Option<T>

Method to wait for expired message (timeout) If there is no pending message (is_empty == true) the method return immediatelly. It doesn’t block until a message is pending

use std::time::Duration;
use tokio::sync::mpsc::Sender;
use prosa::event::pending::PendingMsgs;
use prosa::core::msg::{Msg, RequestMsg, InternalMsg};
use prosa_utils::msg::simple_string_tvf::SimpleStringTvf;

async fn processing(tvf: SimpleStringTvf, queue: Sender<InternalMsg<SimpleStringTvf>>) {
    let mut pending_msg: PendingMsgs<RequestMsg<SimpleStringTvf>, SimpleStringTvf> = Default::default();
    let mut msg: Option<RequestMsg<SimpleStringTvf>> = pending_msg.pull().await;
    assert!(msg.is_none());
    pending_msg.push(RequestMsg::new(String::from("service"), tvf, queue), Duration::from_millis(200));
    tokio::select! {
        Some(msg) = pending_msg.pull(), if !pending_msg.is_empty() => {
            println!("Timeout message {:?}", msg);
        }
    }
}

Trait Implementations§

Source§

impl<T, M> Debug for PendingMsgs<T, M>
where T: Msg<M> + Debug, M: Sized + Clone + Tvf + Debug,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T, M> Default for PendingMsgs<T, M>
where T: Msg<M>, M: Sized + Clone + Tvf,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T, M> Freeze for PendingMsgs<T, M>

§

impl<T, M> RefUnwindSafe for PendingMsgs<T, M>

§

impl<T, M> Send for PendingMsgs<T, M>
where M: Send, T: Send,

§

impl<T, M> Sync for PendingMsgs<T, M>
where M: Sync, T: Sync,

§

impl<T, M> Unpin for PendingMsgs<T, M>
where M: Unpin, T: Unpin,

§

impl<T, M> UnsafeUnpin for PendingMsgs<T, M>

§

impl<T, M> UnwindSafe for PendingMsgs<T, M>
where T: UnwindSafe, M: UnwindSafe,

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> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
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<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