1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use std::collections::HashMap;
use std::num::{NonZeroU8, NonZeroUsize};
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};
use reqwest::Request;
use tokio::{select, sync};
use tokio::fs::File;
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;
use tracing::info;
#[cfg(feature = "tracing")]
use tracing::warn;
use crate::{chunk_item::{ChunkItem, ChunkMessageInfo, ChunkMessageKind, DownloadedChunkItem}, ChunkIterator, DownloadError};
use crate::{DownloadedLenChangeNotify, DownloadingEndCause};
pub struct ChunkManager {
downloaded_len_sender: Arc<sync::watch::Sender<u64>>,
pub chunk_iterator: ChunkIterator,
downloading_chunks: Mutex<HashMap<usize, DownloadedChunkItem>>,
#[cfg(feature = "breakpoint-resume")]
pub data_archive_notify: sync::Notify,
#[cfg(feature = "breakpoint-resume")]
pub archive_complete_notify: sync::Notify,
download_connection_count_sender: sync::watch::Sender<NonZeroU8>,
pub download_connection_count_receiver: sync::watch::Receiver<NonZeroU8>,
client: reqwest::Client,
cancel_token: CancellationToken,
superfluities_connection_count: AtomicU8,
etag: Option<headers::ETag>,
retry_count: u8,
}
impl ChunkManager {
pub fn new(
download_connection_count: NonZeroU8,
client: reqwest::Client,
cancel_token: CancellationToken,
downloaded_len_sender: Arc<sync::watch::Sender<u64>>,
chunk_iterator: ChunkIterator,
etag: Option<headers::ETag>,
retry_count: u8,
) -> Self {
let (download_connection_count_sender, download_connection_count_receiver) =
sync::watch::channel(download_connection_count);
#[cfg(feature = "breakpoint-resume")]
let (data_archive_notify, archive_complete_notify) =
(sync::Notify::new(), sync::Notify::new());
Self {
#[cfg(feature = "breakpoint-resume")]
data_archive_notify,
#[cfg(feature = "breakpoint-resume")]
archive_complete_notify,
downloaded_len_sender,
chunk_iterator,
downloading_chunks: Mutex::new(HashMap::new()),
download_connection_count_sender,
download_connection_count_receiver,
client,
cancel_token,
superfluities_connection_count: AtomicU8::new(0),
etag,
retry_count,
}
}
pub fn change_connection_count(&self, connection_count: NonZeroU8) -> Result<(), sync::watch::error::SendError<NonZeroU8>> {
self.download_connection_count_sender.send(connection_count)
}
pub fn change_chunk_size(&self, chunk_size: NonZeroUsize) {
let mut guard = self.chunk_iterator.data.lock();
guard.remaining.chunk_size = chunk_size;
}
pub fn downloaded_len(&self) -> u64 {
*self.downloaded_len_sender.borrow()
}
pub fn connection_count(&self) -> u8 {
self.download_connection_count_sender.borrow().get()
}
fn clone_request(request: &Request) -> Box<Request> {
let mut req = Request::new(request.method().clone(), request.url().clone());
*req.timeout_mut() = request.timeout().cloned();
*req.headers_mut() = request.headers().clone();
*req.version_mut() = request.version();
Box::new(req)
}
pub async fn start_download(
&self,
file: Arc<Mutex<File>>,
request: Box<Request>,
downloaded_len_receiver: Option<Arc<dyn DownloadedLenChangeNotify>>,
breakpoint_resume: bool,
) -> Result<DownloadingEndCause, DownloadError> {
let (chunk_message_sender, mut chunk_message_receiver) =
sync::mpsc::channel::<ChunkMessageInfo>(1024);
let mut is_iter_all_chunk = !self
.download_next_chunk(
file.clone(),
chunk_message_sender.clone(),
downloaded_len_receiver.clone(),
Self::clone_request(&request),
)
.await;
if is_iter_all_chunk {
#[cfg(feature = "tracing")]
warn!("No Chunk!");
return Ok(DownloadingEndCause::DownloadFinished);
}
for _ in 0..(self.connection_count() - 1) {
is_iter_all_chunk = !self
.download_next_chunk(
file.clone(),
chunk_message_sender.clone(),
downloaded_len_receiver.clone(),
Self::clone_request(&request),
)
.await;
if is_iter_all_chunk {
break;
}
}
let connection_count_handle_future = {
let mut download_connection_count_receiver =
self.download_connection_count_receiver.clone();
let chunk_message_sender = chunk_message_sender.clone();
let file = file.clone();
let downloaded_len_receiver = downloaded_len_receiver.clone();
let request = Self::clone_request(&request);
async move {
while download_connection_count_receiver.changed().await.is_ok() {
let download_connection_count = download_connection_count_receiver.borrow().get();
let current_count = self.get_chunks().await.len();
let diff = download_connection_count as i16 - current_count as i16;
info!("diff {}",diff);
if diff >= 0 {
self.superfluities_connection_count
.store(0, Ordering::SeqCst);
for _ in 0..diff {
if !self.download_next_chunk(
file.clone(),
chunk_message_sender.clone(),
downloaded_len_receiver.clone(),
Self::clone_request(&request),
)
.await {
break;
}
}
} else {
self.superfluities_connection_count
.store(diff.unsigned_abs() as u8, Ordering::SeqCst);
}
}
DownloadingEndCause::Cancelled
}
};
let message_handle_future = async move {
let chunk_message_sender = chunk_message_sender;
while let Some(message_info) = chunk_message_receiver.recv().await {
match message_info.kind {
ChunkMessageKind::DownloadFinished => {
let (downloading_chunk_count, chunk_item) = self.remove_chunk(message_info.chunk_index).await;
let _ = chunk_item.ok_or(DownloadError::ChunkRemoveFailed(message_info.chunk_index))?;
#[cfg(feature = "breakpoint-resume")]
{
if breakpoint_resume {
let notified = self.archive_complete_notify.notified();
self.data_archive_notify.notify_one();
notified.await;
}
}
if is_iter_all_chunk {
if downloading_chunk_count == 0 {
debug_assert_eq!(self.chunk_iterator.content_length, *self.downloaded_len_sender.borrow());
break;
}
} else if self.superfluities_connection_count.load(Ordering::SeqCst) == 0 {
is_iter_all_chunk = !self
.download_next_chunk(
file.clone(),
chunk_message_sender.clone(),
downloaded_len_receiver.clone(),
Self::clone_request(&request),
)
.await;
} else {
self.superfluities_connection_count
.fetch_sub(1, Ordering::SeqCst);
}
}
ChunkMessageKind::Error(err) => {
return Err(err);
}
ChunkMessageKind::DownloadCancelled => {
}
ChunkMessageKind::DownloadLenAppend(append_len) => {
self.downloaded_len_sender
.send_modify(|n| *n += append_len as u64);
}
}
}
Result::<DownloadingEndCause, DownloadError>::Ok(DownloadingEndCause::DownloadFinished)
};
let cancellation_token = self.cancel_token.clone();
select! {
r = connection_count_handle_future => {Ok(r)}
r = message_handle_future => {r}
_ = cancellation_token.cancelled() => {
Ok(DownloadingEndCause::Cancelled)
}
}
}
async fn insert_chunk(&self, item: DownloadedChunkItem) {
let mut downloading_chunks = self.downloading_chunks.lock().await;
downloading_chunks.insert(item.chunk_info.index, item);
}
pub async fn get_chunks(&self) -> Vec<Arc<ChunkItem>> {
let downloading_chunks = self.downloading_chunks.lock().await;
downloading_chunks
.values()
.map(|n| n.chunk_item.clone())
.collect()
}
async fn remove_chunk(&self, index: usize) -> (usize, Option<DownloadedChunkItem>) {
let mut downloading_chunks = self.downloading_chunks.lock().await;
let removed = downloading_chunks.remove(&index);
(downloading_chunks.len(), removed)
}
async fn download_next_chunk(
&self,
file: Arc<Mutex<File>>,
sender: sync::mpsc::Sender<ChunkMessageInfo>,
downloaded_len_receiver: Option<Arc<dyn DownloadedLenChangeNotify>>,
request: Box<Request>,
) -> bool {
if let Some(chunk_info) = self.chunk_iterator.next() {
let chunk_item = Arc::new(ChunkItem::new(
chunk_info,
self.cancel_token.child_token(),
self.client.clone(),
sender,
file,
downloaded_len_receiver,
self.etag.clone(),
));
let item = chunk_item.start_download(request, self.retry_count);
self.insert_chunk(item).await;
true
} else {
false
}
}
}