Skip to content

Commit f03dc6a

Browse files
committed
refactor: factor out wait_for_all_work_done()
1 parent 77cf536 commit f03dc6a

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

src/context.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -553,23 +553,7 @@ impl Context {
553553

554554
if self.scheduler.is_running().await {
555555
self.scheduler.maybe_network().await;
556-
557-
// Wait until fetching is finished.
558-
// Ideally we could wait for connectivity change events,
559-
// but sleep loop is good enough.
560-
561-
// First 100 ms sleep in chunks of 10 ms.
562-
for _ in 0..10 {
563-
if self.all_work_done().await {
564-
break;
565-
}
566-
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
567-
}
568-
569-
// If we are not finished in 100 ms, keep waking up every 100 ms.
570-
while !self.all_work_done().await {
571-
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
572-
}
556+
self.wait_for_all_work_done().await;
573557
} else {
574558
// Pause the scheduler to ensure another connection does not start
575559
// while we are fetching on a dedicated connection.

src/scheduler/connectivity.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ impl Context {
535535
}
536536

537537
/// Returns true if all background work is done.
538-
pub async fn all_work_done(&self) -> bool {
538+
async fn all_work_done(&self) -> bool {
539539
let lock = self.scheduler.inner.read().await;
540540
let stores: Vec<_> = match *lock {
541541
InnerSchedulerState::Started(ref sched) => sched
@@ -555,4 +555,23 @@ impl Context {
555555
}
556556
true
557557
}
558+
559+
/// Waits until background work is finished.
560+
pub async fn wait_for_all_work_done(&self) {
561+
// Ideally we could wait for connectivity change events,
562+
// but sleep loop is good enough.
563+
564+
// First 100 ms sleep in chunks of 10 ms.
565+
for _ in 0..10 {
566+
if self.all_work_done().await {
567+
break;
568+
}
569+
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
570+
}
571+
572+
// If we are not finished in 100 ms, keep waking up every 100 ms.
573+
while !self.all_work_done().await {
574+
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
575+
}
576+
}
558577
}

0 commit comments

Comments
 (0)