mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-10 23:12:54 +00:00
Addresses the 1.82 beta clippy lint `too_long_first_doc_paragraph` by adding newlines to the first sentence if it is short enough, and making a short first sentence if there is the need.
37 lines
896 B
Rust
37 lines
896 B
Rust
use tokio_util::sync::CancellationToken;
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
pub enum YieldingLoopError {
|
|
#[error("Cancelled")]
|
|
Cancelled,
|
|
}
|
|
|
|
/// Helper for long synchronous loops, e.g. over all tenants in the system.
|
|
///
|
|
/// Periodically yields to avoid blocking the executor, and after resuming
|
|
/// checks the provided cancellation token to drop out promptly on shutdown.
|
|
#[inline(always)]
|
|
pub async fn yielding_loop<I, T, F>(
|
|
interval: usize,
|
|
cancel: &CancellationToken,
|
|
iter: I,
|
|
mut visitor: F,
|
|
) -> Result<(), YieldingLoopError>
|
|
where
|
|
I: Iterator<Item = T>,
|
|
F: FnMut(T),
|
|
{
|
|
for (i, item) in iter.enumerate() {
|
|
visitor(item);
|
|
|
|
if (i + 1) % interval == 0 {
|
|
tokio::task::yield_now().await;
|
|
if cancel.is_cancelled() {
|
|
return Err(YieldingLoopError::Cancelled);
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|