mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 13:10:11 +08:00
* docs(backend): define background task ownership * refactor(backend): give background operations single task owners * test(backend): remove actor migration regression cases * docs(changelog): drop backend actor migration entry * refactor(backend): extract workspace task drivers * fix(timer): omit profile identifier from panic log * docs(backend): define workspace crate boundaries * refactor(lightweight): use the task countdown driver * refactor(lightweight): name countdown callbacks * refactor(task): simplify driver startup and coalescing requests * refactor(core): bind service handoff to the sidecar generation * refactor(tun): coalesce background run state reconciliation * refactor(core): own sidecar exit resources in one session * refactor(core): remove automatic Windows service handoff * refactor(task): simplify coalescing job signals * refactor(runstate): keep operation status with service state * refactor(dns): coalesce background override synchronization * refactor(sysproxy): own the local guard under its operation lock * fix(tray): read menu settings at publication * fix(dns): retain script lock across caller cancellation * refactor(profiles): own selected node restoration in one task * refactor(timer): own subscription schedules in the scheduler * test(timer): remove the added application test * fix(service): renew provider sync retry budget on refresh
147 lines
4.5 KiB
Rust
147 lines
4.5 KiB
Rust
use futures::FutureExt as _;
|
|
use parking_lot::Mutex;
|
|
use std::{
|
|
future::Future,
|
|
panic::AssertUnwindSafe,
|
|
sync::{
|
|
Arc,
|
|
atomic::{AtomicBool, Ordering},
|
|
},
|
|
};
|
|
use tokio::{
|
|
sync::Notify,
|
|
time::{Instant, sleep_until},
|
|
};
|
|
|
|
/// The next decision after the driver drains pending inputs.
|
|
pub enum Next<W> {
|
|
Idle,
|
|
At(Instant),
|
|
Run(W),
|
|
}
|
|
|
|
/// Synchronous policy owned and called by one driver.
|
|
pub trait ScheduledTask {
|
|
/// The merged pending input. Its default value represents no requests.
|
|
type Input: Default;
|
|
type Work: Copy + Send;
|
|
type Output;
|
|
|
|
fn on_input(&mut self, input: Self::Input, now: Instant);
|
|
fn next(&mut self, now: Instant) -> Next<Self::Work>;
|
|
/// Receives the execution result or an unwind from the execution callback.
|
|
fn finished(&mut self, work: Self::Work, result: std::thread::Result<Self::Output>, now: Instant);
|
|
}
|
|
|
|
/// A coalescing slot shared with one driver.
|
|
pub struct Mailbox<I> {
|
|
pending: Mutex<I>,
|
|
changed: Notify,
|
|
}
|
|
|
|
impl<I> Mailbox<I> {
|
|
/// Merge into the pending slot under a short synchronous lock.
|
|
///
|
|
/// Return true from `merge` when the driver should wake up. This only
|
|
/// submits input. Any completion reply belongs to the caller's protocol.
|
|
pub fn update(&self, merge: impl FnOnce(&mut I) -> bool) {
|
|
let changed = merge(&mut *self.pending.lock());
|
|
if changed {
|
|
self.changed.notify_one();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Create the mailbox and its driver. The caller chooses how to run the future.
|
|
///
|
|
/// While the driver is running, pending inputs wake idle waits and do not
|
|
/// interrupt an active job. Keep the driver alive until any issued effects
|
|
/// complete. Dropping producers does not shut it down.
|
|
pub fn new<P, F, Fut>(policy: P, execute: F) -> (Arc<Mailbox<P::Input>>, impl Future<Output = ()> + Send + 'static)
|
|
where
|
|
P: ScheduledTask + Send + 'static,
|
|
P::Input: Send + 'static,
|
|
F: FnMut(P::Work) -> Fut + Send + 'static,
|
|
Fut: Future<Output = P::Output> + Send + 'static,
|
|
{
|
|
let inbox = Arc::new(Mailbox {
|
|
pending: Mutex::default(),
|
|
changed: Notify::new(),
|
|
});
|
|
let receiver = Arc::clone(&inbox);
|
|
(inbox, drive(policy, receiver, execute))
|
|
}
|
|
|
|
async fn drive<P, F, Fut>(mut policy: P, inbox: Arc<Mailbox<P::Input>>, mut execute: F)
|
|
where
|
|
P: ScheduledTask,
|
|
F: FnMut(P::Work) -> Fut,
|
|
Fut: Future<Output = P::Output>,
|
|
{
|
|
loop {
|
|
let input = std::mem::take(&mut *inbox.pending.lock());
|
|
let now = Instant::now();
|
|
policy.on_input(input, now);
|
|
match policy.next(now) {
|
|
Next::Idle => inbox.changed.notified().await,
|
|
Next::At(due) => {
|
|
tokio::select! {
|
|
_ = inbox.changed.notified() => {}
|
|
_ = sleep_until(due) => {}
|
|
}
|
|
}
|
|
Next::Run(work) => {
|
|
// Pending inputs merge while the job runs. They cannot interrupt its effects.
|
|
let run = &mut execute;
|
|
let result = AssertUnwindSafe(async move { run(work).await }).catch_unwind().await;
|
|
policy.finished(work, result, Instant::now());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A request to run a job with at most one pending successor.
|
|
pub struct CoalescingJob(Arc<JobSignal>);
|
|
|
|
#[derive(Default)]
|
|
struct JobSignal {
|
|
pending: AtomicBool,
|
|
changed: Notify,
|
|
}
|
|
|
|
impl CoalescingJob {
|
|
pub fn request(&self) {
|
|
if !self.0.pending.swap(true, Ordering::Release) {
|
|
self.0.changed.notify_one();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Reconcile on demand without interrupting work in progress.
|
|
pub fn coalescing<Fut>(
|
|
mut execute: impl FnMut() -> Fut + Send + 'static,
|
|
mut on_panic: impl FnMut() + Send + 'static,
|
|
) -> (CoalescingJob, impl Future<Output = ()> + Send + 'static)
|
|
where
|
|
Fut: Future<Output = ()> + Send + 'static,
|
|
{
|
|
let signal = Arc::new(JobSignal::default());
|
|
let worker_signal = Arc::clone(&signal);
|
|
let driver = async move {
|
|
loop {
|
|
worker_signal.changed.notified().await;
|
|
// Requests coalesce until the driver claims the batch, even after it has been woken.
|
|
worker_signal.pending.swap(false, Ordering::Acquire);
|
|
let run = &mut execute;
|
|
if AssertUnwindSafe(async move { run().await })
|
|
.catch_unwind()
|
|
.await
|
|
.is_err()
|
|
{
|
|
on_panic();
|
|
}
|
|
}
|
|
};
|
|
(CoalescingJob(signal), driver)
|
|
}
|