Skip to content

Commit

Permalink
handle idle end event for idle detection in persisting
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Oct 4, 2024
1 parent 2bbe42d commit a0a6bf7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
27 changes: 21 additions & 6 deletions turbopack/crates/turbo-tasks-backend/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ struct TurboTasksBackendInner {

stopping: AtomicBool,
stopping_event: Event,
idle_event: Event,
idle_start_event: Event,
idle_end_event: Event,

backing_storage: Arc<dyn BackingStorage + Sync + Send>,
}
Expand Down Expand Up @@ -167,7 +168,8 @@ impl TurboTasksBackendInner {
last_snapshot: AtomicU64::new(0),
stopping: AtomicBool::new(false),
stopping_event: Event::new(|| "TurboTasksBackend::stopping_event".to_string()),
idle_event: Event::new(|| "TurboTasksBackend::idle_event".to_string()),
idle_start_event: Event::new(|| "TurboTasksBackend::idle_start_event".to_string()),
idle_end_event: Event::new(|| "TurboTasksBackend::idle_end_event".to_string()),
backing_storage,
}
}
Expand Down Expand Up @@ -630,7 +632,11 @@ impl TurboTasksBackendInner {
}

fn idle_start(&self) {
self.idle_event.notify(usize::MAX);
self.idle_start_event.notify(usize::MAX);
}

fn idle_end(&self) {
self.idle_end_event.notify(usize::MAX);
}

fn get_or_create_persistent_task(
Expand Down Expand Up @@ -1170,16 +1176,21 @@ impl TurboTasksBackendInner {
if until > Instant::now() {
let mut stop_listener = self.stopping_event.listen();
if !self.stopping.load(Ordering::Acquire) {
let mut idle_listener = self.idle_event.listen();
let mut idle_start_listener = self.idle_start_event.listen();
let mut idle_end_listener = self.idle_end_event.listen();
let mut idle_time = until + IDLE_TIMEOUT;
loop {
tokio::select! {
_ = &mut stop_listener => {
break;
},
_ = &mut idle_listener => {
_ = &mut idle_start_listener => {
idle_time = Instant::now() + IDLE_TIMEOUT;
idle_listener = self.idle_event.listen()
idle_start_listener = self.idle_start_event.listen()
},
_ = &mut idle_end_listener => {
idle_time = until + IDLE_TIMEOUT;
idle_end_listener = self.idle_end_event.listen()
},
_ = tokio::time::sleep_until(until.into()) => {
break;
Expand Down Expand Up @@ -1300,6 +1311,10 @@ impl Backend for TurboTasksBackend {
self.0.idle_start();
}

fn idle_end(&self, _turbo_tasks: &dyn TurboTasksBackendApi<Self>) {
self.0.idle_end();
}

fn get_or_create_persistent_task(
&self,
task_type: CachedTaskType,
Expand Down
2 changes: 2 additions & 0 deletions turbopack/crates/turbo-tasks/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ pub trait Backend: Sync + Send {

#[allow(unused_variables)]
fn idle_start(&self, turbo_tasks: &dyn TurboTasksBackendApi<Self>) {}
#[allow(unused_variables)]
fn idle_end(&self, turbo_tasks: &dyn TurboTasksBackendApi<Self>) {}

fn invalidate_task(&self, task: TaskId, turbo_tasks: &dyn TurboTasksBackendApi<Self>);

Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbo-tasks/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
{
*self.start.lock().unwrap() = Some(Instant::now());
self.event_start.notify(usize::MAX);
self.backend.idle_end(self);
}
}

Expand Down

0 comments on commit a0a6bf7

Please sign in to comment.