pub struct Regulator { /* private fields */ }Expand description
Transaction regulator use to asynchronously regulate flow to fixed TPS
use std::time::Duration;
use tokio::sync::mpsc;
use prosa::event::speed::Regulator;
async fn queue_regulation(regulator: &mut Regulator, tx: mpsc::Sender<u16>, mut rx: mpsc::Receiver<u16>) {
tokio::select! {
_ = rx.recv() => {
// Specify a response time if you have it to avoid spamming a sick receiver
regulator.notify_receive_transaction(Duration::default());
}
_ = regulator.tick() => {
// Can send a transaction
tx.send(1234);
regulator.notify_send_transaction();
}
};
}Implementations§
Source§impl Regulator
impl Regulator
Sourcepub fn new(
max_speed: f64,
timeout_threshold: Duration,
max_concurrents_send: u32,
speed_interval: u16,
) -> Regulator
pub fn new( max_speed: f64, timeout_threshold: Duration, max_concurrents_send: u32, speed_interval: u16, ) -> Regulator
Create a new regulator with:
- Maximum TPS speed
- Threshold time before sending the next request if the distant respond a timeout (to not overload the distant)
- Maximum concurents request in parallel
- Number of interval used to know TPS rate (15 by default)
Sourcepub fn notify_send_transaction(&mut self)
pub fn notify_send_transaction(&mut self)
Indicate that a new transaction have been sent
Sourcepub fn notify_receive_transaction(&mut self, response_time: Duration)
pub fn notify_receive_transaction(&mut self, response_time: Duration)
Indicate that we receive a response to a sended transaction
Sourcepub fn add_tick_overhead(&mut self, overhead: Duration)
pub fn add_tick_overhead(&mut self, overhead: Duration)
Add an overhead to the tick duration if the tick need to be delayed
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Regulator
impl RefUnwindSafe for Regulator
impl Send for Regulator
impl Sync for Regulator
impl Unpin for Regulator
impl UnsafeUnpin for Regulator
impl UnwindSafe for Regulator
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