Skip to main content

Throttler

Struct Throttler 

Source
pub struct Throttler<T, F> { /* private fields */ }
Expand description

Throttler rate limits messages by dropping or buffering them.

Throttler takes messages of type T and callback of type F for dropping or processing messages.

The throttler stores its limit and interval as non-zero values from RateLimit. Internal counters, buffers, and timer state stay private so callers can observe state without breaking rate-limit invariants.

§Callback contract

The output_send and output_drop callbacks are invoked inline from Throttler::send and the drain handler. They must not reenter the throttler (for example by calling send synchronously), since the throttler mutates its buffer and window state through UnsafeCell without borrow-check protection. Route side effects through an asynchronous queue when in doubt.

Implementations§

Source§

impl<T, F> Throttler<T, F>
where T: Debug,

Source

pub fn new( rate_limit: RateLimit, clock: Rc<RefCell<dyn Clock>>, timer_name: &str, output_send: F, output_drop: Option<F>, actor_id: Ustr, ) -> Self

Creates a new Throttler instance.

The timer is registered under a name namespaced by actor_id so multiple throttlers can share one clock.

Source

pub fn delta_next(&self) -> u64

Time delta when the next message can be sent.

Uses saturating subtraction so a clock regression or a future-dated timestamp yields a zero delta instead of panicking.

Source

pub fn reset(&mut self)

Reset the throttler which clears internal state and cancels any pending timer so no drain or resume callback fires after reset.

Source§

impl<T, F> Throttler<T, F>

Source

pub const fn limit(&self) -> usize

Maximum number of messages that can be sent within the interval.

Source

pub const fn interval_ns(&self) -> u64

Interval between messages in nanoseconds.

Source

pub const fn rate_limit(&self) -> RateLimit

Rate limit configured for this throttler.

Source

pub fn qsize(&self) -> usize

Number of messages queued in buffer.

Source

pub fn used(&self) -> f64

Fractional value of rate limit consumed in current interval.

Source

pub const fn is_limiting(&self) -> bool

Whether the throttler is currently limiting the message rate.

Source

pub const fn recv_count(&self) -> usize

Number of messages received.

Source

pub const fn sent_count(&self) -> usize

Number of messages sent.

Source§

impl<T, F> Throttler<T, F>
where T: 'static + Debug, F: Fn(T) + 'static,

Source

pub fn to_actor(self) -> Rc<UnsafeCell<Self>>

Source

pub fn dispose(&mut self)

Disposes of the throttler by cancelling its timer, deregistering its process endpoint from the message bus, and removing it from the actor registry.

Call this before dropping a throttler registered via Throttler::to_actor to avoid leaking the process endpoint. For embedded throttlers (not registered) this is still safe: the endpoint and registry removals are no-ops.

Source

pub fn try_reserve(&mut self, count: usize) -> bool

Reserves capacity for count messages without sending callbacks.

Returns false when the current window cannot accept all messages. No partial reservation is made in that case. The resume timer is armed only when the window is genuinely full (delta_next > 0); when the window already slid (delta_next == 0) the next call re-evaluates without arming a zero-delta timer that would fire immediately and log spam.

Source

pub fn send(&mut self, msg: T)
where T: 'static, F: Fn(T) + 'static,

Trait Implementations§

Source§

impl<T, F> Actor for Throttler<T, F>
where T: 'static + Debug, F: Fn(T) + 'static,

Source§

fn id(&self) -> Ustr

The unique identifier for the actor.
Source§

fn handle(&mut self, _msg: &dyn Any)

Handles the msg.
Source§

fn as_any(&self) -> &dyn Any

Returns a reference to self as Any, for downcasting support.
Source§

fn as_any_mut(&mut self) -> &mut dyn Any
where Self: Sized,

Returns a mutable reference to self as Any, for downcasting support. Read more
Source§

impl<T, F> Debug for Throttler<T, F>
where T: Debug,

Source§

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

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

impl<T, F> Drop for Throttler<T, F>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<T, F> !RefUnwindSafe for Throttler<T, F>

§

impl<T, F> !Send for Throttler<T, F>

§

impl<T, F> !Sync for Throttler<T, F>

§

impl<T, F> !UnwindSafe for Throttler<T, F>

§

impl<T, F> Freeze for Throttler<T, F>
where F: Freeze,

§

impl<T, F> Unpin for Throttler<T, F>
where F: Unpin, T: Unpin,

§

impl<T, F> UnsafeUnpin for Throttler<T, F>
where F: UnsafeUnpin,

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
§

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

§

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.

§

impl<T> Instrument for T

§

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

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

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.

§

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

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

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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