forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp.rs
More file actions
307 lines (278 loc) · 11 KB
/
Copy pathtcp.rs
File metadata and controls
307 lines (278 loc) · 11 KB
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
use crate::{
dns::Resolver,
emit,
internal_events::{
TcpConnectionDisconnected, TcpConnectionEstablished, TcpConnectionFailed,
TcpConnectionShutdown, TcpEventSent, TcpFlushError,
},
sinks::util::{encode_event, encoding::EncodingConfig, Encoding, SinkBuildError, StreamSink},
sinks::{Healthcheck, RouterSink},
tls::{MaybeTlsConnector, MaybeTlsSettings, MaybeTlsStream, TlsConfig},
topology::config::SinkContext,
};
use bytes::Bytes;
use futures01::{
future, stream::iter_ok, try_ready, Async, AsyncSink, Future, Poll, Sink, StartSend,
};
use serde::{Deserialize, Serialize};
use snafu::Snafu;
use std::io::{ErrorKind, Read};
use std::net::SocketAddr;
use std::time::{Duration, Instant};
use tokio01::{
codec::{BytesCodec, FramedWrite},
net::tcp::TcpStream,
timer::Delay,
};
use tokio_retry::strategy::ExponentialBackoff;
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct TcpSinkConfig {
pub address: String,
pub encoding: EncodingConfig<Encoding>,
pub tls: Option<TlsConfig>,
}
impl TcpSinkConfig {
pub fn new(address: String, encoding: EncodingConfig<Encoding>) -> Self {
Self {
address,
encoding,
tls: None,
}
}
pub fn build(&self, cx: SinkContext) -> crate::Result<(RouterSink, Healthcheck)> {
let uri = self.address.parse::<http::Uri>()?;
let host = uri.host().ok_or(SinkBuildError::MissingHost)?.to_string();
let port = uri.port_u16().ok_or(SinkBuildError::MissingPort)?;
let tls = MaybeTlsSettings::from_config(&self.tls, false)?;
let tcp = TcpSink::new(host, port, cx.resolver(), tls);
let healthcheck = tcp.healthcheck();
let encoding = self.encoding.clone();
let sink = Box::new(
StreamSink::new(tcp, cx.acker())
.with_flat_map(move |event| iter_ok(encode_event(event, &encoding))),
);
Ok((sink, healthcheck))
}
}
pub struct TcpSink {
host: String,
port: u16,
resolver: Resolver,
tls: MaybeTlsSettings,
state: TcpSinkState,
backoff: ExponentialBackoff,
span: tracing::Span,
}
enum TcpSinkState {
Disconnected,
ResolvingDns(crate::dns::ResolverFuture),
Connecting(MaybeTlsConnector),
Connected(TcpOrTlsStream),
Backoff(Delay),
}
type TcpOrTlsStream = FramedWrite<MaybeTlsStream<TcpStream>, BytesCodec>;
impl TcpSink {
pub fn new(host: String, port: u16, resolver: Resolver, tls: MaybeTlsSettings) -> Self {
let span = info_span!("connection", %host, %port);
Self {
host,
port,
resolver,
tls,
state: TcpSinkState::Disconnected,
backoff: Self::fresh_backoff(),
span,
}
}
pub fn healthcheck(&self) -> Healthcheck {
tcp_healthcheck(
self.host.clone(),
self.port,
self.resolver,
self.tls.clone(),
)
}
fn fresh_backoff() -> ExponentialBackoff {
// TODO: make configurable
ExponentialBackoff::from_millis(2)
.factor(250)
.max_delay(Duration::from_secs(60))
}
fn next_delay(&mut self) -> Delay {
Delay::new(Instant::now() + self.backoff.next().unwrap())
}
fn poll_connection(&mut self) -> Poll<&mut TcpOrTlsStream, ()> {
loop {
self.state = match self.state {
TcpSinkState::Disconnected => {
debug!(message = "resolving dns.", host = %self.host);
let fut = self.resolver.lookup_ip_01(self.host.clone());
TcpSinkState::ResolvingDns(fut)
}
TcpSinkState::ResolvingDns(ref mut dns) => match dns.poll() {
Ok(Async::Ready(mut ips)) => {
if let Some(ip) = ips.next() {
let addr = SocketAddr::new(ip, self.port);
debug!(message = "connecting", %addr);
match self.tls.connect(self.host.clone(), addr) {
Ok(connector) => TcpSinkState::Connecting(connector),
Err(error) => {
error!(message = "unable to connect", %error);
TcpSinkState::Backoff(self.next_delay())
}
}
} else {
error!("DNS resolved but there were no IP addresses.");
TcpSinkState::Backoff(self.next_delay())
}
}
Ok(Async::NotReady) => return Ok(Async::NotReady),
Err(error) => {
error!(message = "unable to resolve dns.", %error);
TcpSinkState::Backoff(self.next_delay())
}
},
TcpSinkState::Backoff(ref mut delay) => match delay.poll() {
Ok(Async::NotReady) => return Ok(Async::NotReady),
// Err can only occur if the tokio runtime has been shutdown or if more than 2^63 timers have been created
Err(err) => unreachable!(err),
Ok(Async::Ready(())) => {
debug!(message = "disconnected.");
TcpSinkState::Disconnected
}
},
TcpSinkState::Connecting(ref mut connect_future) => match connect_future.poll() {
Ok(Async::Ready(stream)) => {
emit!(TcpConnectionEstablished {
peer_addr: stream.peer_addr().ok(),
});
self.backoff = Self::fresh_backoff();
TcpSinkState::Connected(FramedWrite::new(stream, BytesCodec::new()))
}
Ok(Async::NotReady) => return Ok(Async::NotReady),
Err(error) => {
emit!(TcpConnectionFailed { error });
TcpSinkState::Backoff(self.next_delay())
}
},
TcpSinkState::Connected(ref mut connection) => return Ok(Async::Ready(connection)),
};
}
}
}
impl Sink for TcpSink {
type SinkItem = Bytes;
type SinkError = ();
fn start_send(&mut self, line: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
let span = self.span.clone();
let _enter = span.enter();
match self.poll_connection() {
Ok(Async::Ready(connection)) => {
// Test if the remote has issued a disconnect by calling read(2)
// with a 1 sized buffer.
//
// This can return a proper disconnect error or `Ok(0)`
// which means the pipe is broken and we should try to reconnect.
//
// If this returns `WouldBlock` we know the connection is still
// valid and the write will most likely succeed.
match connection.get_mut().read(&mut [0u8; 1]) {
Err(error) if error.kind() != ErrorKind::WouldBlock => {
emit!(TcpConnectionDisconnected { error });
self.state = TcpSinkState::Disconnected;
Ok(AsyncSink::NotReady(line))
}
Ok(0) => {
// Maybe this is only a sign to close the channel,
// in which case we should try to flush our buffers
// before disconnecting.
match connection.poll_complete() {
// Flush done so we can safely disconnect, or
// error in which case we have really been
// disconnected.
Ok(Async::Ready(())) | Err(_) => {
emit!(TcpConnectionShutdown {});
self.state = TcpSinkState::Disconnected;
}
Ok(Async::NotReady) => (),
}
Ok(AsyncSink::NotReady(line))
}
_ => {
emit!(TcpEventSent {
byte_size: line.len()
});
match connection.start_send(line) {
Err(error) => {
error!(message = "connection disconnected.", %error);
self.state = TcpSinkState::Disconnected;
Ok(AsyncSink::Ready)
}
Ok(ok) => Ok(ok),
}
}
}
}
Ok(Async::NotReady) => Ok(AsyncSink::NotReady(line)),
Err(_) => unreachable!(),
}
}
fn poll_complete(&mut self) -> Result<Async<()>, Self::SinkError> {
// Stream::forward will immediately poll_complete the sink it's forwarding to,
// but we don't want to connect before the first event actually comes through.
if let TcpSinkState::Disconnected = self.state {
return Ok(Async::Ready(()));
}
let span = self.span.clone();
let _enter = span.enter();
let connection = try_ready!(self.poll_connection());
match connection.poll_complete() {
Err(error) => {
emit!(TcpFlushError { error });
self.state = TcpSinkState::Disconnected;
Ok(Async::Ready(()))
}
Ok(ok) => Ok(ok),
}
}
}
#[derive(Debug, Snafu)]
enum HealthcheckError {
#[snafu(display("Connect error: {}", source))]
ConnectError { source: crate::tls::TlsError },
#[snafu(display("Unable to resolve DNS: {}", source))]
DnsError { source: crate::dns::DnsError },
#[snafu(display("No addresses returned."))]
NoAddresses,
}
pub fn tcp_healthcheck(
host: String,
port: u16,
resolver: Resolver,
tls: MaybeTlsSettings,
) -> Healthcheck {
// Lazy to avoid immediately connecting
let check = future::lazy(move || {
resolver
.lookup_ip_01(host.clone())
.map_err(|source| HealthcheckError::DnsError { source }.into())
.and_then(|mut ip| {
ip.next()
.ok_or_else(|| HealthcheckError::NoAddresses.into())
})
.and_then(move |ip| {
let addr = SocketAddr::new(ip, port);
future::result(
tls.connect(host, addr)
.map_err(|source| HealthcheckError::ConnectError { source }.into()),
)
})
.and_then(|maybe| {
maybe
.map(|_| ())
.map_err(|source| HealthcheckError::ConnectError { source }.into())
})
});
Box::new(check)
}