Skip to main content

Debounced

Struct Debounced 

Source
pub struct Debounced<T> { /* private fields */ }
Available on crate feature cronutil only.
Expand description

去抖聚合队列: 攒到阈值条数或时间窗口先到者触发一次批量刷新。 即时直发仅限低频单条场景; 采集 / 回灌 / 批量管道应走本队列。

  • 刷新回调是 async 函数, 在 Tokio 任务里串行执行, 批次间保持 FIFO
  • Debounced::add / Debounced::flush 是同步非阻塞调用, 可在任意线程调用, 回调内也可安全调用 (如失败回灌重试)
  • 回调 panic 被捕获并丢弃该批, 业务错误与重试在回调内自行处理
  • Clone 共享同一队列; 退出前调用 Debounced::close 刷完剩余条目, 未 close 直接丢弃会丢掉缓冲
use std::time::Duration;
use rust_web_utils::cronutil::Debounced;

let q = Debounced::new(100, Duration::from_secs(2), |batch: Vec<String>| async move {
    // bulk_index(batch).await
    let _ = batch;
});
q.add(["doc-1".to_owned()]);
q.close().await;

Implementations§

Source§

impl<T: Send + 'static> Debounced<T>

Source

pub fn new<F, Fut>(threshold: usize, window: Duration, flush: F) -> Self
where F: Fn(Vec<T>) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

创建去抖队列: threshold 为触发刷新的条数阈值 (零时按 1), window 为最长等待时间窗 (零时按 1s), flush 为批量刷新回调 (串行调用, 不会并发)。

必须在 Tokio 运行时上下文内调用 (取当前运行时句柄), 否则 panic; 运行时外创建用 Debounced::with_handle

Source

pub fn with_handle<F, Fut>( handle: Handle, threshold: usize, window: Duration, flush: F, ) -> Self
where F: Fn(Vec<T>) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

同 Debounced::new, 显式指定定时器与回调运行的 Tokio 运行时

Source

pub fn add(&self, items: impl IntoIterator<Item = T>)

追加条目; 达到阈值立即成批, 否则等待时间窗。close 之后调用同样入队并立即刷新, 不丢数据

Source

pub fn flush(&self)

立即把当前缓冲成批并触发刷新 (不等阈值与时间窗); 缓冲为空时是空操作。不等待刷新完成

Source

pub async fn close(&self)

关闭队列, 刷新剩余条目并等待全部在途批次完成; 之后的 Debounced::add 入队即刷。 在刷新回调内直接 .await 时不等待 (剩余批次由当前 drain 继续处理), 不会死锁; 回调内把 close 再 spawn 出去并等待它会死锁 (它在等回调自己结束)

Source

pub fn len(&self) -> usize

缓冲与待刷批次中的条目总数 (不含正在刷新的批次)

Source

pub fn is_empty(&self) -> bool

缓冲与待刷批次是否为空

Trait Implementations§

Source§

impl<T> Clone for Debounced<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Debounced<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Debounced<T>

§

impl<T> !UnwindSafe for Debounced<T>

§

impl<T> Forget for Debounced<T>

§

impl<T> Freeze for Debounced<T>
where *const ArcInner<Inner<T>> is !null: Freeze, PhantomData<ArcInner<Inner<T>>>: Freeze,

§

impl<T> Move for Debounced<T>

§

impl<T> Send for Debounced<T>
where Arc<Inner<T>>: Send,

§

impl<T> Sync for Debounced<T>
where Arc<Inner<T>>: Sync,

§

impl<T> Unpin for Debounced<T>
where Arc<Inner<T>>: Unpin,

§

impl<T> UnsafeUnpin for Debounced<T>
where *const ArcInner<Inner<T>> is !null: UnsafeUnpin, PhantomData<ArcInner<Inner<T>>>: 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
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.