Skip to content

Commit d96688d

Browse files
downloader: Add hierarchical cancel tokens to cancel all downloads
1 parent 31f5218 commit d96688d

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/downloader.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub enum Status {
7777
pub struct DownloadManager {
7878
queue: mpsc::Sender<DownloadRequest>,
7979
semaphore: Arc<Semaphore>,
80+
cancel: CancellationToken,
8081
}
8182

8283
impl Drop for DownloadManager {
@@ -94,6 +95,7 @@ impl DownloadManager {
9495
let manager = Self {
9596
queue: tx,
9697
semaphore: semaphore.clone(),
98+
cancel: CancellationToken::new(),
9799
};
98100
// Spawn the dispatcher thread to handle download requests
99101
tokio::spawn(async move { dispatcher_thread(client, rx, semaphore).await });
@@ -115,7 +117,7 @@ impl DownloadManager {
115117
pub fn add_request(&self, url: Url, destination: PathBuf) -> DownloadHandle {
116118
let (result_tx, result_rx) = oneshot::channel();
117119
let (status_tx, status_rx) = watch::channel(Status::Pending);
118-
let cancel = CancellationToken::new();
120+
let cancel = self.cancel.child_token();
119121

120122
let req = DownloadRequest {
121123
url,
@@ -133,6 +135,10 @@ impl DownloadManager {
133135
cancel,
134136
}
135137
}
138+
139+
pub fn cancel_all(&self) {
140+
self.cancel.cancel();
141+
}
136142
}
137143

138144
async fn dispatcher_thread(

0 commit comments

Comments
 (0)