refactor: simplify control flow and error handling in metrics and netlink
This commit is contained in:
3 files changed
+18
-20
No files matched your search
+6
-8
@@ -78,8 +78,10 @@ impl Transport {
|
||||
|
||||
let full_data = format!("{}\n", data);
|
||||
// take the stream out, write, put it back or set None on failure
|
||||
if let Some(s) = stream {
|
||||
if let Err(e) = std::io::Write::write_all(s, full_data.as_bytes()) {
|
||||
let write_error = stream
|
||||
.as_mut()
|
||||
.and_then(|s| std::io::Write::write_all(s, full_data.as_bytes()).err());
|
||||
if let Some(e) = write_error {
|
||||
warn!("TCP send failed: {}", e);
|
||||
*stream = None;
|
||||
*reconnect_backoff = (*reconnect_backoff * 2).min(MAX_RECONNECT_BACKOFF);
|
||||
@@ -87,7 +89,6 @@ impl Transport {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Metric {
|
||||
@@ -209,11 +210,8 @@ impl Metrics {
|
||||
}
|
||||
|
||||
// greedily drain without blocking
|
||||
loop {
|
||||
match self.metrics_rx.try_recv() {
|
||||
Ok(metric) => batch.push(metric),
|
||||
Err(_) => break,
|
||||
}
|
||||
while let Ok(metric) = self.metrics_rx.try_recv() {
|
||||
batch.push(metric);
|
||||
if batch.len() >= batch_size {
|
||||
break;
|
||||
}
|
||||
|
||||
+3
-5
@@ -85,9 +85,9 @@ impl Netlink {
|
||||
while let Some(reply) = iter.recv() {
|
||||
let (header, attrs) = reply?;
|
||||
|
||||
if header.ifindex == ifindex {
|
||||
if let Ok(kind) = attrs.get_kind() {
|
||||
if kind
|
||||
if header.ifindex == ifindex
|
||||
&& let Ok(kind) = attrs.get_kind()
|
||||
&& kind
|
||||
.to_str()
|
||||
.map_err(|_| NetlinkError::NlQdiscError("Invalid UTF-8".to_string()))?
|
||||
== "cake"
|
||||
@@ -98,8 +98,6 @@ impl Netlink {
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(NetlinkError::NoQdiscFound(ifname.to_string()))
|
||||
}
|
||||
|
||||
@@ -446,8 +446,8 @@ impl Ratecontroller {
|
||||
delta_delay_up: self.state_ul.delta_stat,
|
||||
});
|
||||
|
||||
if let Some(ref mut fd) = stats_fd {
|
||||
if let Err(e) = fd.write_all(
|
||||
let stats_write_error = stats_fd.as_mut().and_then(|fd| {
|
||||
fd.write_all(
|
||||
format!(
|
||||
"{},{},{},{},{},{},{},{}\n",
|
||||
stats_time.secs(),
|
||||
@@ -460,16 +460,19 @@ impl Ratecontroller {
|
||||
self.state_ul.current_rate as u64
|
||||
)
|
||||
.as_bytes(),
|
||||
) {
|
||||
)
|
||||
.err()
|
||||
});
|
||||
if let Some(e) = stats_write_error {
|
||||
warn!("Failed to write statistics: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
lastchg_t = now_t;
|
||||
}
|
||||
|
||||
if let Some(ref mut fd) = speed_hist_fd {
|
||||
if now_t.duration_since(lastdump_t).as_secs_f64() > 300.0 {
|
||||
if let Some(ref mut fd) = speed_hist_fd
|
||||
&& now_t.duration_since(lastdump_t).as_secs_f64() > 300.0
|
||||
{
|
||||
for i in 0..self.config.speed_hist_size as usize {
|
||||
let hist_time = Time::new(ClockId::Realtime);
|
||||
if let Err(e) = fd.write_all(
|
||||
@@ -490,5 +493,4 @@ impl Ratecontroller {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user