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
pub use self::addr_stream::AddrStream;
use async_std::net::{SocketAddr, TcpListener};
use futures::FutureExt as _;
use futures_timer::Delay;
use hyper::server::accept::Accept;
use log::{debug, error, trace};
use std::fmt;
use std::future::Future;
use std::io;
use std::net::{TcpListener as StdListener, ToSocketAddrs};
use std::pin::Pin;
use std::task::{self, Poll};
use std::time::Duration;
#[must_use = "streams do nothing unless polled"]
pub struct AddrIncoming {
addr: SocketAddr,
listener: TcpListener,
sleep_on_errors: bool,
tcp_nodelay: bool,
timeout: Option<Delay>,
}
impl AddrIncoming {
pub(super) fn new(addr: impl ToSocketAddrs) -> io::Result<Self> {
let listener = StdListener::bind(addr)?;
AddrIncoming::from_std(listener)
}
pub(super) fn from_std(listener: StdListener) -> io::Result<Self> {
let addr = listener.local_addr()?;
Ok(AddrIncoming {
listener: listener.into(),
addr,
sleep_on_errors: true,
tcp_nodelay: false,
timeout: None,
})
}
pub fn bind(addr: impl ToSocketAddrs) -> io::Result<Self> {
AddrIncoming::new(addr)
}
pub fn local_addr(&self) -> SocketAddr {
self.addr
}
#[cfg_attr(tarpaulin, skip)]
pub fn set_nodelay(&mut self, enabled: bool) -> &mut Self {
self.tcp_nodelay = enabled;
self
}
#[cfg_attr(tarpaulin, skip)]
pub fn set_sleep_on_errors(&mut self, val: bool) {
self.sleep_on_errors = val;
}
fn poll_next_(&mut self, cx: &mut task::Context<'_>) -> Poll<io::Result<AddrStream>> {
if let Some(ref mut to) = self.timeout {
match Pin::new(to).poll(cx) {
Poll::Ready(()) => {}
Poll::Pending => return Poll::Pending,
}
}
self.timeout = None;
let accept = self.listener.accept();
futures::pin_mut!(accept);
loop {
match accept.poll_unpin(cx) {
Poll::Ready(Ok((socket, addr))) => {
if let Err(e) = socket.set_nodelay(self.tcp_nodelay) {
trace!("error trying to set TCP nodelay: {}", e);
}
return Poll::Ready(Ok(AddrStream::new(socket, addr)));
}
Poll::Pending => return Poll::Pending,
Poll::Ready(Err(e)) => {
if is_connection_error(&e) {
debug!("accepted connection already errored: {}", e);
continue;
}
if self.sleep_on_errors {
error!("accept error: {}", e);
let mut timeout = Delay::new(Duration::from_secs(1));
match Pin::new(&mut timeout).poll(cx) {
Poll::Ready(()) => {
continue;
}
Poll::Pending => {
self.timeout = Some(timeout);
return Poll::Pending;
}
}
} else {
return Poll::Ready(Err(e));
}
}
}
}
}
}
impl Accept for AddrIncoming {
type Conn = AddrStream;
type Error = io::Error;
fn poll_accept(
mut self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
let result = futures::ready!(self.poll_next_(cx));
Poll::Ready(Some(result))
}
}
#[cfg_attr(tarpaulin, skip)]
fn is_connection_error(e: &io::Error) -> bool {
match e.kind() {
io::ErrorKind::ConnectionRefused
| io::ErrorKind::ConnectionAborted
| io::ErrorKind::ConnectionReset => true,
_ => false,
}
}
#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for AddrIncoming {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AddrIncoming")
.field("addr", &self.addr)
.field("sleep_on_errors", &self.sleep_on_errors)
.field("tcp_nodelay", &self.tcp_nodelay)
.finish()
}
}
mod addr_stream {
use async_std::net::TcpStream;
use async_std::sync::Arc;
use std::io;
use std::net::SocketAddr;
use std::pin::Pin;
use std::task::{self, Poll};
use tokio::io::{AsyncRead, AsyncWrite};
#[derive(Debug, Clone)]
pub struct AddrStream {
inner: Arc<TcpStream>,
pub(super) remote_addr: SocketAddr,
}
impl AddrStream {
pub(super) fn new(tcp: TcpStream, addr: SocketAddr) -> AddrStream {
AddrStream {
inner: Arc::new(tcp),
remote_addr: addr,
}
}
#[inline]
pub fn remote_addr(&self) -> SocketAddr {
self.remote_addr
}
#[inline]
pub fn stream(&self) -> &TcpStream {
&*self.inner
}
}
impl AsyncRead for AddrStream {
#[inline]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
futures::AsyncRead::poll_read(Pin::new(&mut self.stream()), cx, buf)
}
}
impl AsyncWrite for AddrStream {
#[inline]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
futures::AsyncWrite::poll_write(Pin::new(&mut self.stream()), cx, buf)
}
#[inline]
fn poll_flush(self: Pin<&mut Self>, _cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
#[inline]
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
futures::AsyncWrite::poll_close(Pin::new(&mut self.stream()), cx)
}
}
}