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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use std::sync::Arc;
use crate::{bounds, erase, retry, Client};
use aws_smithy_async::rt::sleep::{default_async_sleep, AsyncSleep};
use aws_smithy_http::body::SdkBody;
use aws_smithy_http::result::ConnectorError;
use aws_smithy_types::timeout;
#[derive(Clone, Debug)]
pub struct Builder<C = (), M = (), R = retry::Standard> {
connector: C,
middleware: M,
standard_retry_config: Option<retry::Config>,
retry_policy: R,
timeout_config: timeout::Config,
sleep_impl: Option<Arc<dyn AsyncSleep>>,
}
impl<C, M> Default for Builder<C, M>
where
C: Default,
M: Default,
{
fn default() -> Self {
Self {
connector: Default::default(),
middleware: Default::default(),
standard_retry_config: Some(retry::Config::default()),
retry_policy: Default::default(),
timeout_config: Default::default(),
sleep_impl: default_async_sleep(),
}
}
}
impl<C, M> Builder<C, M>
where
C: Default,
M: Default,
{
pub fn new() -> Self {
Self::default()
}
}
impl<M, R> Builder<(), M, R> {
pub fn connector<C>(self, connector: C) -> Builder<C, M, R> {
Builder {
connector,
standard_retry_config: self.standard_retry_config,
retry_policy: self.retry_policy,
middleware: self.middleware,
timeout_config: self.timeout_config,
sleep_impl: self.sleep_impl,
}
}
pub fn connector_fn<F, FF>(self, map: F) -> Builder<tower::util::ServiceFn<F>, M, R>
where
F: Fn(http::Request<SdkBody>) -> FF + Send,
FF: std::future::Future<Output = Result<http::Response<SdkBody>, ConnectorError>>,
tower::util::ServiceFn<F>: bounds::SmithyConnector,
{
self.connector(tower::service_fn(map))
}
}
impl<C, R> Builder<C, (), R> {
pub fn middleware<M>(self, middleware: M) -> Builder<C, M, R> {
Builder {
connector: self.connector,
standard_retry_config: self.standard_retry_config,
retry_policy: self.retry_policy,
timeout_config: self.timeout_config,
middleware,
sleep_impl: self.sleep_impl,
}
}
pub fn middleware_fn<F>(self, map: F) -> Builder<C, tower::util::MapRequestLayer<F>, R>
where
F: Fn(aws_smithy_http::operation::Request) -> aws_smithy_http::operation::Request
+ Clone
+ Send
+ Sync
+ 'static,
{
self.middleware(tower::util::MapRequestLayer::new(map))
}
}
impl<C, M> Builder<C, M, retry::Standard> {
pub fn retry_policy<R>(self, retry_policy: R) -> Builder<C, M, R> {
Builder {
connector: self.connector,
standard_retry_config: None,
retry_policy,
timeout_config: self.timeout_config,
middleware: self.middleware,
sleep_impl: self.sleep_impl,
}
}
}
impl<C, M> Builder<C, M> {
pub fn set_retry_config(&mut self, config: retry::Config) {
self.standard_retry_config = Some(config.clone());
self.retry_policy.with_config(config);
}
pub fn set_timeout_config(&mut self, timeout_config: timeout::Config) {
self.timeout_config = timeout_config;
}
pub fn set_sleep_impl(&mut self, async_sleep: Option<Arc<dyn AsyncSleep>>) {
self.sleep_impl = async_sleep;
}
pub fn sleep_impl(mut self, async_sleep: Option<Arc<dyn AsyncSleep>>) -> Self {
self.set_sleep_impl(async_sleep);
self
}
}
impl<C, M, R> Builder<C, M, R> {
pub fn map_connector<F, C2>(self, map: F) -> Builder<C2, M, R>
where
F: FnOnce(C) -> C2,
{
Builder {
connector: map(self.connector),
middleware: self.middleware,
standard_retry_config: self.standard_retry_config,
retry_policy: self.retry_policy,
timeout_config: self.timeout_config,
sleep_impl: self.sleep_impl,
}
}
pub fn map_middleware<F, M2>(self, map: F) -> Builder<C, M2, R>
where
F: FnOnce(M) -> M2,
{
Builder {
connector: self.connector,
middleware: map(self.middleware),
standard_retry_config: self.standard_retry_config,
retry_policy: self.retry_policy,
timeout_config: self.timeout_config,
sleep_impl: self.sleep_impl,
}
}
pub fn build(self) -> Client<C, M, R> {
if self.sleep_impl.is_none() {
const ADDITIONAL_HELP: &str =
"Either disable retry by setting max attempts to one, or pass in a `sleep_impl`. \
If you're not using Tokio, then an implementation of the `AsyncSleep` trait from \
the `aws-smithy-async` crate is required for your async runtime. If you are using \
Tokio, then make sure the `rt-tokio` feature is enabled to have its sleep \
implementation set automatically.";
if self
.standard_retry_config
.map(|src| src.has_retry())
.unwrap_or(false)
{
panic!("Retries require a `sleep_impl`, but none was passed into the builder. {ADDITIONAL_HELP}");
} else if self.timeout_config.has_timeouts() {
panic!("Timeouts require a `sleep_impl`, but none was passed into the builder. {ADDITIONAL_HELP}");
}
}
Client {
connector: self.connector,
retry_policy: self.retry_policy,
middleware: self.middleware,
timeout_config: self.timeout_config,
sleep_impl: self.sleep_impl,
}
}
}
impl<C, M, R> Builder<C, M, R>
where
C: bounds::SmithyConnector,
M: bounds::SmithyMiddleware<erase::DynConnector> + Send + Sync + 'static,
R: retry::NewRequestPolicy,
{
pub fn build_dyn(self) -> erase::DynClient<R> {
self.build().into_dyn()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::never::NeverConnector;
use aws_smithy_async::rt::sleep::Sleep;
use aws_smithy_types::timeout;
use aws_smithy_types::tristate::TriState;
use std::panic::{self, AssertUnwindSafe};
use std::time::Duration;
#[derive(Clone, Debug)]
struct StubSleep;
impl AsyncSleep for StubSleep {
fn sleep(&self, _duration: Duration) -> Sleep {
todo!()
}
}
#[test]
fn defaults_dont_panic() {
let builder = Builder::new()
.connector(NeverConnector::new())
.middleware(tower::layer::util::Identity::new());
let _ = builder.build();
}
#[test]
fn defaults_panic_if_default_tokio_sleep_not_available() {
let mut builder = Builder::new()
.connector(NeverConnector::new())
.middleware(tower::layer::util::Identity::new());
builder.set_sleep_impl(None);
let result = panic::catch_unwind(AssertUnwindSafe(move || {
let _ = builder.build();
}));
assert!(result.is_err());
}
#[test]
fn timeouts_without_sleep_panics() {
let mut builder = Builder::new()
.connector(NeverConnector::new())
.middleware(tower::layer::util::Identity::new());
builder.set_sleep_impl(None);
let timeout_config = timeout::Config::new().with_http_timeouts(
timeout::Http::new().with_connect_timeout(TriState::Set(Duration::from_secs(1))),
);
assert!(timeout_config.has_timeouts());
builder.set_timeout_config(timeout_config);
let result = panic::catch_unwind(AssertUnwindSafe(move || {
let _ = builder.build();
}));
assert!(result.is_err());
}
#[test]
fn retry_without_sleep_panics() {
let mut builder = Builder::new()
.connector(NeverConnector::new())
.middleware(tower::layer::util::Identity::new());
builder.set_sleep_impl(None);
let retry_config = retry::Config::default();
assert!(retry_config.has_retry());
builder.set_retry_config(retry_config);
let result = panic::catch_unwind(AssertUnwindSafe(move || {
let _ = builder.build();
}));
assert!(result.is_err());
}
#[test]
fn custom_retry_policy_without_sleep_doesnt_panic() {
let mut builder = Builder::new()
.connector(NeverConnector::new())
.middleware(tower::layer::util::Identity::new())
.retry_policy(retry::Standard::default());
builder.set_sleep_impl(None);
let _ = builder.build();
}
#[test]
fn no_panics_when_sleep_given() {
let mut builder = Builder::new()
.connector(NeverConnector::new())
.middleware(tower::layer::util::Identity::new());
let timeout_config = timeout::Config::new().with_http_timeouts(
timeout::Http::new().with_connect_timeout(TriState::Set(Duration::from_secs(1))),
);
assert!(timeout_config.has_timeouts());
builder.set_timeout_config(timeout_config);
let retry_config = retry::Config::default();
assert!(retry_config.has_retry());
builder.set_retry_config(retry_config);
let _ = builder.build();
}
}