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
use crate::timeout::*;
use futures::{
future::{Future, TryFutureExt},
prelude::*,
stream,
};
use futures_retry::{FutureRetry, RetryPolicy};
use futures_stopwatch::try_stopwatch;
use rusoto_core::{ByteStream, RusotoError};
use rusoto_s3::*;
use snafu::futures::TryFutureExt as S;
use snafu::ResultExt;
use std::{
marker::Unpin,
path::{Path, PathBuf},
sync::{Arc, Mutex},
time::Duration,
};
use tokio_util::codec::{BytesCodec, FramedRead};
mod config;
pub mod err;
mod list_actions;
mod upload;
pub use list_actions::*;
pub use upload::*;
pub mod timeout;
pub use config::UploadConfig;
pub use err::Error;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod test;
#[derive(Debug, Clone, Copy)]
pub struct RequestReport {
pub seq: usize,
pub bytes: u64,
pub total_time: Duration,
pub success_time: Duration,
pub attempts: usize,
pub est: f64,
}
pub async fn s3_single_request<F, G, R>(future_factory: F) -> Result<(RequestReport, R), Error>
where
F: Fn() -> G + Unpin + Clone + Send + Sync + 'static,
G: Future<Output = Result<R, Error>> + Send,
{
s3_request(
move || {
let factory = future_factory.clone();
async move { Ok((factory(), 0)) }
},
10,
Arc::new(Mutex::new(TimeoutState::new(UploadConfig::default()))),
)
.await
}
pub(crate) async fn s3_request<F, G, H, T, R>(
future_factory: F,
n_retries: usize,
timeout: Arc<Mutex<T>>,
) -> Result<(RequestReport, R), Error>
where
F: Fn() -> G + Unpin + Clone + Send + Sync + 'static,
G: Future<Output = Result<(H, u64), Error>> + Send,
H: Future<Output = Result<R, Error>> + Send,
T: timeout::Timeout,
{
let mut attempts1 = 0;
let mut attempts2 = 0;
try_stopwatch(
FutureRetry::new(
move || {
let future_factory = future_factory.clone();
let timeout = timeout.clone();
async move {
attempts1 += 1;
let (request, len) = future_factory().await?;
let (est, timeout_value) = {
let t = timeout.lock().unwrap();
(t.get_estimate(), t.get_timeout(len, attempts1))
};
try_stopwatch(
tokio::time::timeout(timeout_value, request)
.with_context(|| err::Timeout {})
.map(|result| result.and_then(|x| x)),
)
.map_ok(move |(response, success_time)| (response, success_time, len, est))
.await
}
},
{
move |e| {
attempts2 += 1;
if attempts2 > n_retries {
RetryPolicy::ForwardError(e)
} else {
RetryPolicy::WaitRetry(Duration::from_millis(200))
}
}
},
),
)
.await
.map(
move |(((response, success_time, bytes, est), attempts), total_time)| {
(
RequestReport {
seq: 0,
bytes,
total_time,
success_time,
attempts,
est,
},
response,
)
},
)
.map_err(|(err, _attempts)| err)
}
pub fn testing_s3_client() -> S3Client {
use rusoto_core::{credential::ProfileProvider, region::Region, HttpClient};
let client = HttpClient::new().unwrap();
let region = Region::Custom {
name: "minio".to_owned(),
endpoint: "http://localhost:9000".to_owned(),
};
let mut profile = ProfileProvider::new().unwrap();
profile.set_profile("testing");
rusoto_s3::S3Client::new_with(client, profile, region)
}
pub fn strip_prefix<P: AsRef<Path>>(prefix: P) -> impl Fn(&Path) -> PathBuf {
move |path| path.strip_prefix(prefix.as_ref()).unwrap().to_path_buf()
}