pub struct PendingMsgs<T, M>{ /* 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>
impl<T, M> PendingMsgs<T, M>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of pending messages, also referred to as its ‘length’.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Method to create a new pending message list with a specific capacity
Sourcepub fn push_with_id(&mut self, id: u64, msg: T, timeout: Duration)
pub fn push_with_id(&mut self, id: u64, msg: T, timeout: Duration)
Method to push a pending message with a custom id
Sourcepub fn pull_msg(&mut self, msg_id: u64) -> Option<T>
pub fn pull_msg(&mut self, msg_id: u64) -> Option<T>
Method to pull a pending message to process it
Sourcepub async fn pull(&mut self) -> Option<T>
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>
impl<T, M> Debug for PendingMsgs<T, M>
Auto Trait Implementations§
impl<T, M> Freeze for PendingMsgs<T, M>
impl<T, M> RefUnwindSafe for PendingMsgs<T, M>where
M: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, M> Send for PendingMsgs<T, M>
impl<T, M> Sync for PendingMsgs<T, M>
impl<T, M> Unpin for PendingMsgs<T, M>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request