1
1
use super :: { DownloadProgress , DownloadRequest } ;
2
- use crate :: { downloader :: Status , error:: DownloadError , Error } ;
2
+ use crate :: { error:: DownloadError , Error } ;
3
3
use reqwest:: Client ;
4
4
use std:: time:: Duration ;
5
5
use tokio:: { fs:: File , io:: AsyncWriteExt } ;
@@ -25,38 +25,33 @@ pub(super) async fn download_thread(client: Client, mut req: DownloadRequest) {
25
25
let max_attempts = req. config ( ) . max_retries ( ) ;
26
26
for attempt in 0 ..=( max_attempts + 1 ) {
27
27
if attempt > max_attempts {
28
- req. status . send ( Status :: Failed ) . ok ( ) ;
29
- req. result
30
- . send ( Err ( Error :: Download ( DownloadError :: RetriesExhausted {
31
- last_error_msg : last_error
32
- . as_ref ( )
33
- . map ( ToString :: to_string)
34
- . unwrap_or_else ( || "Unknown Error" . to_string ( ) ) ,
35
- } ) ) )
36
- . ok ( ) ;
28
+ req. fail ( Error :: Download ( DownloadError :: RetriesExhausted {
29
+ last_error_msg : last_error
30
+ . as_ref ( )
31
+ . map ( ToString :: to_string)
32
+ . unwrap_or_else ( || "Unknown Error" . to_string ( ) ) ,
33
+ } ) ) ;
37
34
return ;
38
35
}
39
36
40
37
if attempt > 0 {
41
- req. status . send ( Status :: Retrying ) . ok ( ) ;
38
+ req. retry ( ) ;
42
39
// Basic exponential backoff
43
40
let delay_ms = 1000 * 2u64 . pow ( attempt as u32 - 1 ) ;
44
41
let delay = Duration :: from_millis ( delay_ms) ;
45
42
46
43
tokio:: select! {
47
44
_ = tokio:: time:: sleep( delay) => { } ,
48
45
_ = req. cancel. cancelled( ) => {
49
- req. status. send( Status :: Failed ) . ok( ) ;
50
- req. result. send( Err ( Error :: Download ( DownloadError :: Cancelled ) ) ) . ok( ) ;
46
+ req. cancel( ) ;
51
47
return ;
52
48
}
53
49
}
54
50
}
55
51
56
52
match download ( client. clone ( ) , & mut req) . await {
57
53
Ok ( file) => {
58
- req. status . send ( Status :: Completed ) . ok ( ) ;
59
- req. result . send ( Ok ( file) ) . ok ( ) ;
54
+ req. complete ( file) ;
60
55
return ;
61
56
}
62
57
Err ( e) => {
@@ -65,13 +60,7 @@ pub(super) async fn download_thread(client: Client, mut req: DownloadRequest) {
65
60
continue ;
66
61
}
67
62
68
- let status = if matches ! ( e, Error :: Download ( DownloadError :: Cancelled ) ) {
69
- Status :: Cancelled
70
- } else {
71
- Status :: Failed
72
- } ;
73
- req. status . send ( status) . ok ( ) ;
74
- req. result . send ( Err ( e) ) . ok ( ) ;
63
+ req. fail ( e) ;
75
64
return ;
76
65
}
77
66
}
0 commit comments