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>
impl<T: Send + 'static> Debounced<T>
Sourcepub fn new<F, Fut>(threshold: usize, window: Duration, flush: F) -> Self
pub fn new<F, Fut>(threshold: usize, window: Duration, flush: F) -> Self
创建去抖队列: threshold 为触发刷新的条数阈值 (零时按 1), window 为最长等待时间窗
(零时按 1s), flush 为批量刷新回调 (串行调用, 不会并发)。
必须在 Tokio 运行时上下文内调用 (取当前运行时句柄), 否则 panic;
运行时外创建用 Debounced::with_handle
Sourcepub fn with_handle<F, Fut>(
handle: Handle,
threshold: usize,
window: Duration,
flush: F,
) -> Self
pub fn with_handle<F, Fut>( handle: Handle, threshold: usize, window: Duration, flush: F, ) -> Self
同 Debounced::new, 显式指定定时器与回调运行的 Tokio 运行时
Sourcepub fn add(&self, items: impl IntoIterator<Item = T>)
pub fn add(&self, items: impl IntoIterator<Item = T>)
追加条目; 达到阈值立即成批, 否则等待时间窗。close 之后调用同样入队并立即刷新, 不丢数据
Sourcepub async fn close(&self)
pub async fn close(&self)
关闭队列, 刷新剩余条目并等待全部在途批次完成; 之后的 Debounced::add 入队即刷。
在刷新回调内直接 .await 时不等待 (剩余批次由当前 drain 继续处理), 不会死锁;
回调内把 close 再 spawn 出去并等待它会死锁 (它在等回调自己结束)
Trait Implementations§
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>
impl<T> Move for Debounced<T>
impl<T> Send for Debounced<T>
impl<T> Sync for Debounced<T>
impl<T> Unpin for Debounced<T>
impl<T> UnsafeUnpin for Debounced<T>where
*const ArcInner<Inner<T>> is !null: UnsafeUnpin,
PhantomData<ArcInner<Inner<T>>>: UnsafeUnpin,
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