Skip to main content

tuwunel_service/integrity/
scan.rs

1use futures::{Stream, TryStreamExt};
2use tokio::task::consume_budget;
3use tuwunel_core::{Result, Server, utils::TryReadyExt};
4
5/// Bounds a row walk by shutdown, reports its position and shares the runtime.
6///
7/// After a stop request every row becomes an interrupted error, which ends a
8/// short-circuiting consumer such as `try_fold` at the next row. Each row read
9/// while the server runs advances the progress of any phase in flight. A cursor
10/// is ready on every poll after its seek, so each row also spends cooperative
11/// budget; otherwise a walk on a serving database would hold its worker for the
12/// whole column.
13pub(crate) trait ScanExt<T>: Stream<Item = Result<T>> + Send + Sized {
14	fn scanned(self, server: &Server) -> impl Stream<Item = Result<T>> + Send;
15}
16
17impl<T, S> ScanExt<T> for S
18where
19	S: Stream<Item = Result<T>> + Send,
20	T: Send,
21{
22	fn scanned(self, server: &Server) -> impl Stream<Item = Result<T>> + Send {
23		self.ready_and_then(|row| server.check_running().map(|()| row))
24			.inspect_ok(|_| server.progress.advance())
25			.and_then(async |row| {
26				consume_budget().await;
27
28				Ok(row)
29			})
30	}
31}