Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: factor out wait_for_all_work_done() #6311

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,23 +553,7 @@ impl Context {

if self.scheduler.is_running().await {
self.scheduler.maybe_network().await;

// Wait until fetching is finished.
// Ideally we could wait for connectivity change events,
// but sleep loop is good enough.

// First 100 ms sleep in chunks of 10 ms.
for _ in 0..10 {
if self.all_work_done().await {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}

// If we are not finished in 100 ms, keep waking up every 100 ms.
while !self.all_work_done().await {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
self.wait_for_all_work_done().await;
} else {
// Pause the scheduler to ensure another connection does not start
// while we are fetching on a dedicated connection.
Expand Down
21 changes: 20 additions & 1 deletion src/scheduler/connectivity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ impl Context {
}

/// Returns true if all background work is done.
pub async fn all_work_done(&self) -> bool {
async fn all_work_done(&self) -> bool {
let lock = self.scheduler.inner.read().await;
let stores: Vec<_> = match *lock {
InnerSchedulerState::Started(ref sched) => sched
Expand All @@ -555,4 +555,23 @@ impl Context {
}
true
}

/// Waits until background work is finished.
pub async fn wait_for_all_work_done(&self) {
// Ideally we could wait for connectivity change events,
// but sleep loop is good enough.

// First 100 ms sleep in chunks of 10 ms.
for _ in 0..10 {
if self.all_work_done().await {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}

// If we are not finished in 100 ms, keep waking up every 100 ms.
while !self.all_work_done().await {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
}
}
Loading