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
use std::pin::Pin;
use std::task::{Context, Poll};
use futures_util::stream::{Stream, StreamExt};
use crate::error::{ProtoError, ProtoErrorKind};
use crate::xfer::{DnsRequest, DnsResponse};
use crate::DnsHandle;
#[derive(Clone)]
#[must_use = "queries can only be sent through a ClientHandle"]
pub struct RetryDnsHandle<H>
where
H: DnsHandle + Unpin + Send,
H::Error: RetryableError,
{
handle: H,
attempts: usize,
}
impl<H> RetryDnsHandle<H>
where
H: DnsHandle + Unpin + Send,
H::Error: RetryableError,
{
pub fn new(handle: H, attempts: usize) -> Self {
Self { handle, attempts }
}
}
impl<H> DnsHandle for RetryDnsHandle<H>
where
H: DnsHandle + Send + Unpin + 'static,
H::Error: RetryableError,
{
type Response = Pin<Box<dyn Stream<Item = Result<DnsResponse, Self::Error>> + Send + Unpin>>;
type Error = <H as DnsHandle>::Error;
fn send<R: Into<DnsRequest>>(&mut self, request: R) -> Self::Response {
let request = request.into();
let stream = self.handle.send(request.clone());
Box::pin(RetrySendStream {
request,
handle: self.handle.clone(),
stream,
remaining_attempts: self.attempts,
})
}
}
struct RetrySendStream<H>
where
H: DnsHandle,
{
request: DnsRequest,
handle: H,
stream: <H as DnsHandle>::Response,
remaining_attempts: usize,
}
impl<H: DnsHandle + Unpin> Stream for RetrySendStream<H>
where
<H as DnsHandle>::Error: RetryableError,
{
type Item = Result<DnsResponse, <H as DnsHandle>::Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
loop {
match self.stream.poll_next_unpin(cx) {
Poll::Ready(Some(Err(e))) => {
if self.remaining_attempts == 0 || !e.should_retry() {
return Poll::Ready(Some(Err(e)));
}
if e.attempted() {
self.remaining_attempts -= 1;
}
let request = self.request.clone();
self.stream = self.handle.send(request);
}
poll => return poll,
}
}
}
}
pub trait RetryableError {
fn should_retry(&self) -> bool;
fn attempted(&self) -> bool;
}
impl RetryableError for ProtoError {
fn should_retry(&self) -> bool {
true
}
fn attempted(&self) -> bool {
!matches!(self.kind(), ProtoErrorKind::Busy)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::error::*;
use crate::op::*;
use crate::xfer::FirstAnswer;
use futures_executor::block_on;
use futures_util::future::*;
use futures_util::stream::*;
use std::sync::{
atomic::{AtomicU16, Ordering},
Arc,
};
use DnsHandle;
#[derive(Clone)]
struct TestClient {
last_succeed: bool,
retries: u16,
attempts: Arc<AtomicU16>,
}
impl DnsHandle for TestClient {
type Response = Box<dyn Stream<Item = Result<DnsResponse, ProtoError>> + Send + Unpin>;
type Error = ProtoError;
fn send<R: Into<DnsRequest>>(&mut self, _: R) -> Self::Response {
let i = self.attempts.load(Ordering::SeqCst);
if (i > self.retries || self.retries - i == 0) && self.last_succeed {
let mut message = Message::new();
message.set_id(i);
return Box::new(once(ok(message.into())));
}
self.attempts.fetch_add(1, Ordering::SeqCst);
Box::new(once(err(ProtoError::from("last retry set to fail"))))
}
}
#[test]
fn test_retry() {
let mut handle = RetryDnsHandle::new(
TestClient {
last_succeed: true,
retries: 1,
attempts: Arc::new(AtomicU16::new(0)),
},
2,
);
let test1 = Message::new();
let result = block_on(handle.send(test1).first_answer()).expect("should have succeeded");
assert_eq!(result.id(), 1);
}
#[test]
fn test_error() {
let mut client = RetryDnsHandle::new(
TestClient {
last_succeed: false,
retries: 1,
attempts: Arc::new(AtomicU16::new(0)),
},
2,
);
let test1 = Message::new();
assert!(block_on(client.send(test1).first_answer()).is_err());
}
}