forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunix.rs
More file actions
220 lines (192 loc) · 6.2 KB
/
Copy pathunix.rs
File metadata and controls
220 lines (192 loc) · 6.2 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
use crate::{
buffers::Acker,
config::SinkContext,
internal_events::{
ConnectionOpen, OpenGauge, SocketMode, UnixSocketConnectionEstablished,
UnixSocketConnectionFailed, UnixSocketError,
},
sink::VecSinkExt,
sinks::{
util::{
retries::ExponentialBackoff,
socket_bytes_sink::{BytesSink, ShutdownCheck},
StreamSink,
},
Healthcheck, VectorSink,
},
Event,
};
use async_trait::async_trait;
use bytes::Bytes;
use futures::{stream::BoxStream, SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use snafu::{ResultExt, Snafu};
use std::{path::PathBuf, pin::Pin, sync::Arc, time::Duration};
use tokio::{net::UnixStream, time::delay_for};
#[derive(Debug, Snafu)]
pub enum UnixError {
#[snafu(display("Connect error: {}", source))]
ConnectError { source: tokio::io::Error },
}
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct UnixSinkConfig {
pub path: PathBuf,
}
impl UnixSinkConfig {
pub fn new(path: PathBuf) -> Self {
Self { path }
}
pub fn build(
&self,
cx: SinkContext,
encode_event: impl Fn(Event) -> Option<Bytes> + Send + Sync + 'static,
) -> crate::Result<(VectorSink, Healthcheck)> {
let connector = UnixConnector::new(self.path.clone());
let sink = UnixSink::new(connector.clone(), cx.acker(), encode_event);
Ok((
VectorSink::Stream(Box::new(sink)),
Box::pin(async move { connector.healthcheck().await }),
))
}
}
#[derive(Debug, Clone)]
struct UnixConnector {
pub path: PathBuf,
}
impl UnixConnector {
fn new(path: PathBuf) -> Self {
Self { path }
}
fn fresh_backoff() -> ExponentialBackoff {
// TODO: make configurable
ExponentialBackoff::from_millis(2)
.factor(250)
.max_delay(Duration::from_secs(60))
}
async fn connect(&self) -> Result<UnixStream, UnixError> {
UnixStream::connect(&self.path).await.context(ConnectError)
}
async fn connect_backoff(&self) -> UnixStream {
let mut backoff = Self::fresh_backoff();
loop {
match self.connect().await {
Ok(stream) => {
emit!(UnixSocketConnectionEstablished { path: &self.path });
return stream;
}
Err(error) => {
emit!(UnixSocketConnectionFailed {
error,
path: &self.path
});
delay_for(backoff.next().unwrap()).await;
}
}
}
}
async fn healthcheck(&self) -> crate::Result<()> {
self.connect().await.map(|_| ()).map_err(Into::into)
}
}
struct UnixSink {
connector: UnixConnector,
acker: Acker,
encode_event: Arc<dyn Fn(Event) -> Option<Bytes> + Send + Sync>,
}
impl UnixSink {
pub fn new(
connector: UnixConnector,
acker: Acker,
encode_event: impl Fn(Event) -> Option<Bytes> + Send + Sync + 'static,
) -> Self {
Self {
connector,
acker,
encode_event: Arc::new(encode_event),
}
}
async fn connect(&mut self) -> BytesSink<UnixStream> {
let stream = self.connector.connect_backoff().await;
BytesSink::new(
stream,
|_| ShutdownCheck::Alive,
self.acker.clone(),
SocketMode::Unix,
)
}
}
#[async_trait]
impl StreamSink for UnixSink {
// Same as TcpSink, more details there.
async fn run(&mut self, input: BoxStream<'_, Event>) -> Result<(), ()> {
let encode_event = Arc::clone(&self.encode_event);
let mut input = input
.map(|event| encode_event(event).unwrap_or_else(Bytes::new))
.peekable();
while Pin::new(&mut input).peek().await.is_some() {
let mut sink = self.connect().await;
let _open_token = OpenGauge::new().open(|count| emit!(ConnectionOpen { count }));
let result = match sink.send_all_peekable(&mut input).await {
Ok(()) => sink.close().await,
Err(error) => Err(error),
};
if let Err(error) = result {
emit!(UnixSocketError {
error,
path: &self.connector.path
});
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sinks::util::{encode_event, Encoding};
use crate::test_util::{random_lines_with_stream, CountReceiver};
use tokio::net::UnixListener;
fn temp_uds_path(name: &str) -> PathBuf {
tempfile::tempdir().unwrap().into_path().join(name)
}
#[tokio::test]
async fn unix_sink_healthcheck() {
let good_path = temp_uds_path("valid_uds");
let _listener = UnixListener::bind(&good_path).unwrap();
assert!(UnixSinkConfig::new(good_path)
.build(SinkContext::new_test(), |_| None)
.unwrap()
.1
.await
.is_ok());
let bad_path = temp_uds_path("no_one_listening");
assert!(UnixSinkConfig::new(bad_path)
.build(SinkContext::new_test(), |_| None)
.unwrap()
.1
.await
.is_err());
}
#[tokio::test]
async fn basic_unix_sink() {
let num_lines = 1000;
let out_path = temp_uds_path("unix_test");
// Set up server to receive events from the Sink.
let mut receiver = CountReceiver::receive_lines_unix(out_path.clone());
// Set up Sink
let config = UnixSinkConfig::new(out_path);
let cx = SinkContext::new_test();
let encoding = Encoding::Text.into();
let (sink, _healthcheck) = config
.build(cx, move |event| encode_event(event, &encoding))
.unwrap();
// Send the test data
let (input_lines, events) = random_lines_with_stream(100, num_lines);
sink.run(events).await.unwrap();
// Wait for output to connect
receiver.connected().await;
// Receive the data sent by the Sink to the receiver
assert_eq!(input_lines, receiver.await);
}
}