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
use async_std::net::{SocketAddr, TcpListener, TcpStream};
use futures::FutureExt as _;
use futures_timer::Delay;
use log::{debug, error, trace};
use roa_core::{Accept, AddrStream};
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 TcpIncoming {
addr: SocketAddr,
listener: TcpListener,
sleep_on_errors: bool,
tcp_nodelay: bool,
timeout: Option<Delay>,
}
impl TcpIncoming {
pub fn bind(addr: impl ToSocketAddrs) -> io::Result<Self> {
let listener = StdListener::bind(addr)?;
TcpIncoming::from_std(listener)
}
pub fn from_std(listener: StdListener) -> io::Result<Self> {
let addr = listener.local_addr()?;
Ok(TcpIncoming {
listener: listener.into(),
addr,
sleep_on_errors: true,
tcp_nodelay: false,
timeout: None,
})
}
pub fn local_addr(&self) -> SocketAddr {
self.addr
}
pub fn set_nodelay(&mut self, enabled: bool) -> &mut Self {
self.tcp_nodelay = enabled;
self
}
pub fn set_sleep_on_errors(&mut self, val: bool) {
self.sleep_on_errors = val;
}
fn poll_stream(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<io::Result<(TcpStream, SocketAddr)>> {
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((stream, addr))) => {
if let Err(e) = stream.set_nodelay(self.tcp_nodelay) {
trace!("error trying to set TCP nodelay: {}", e);
}
return Poll::Ready(Ok((stream, 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 TcpIncoming {
type Conn = AddrStream<TcpStream>;
type Error = io::Error;
#[inline]
fn poll_accept(
mut self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
let (stream, addr) = futures::ready!(self.poll_stream(cx))?;
Poll::Ready(Some(Ok(AddrStream::new(addr, stream))))
}
}
fn is_connection_error(e: &io::Error) -> bool {
match e.kind() {
io::ErrorKind::ConnectionRefused
| io::ErrorKind::ConnectionAborted
| io::ErrorKind::ConnectionReset => true,
_ => false,
}
}
impl fmt::Debug for TcpIncoming {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TcpIncoming")
.field("addr", &self.addr)
.field("sleep_on_errors", &self.sleep_on_errors)
.field("tcp_nodelay", &self.tcp_nodelay)
.finish()
}
}