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
use crate::counters::{STRUCT_LOG_CONNECT_ERROR_COUNT, STRUCT_LOG_TCP_CONNECT_COUNT};
use std::{
io,
io::Write,
net::{TcpStream, ToSocketAddrs},
time::{Duration, Instant},
};
const WRITE_TIMEOUT_MS: u64 = 2000;
const CONNECTION_TIMEOUT_MS: u64 = 5000;
pub(crate) struct TcpWriter {
endpoint: String,
stream: Option<TcpStream>,
last_connection_attempt: Option<Instant>,
}
impl TcpWriter {
pub fn new(endpoint: String) -> Self {
Self {
endpoint,
stream: None,
last_connection_attempt: None,
}
}
pub fn endpoint(&self) -> &str {
&self.endpoint
}
fn refresh_connection(&mut self) -> io::Result<()> {
if self
.last_connection_attempt
.map(|t| t.elapsed() > Duration::from_millis(1000))
.unwrap_or(true)
{
self.last_connection_attempt = Some(Instant::now());
match self.connect() {
Ok(stream) => {
self.stream = Some(stream);
Ok(())
}
Err(e) => {
eprintln!("[Logging] Failed to connect: {}", e);
STRUCT_LOG_CONNECT_ERROR_COUNT.inc();
Err(e)
}
}
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"unable to refresh connection",
))
}
}
fn connect(&mut self) -> io::Result<TcpStream> {
STRUCT_LOG_TCP_CONNECT_COUNT.inc();
let mut last_error = io::Error::new(
io::ErrorKind::Other,
format!("Unable to resolve and connect to {}", self.endpoint),
);
for addr in self.endpoint.to_socket_addrs()? {
match TcpStream::connect_timeout(&addr, Duration::from_millis(CONNECTION_TIMEOUT_MS)) {
Ok(stream) => {
if let Err(err) =
stream.set_write_timeout(Some(Duration::from_millis(WRITE_TIMEOUT_MS)))
{
STRUCT_LOG_CONNECT_ERROR_COUNT.inc();
eprintln!("[Logging] Failed to set write timeout: {}", err);
continue;
}
return Ok(stream);
}
Err(err) => last_error = err,
}
}
Err(last_error)
}
}
impl Write for TcpWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.stream.is_none() {
self.refresh_connection()?;
}
self.stream
.as_mut()
.ok_or_else(|| io::Error::new(io::ErrorKind::NotConnected, "No stream"))
.and_then(|stream| stream.write(buf))
.map_err(|e| {
self.stream = None;
e
})
}
fn flush(&mut self) -> io::Result<()> {
if let Some(mut stream) = self.stream.as_ref() {
stream.flush()
} else {
Err(io::Error::new(
io::ErrorKind::NotConnected,
"Can't flush, not connected",
))
}
}
}