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
use bytes::Bytes;
use futures::future::{self};
use futures::stream::{self, Stream};
use futures_util::io::AsyncRead;
use http::status::StatusCode;
use rand::{thread_rng, Rng};
use rusoto_core::{request::HttpDispatchError, RusotoError};
use std::{
future::Future,
io, iter,
marker::Unpin,
pin::Pin,
sync::Mutex,
task::{Context, Poll},
time::Duration,
};
use tokio::{runtime::Builder, time::delay_for};
pub struct AsyncReadAsSyncStreamOfBytes<R> {
reader: Mutex<R>,
buf: Vec<u8>,
}
pub const READ_BUF_SIZE: usize = 1024 * 1024 * 2;
impl<R> AsyncReadAsSyncStreamOfBytes<R> {
pub fn new(reader: R) -> Self {
Self {
reader: Mutex::new(reader),
buf: vec![0; READ_BUF_SIZE],
}
}
}
impl<R: AsyncRead + Unpin> Stream for AsyncReadAsSyncStreamOfBytes<R> {
type Item = io::Result<Bytes>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
let reader = this.reader.get_mut().expect("lock was poisoned");
let read_size = Pin::new(reader).poll_read(cx, &mut this.buf);
match read_size {
Poll::Pending => Poll::Pending,
Poll::Ready(Err(e)) => Poll::Ready(Some(Err(e))),
Poll::Ready(Ok(0)) => Poll::Ready(None),
Poll::Ready(Ok(n)) => Poll::Ready(Some(Ok(Bytes::copy_from_slice(&this.buf[..n])))),
}
}
}
pub fn error_stream(e: io::Error) -> impl Stream<Item = io::Result<Bytes>> + Unpin + Send + Sync {
stream::iter(iter::once(Err(e)))
}
pub fn block_on_external_io<F: Future>(f: F) -> F::Output {
Builder::new()
.basic_scheduler()
.enable_io()
.enable_time()
.build()
.expect("failed to create Tokio runtime")
.block_on(f)
}
pub trait RetryError {
fn is_retryable(&self) -> bool;
}
pub async fn retry<G, T, F, E>(mut action: G) -> Result<T, E>
where
G: FnMut() -> F,
F: Future<Output = Result<T, E>>,
E: RetryError,
{
const MAX_RETRY_DELAY: Duration = Duration::from_secs(32);
const MAX_RETRY_TIMES: usize = 4;
let mut retry_wait_dur = Duration::from_secs(1);
let mut final_result = action().await;
for _ in 1..MAX_RETRY_TIMES {
if let Err(e) = &final_result {
if e.is_retryable() {
delay_for(retry_wait_dur + Duration::from_millis(thread_rng().gen_range(0, 1000)))
.await;
retry_wait_dur = MAX_RETRY_DELAY.min(retry_wait_dur * 2);
final_result = action().await;
continue;
}
}
break;
}
final_result
}
pub async fn with_timeout<T, E, Fut>(timeout_duration: Duration, fut: Fut) -> Result<T, E>
where
Fut: Future<Output = Result<T, E>> + std::marker::Unpin,
E: From<Box<dyn std::error::Error + Send + Sync>>,
{
let timeout = tokio::time::delay_for(timeout_duration);
match future::select(fut, timeout).await {
future::Either::Left((resp, _)) => resp,
future::Either::Right(((), _)) => Err(box_err!(
"request timeout. duration: {:?}",
timeout_duration
)),
}
}
pub fn http_retriable(status: StatusCode) -> bool {
status.is_server_error() || status == StatusCode::REQUEST_TIMEOUT
}
impl<E> RetryError for RusotoError<E> {
fn is_retryable(&self) -> bool {
match self {
Self::HttpDispatch(e) => e.is_retryable(),
Self::Unknown(resp) if http_retriable(resp.status) => true,
_ => false,
}
}
}
impl RetryError for HttpDispatchError {
fn is_retryable(&self) -> bool {
true
}
}