refactor: replace std::sync::mpsc with flume for improved channel performance
Migrate internal message passing to flume to leverage better performance and additional features like eventual fairness and selection.
This commit is contained in:
8 files changed
+159
-12
No files matched your search
+1
-1
@@ -4,11 +4,11 @@ use crate::metrics::{Metric, MetricsSender};
|
||||
use crate::pinger::PingReply;
|
||||
use crate::util::ArcMutex;
|
||||
use crate::util::MutexExt;
|
||||
use flume::{Receiver, Sender};
|
||||
use log::{debug, info};
|
||||
use std::collections::HashMap;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::mpsc::{Receiver, Sender};
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
|
||||
+5
-5
@@ -16,11 +16,11 @@ mod util;
|
||||
use crate::baseliner::{Baseliner, ReflectorStats};
|
||||
use crate::metrics::{Metric, Metrics, MetricsSender};
|
||||
use ::log::{debug, info};
|
||||
use flume::RecvTimeoutError;
|
||||
use std::collections::HashMap;
|
||||
use std::net::IpAddr;
|
||||
use std::str::FromStr;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
||||
use std::sync::mpsc::{RecvTimeoutError, channel, sync_channel};
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
@@ -98,14 +98,14 @@ fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
let (baseliner_stats_tx, baseliner_stats_rx) = channel();
|
||||
let (error_tx, error_rx) = channel::<anyhow::Error>();
|
||||
let (reselect_tx, reselect_rx) = channel();
|
||||
let (baseliner_stats_tx, baseliner_stats_rx) = flume::unbounded();
|
||||
let (error_tx, error_rx) = flume::unbounded::<anyhow::Error>();
|
||||
let (reselect_tx, reselect_rx) = flume::unbounded();
|
||||
|
||||
let dropped = Arc::new(AtomicU32::new(0));
|
||||
|
||||
let (metrics_tx, metrics_thread_handle) = if config.observability_enabled {
|
||||
let (tx, rx) = sync_channel(1000);
|
||||
let (tx, rx) = flume::bounded(1000);
|
||||
let metrics = Metrics {
|
||||
config: config.clone(),
|
||||
metrics_rx: rx,
|
||||
|
||||
+3
-3
@@ -1,13 +1,13 @@
|
||||
use crate::Config;
|
||||
use crate::config::{MeasurementType, ObservabilityProtocol};
|
||||
use crate::time::Time;
|
||||
use flume::{Receiver, RecvTimeoutError, Sender};
|
||||
use log::{error, info, warn};
|
||||
use rustix::time::ClockId;
|
||||
use std::fmt::Write;
|
||||
use std::net::{IpAddr, TcpStream, UdpSocket};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::sync::mpsc::{Receiver, RecvTimeoutError, SyncSender};
|
||||
use std::time::Duration;
|
||||
|
||||
const MAX_RECONNECT_BACKOFF: u64 = 60;
|
||||
@@ -122,12 +122,12 @@ pub enum Metric {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MetricsSender {
|
||||
tx: Option<SyncSender<(Metric, u64)>>,
|
||||
tx: Option<Sender<(Metric, u64)>>,
|
||||
dropped: Arc<AtomicU32>,
|
||||
}
|
||||
|
||||
impl MetricsSender {
|
||||
pub fn new(tx: SyncSender<(Metric, u64)>, dropped: Arc<AtomicU32>) -> Self {
|
||||
pub fn new(tx: Sender<(Metric, u64)>, dropped: Arc<AtomicU32>) -> Self {
|
||||
Self {
|
||||
tx: Some(tx),
|
||||
dropped,
|
||||
|
||||
+1
-1
@@ -2,12 +2,12 @@ use crate::MeasurementType;
|
||||
use crate::SHUTDOWN;
|
||||
use crate::metrics::{Metric, MetricsSender};
|
||||
use crate::util::RwLockExt;
|
||||
use flume::Sender;
|
||||
use icmp_socket2::socket::IcmpSocket;
|
||||
use icmp_socket2::{IcmpSocket4, Icmpv4Packet};
|
||||
use log::{debug, info, warn};
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::time::{Duration, Instant};
|
||||
use std::{io, thread};
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::netlink::{Netlink, NetlinkError, Qdisc};
|
||||
use crate::time::Time;
|
||||
use crate::util::{ArcMutex, ArcRwLock, MutexExt, RwLockExt};
|
||||
use crate::{Config, ReflectorStats};
|
||||
use flume::Sender;
|
||||
use log::{debug, info, warn};
|
||||
use rustix::thread::ClockId;
|
||||
use std::collections::HashMap;
|
||||
@@ -11,7 +12,6 @@ use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::thread::sleep;
|
||||
use std::time::{Duration, Instant};
|
||||
use thiserror::Error;
|
||||
|
||||
@@ -2,11 +2,11 @@ use crate::SHUTDOWN;
|
||||
use crate::metrics::{Metric, MetricsSender};
|
||||
use crate::util::{ArcMutex, MutexExt, RwLockExt};
|
||||
use crate::{Config, ReflectorStats};
|
||||
use flume::Receiver;
|
||||
use log::{debug, info};
|
||||
use std::collections::HashMap;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::mpsc::Receiver;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
|
||||
Reference in new issue
Block a user