Skip to content

Commit 470e083

Browse files
committed
use RwLock instead of Mutex for packets
1 parent 29c1b69 commit 470e083

File tree

8 files changed

+33
-31
lines changed

8 files changed

+33
-31
lines changed

oryx-tui/src/app.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ratatui::{
55
};
66
use std::{
77
error,
8-
sync::{Arc, Mutex},
8+
sync::{Arc, RwLock},
99
thread,
1010
time::Duration,
1111
};
@@ -43,7 +43,7 @@ pub struct App {
4343
pub help: Help,
4444
pub filter: Filter,
4545
pub start_sniffing: bool,
46-
pub packets: Arc<Mutex<Vec<AppPacket>>>,
46+
pub packets: Arc<RwLock<Vec<AppPacket>>>,
4747
pub notifications: Vec<Notification>,
4848
pub section: Section,
4949
pub data_channel_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>,
@@ -59,7 +59,9 @@ impl Default for App {
5959

6060
impl App {
6161
pub fn new() -> Self {
62-
let packets = Arc::new(Mutex::new(Vec::with_capacity(RawPacket::LEN * 1024 * 1024)));
62+
let packets = Arc::new(RwLock::new(Vec::with_capacity(
63+
RawPacket::LEN * 1024 * 1024,
64+
)));
6365

6466
let (sender, receiver) = kanal::unbounded();
6567

@@ -70,7 +72,7 @@ impl App {
7072
move || loop {
7173
if let Ok((raw_packet, direction)) = receiver.recv() {
7274
let network_packet = NetworkPacket::from(raw_packet);
73-
let mut packets = packets.lock().unwrap();
75+
let mut packets = packets.write().unwrap();
7476
if packets.len() == packets.capacity() {
7577
packets.reserve(1024 * 1024);
7678
}

oryx-tui/src/filter/fuzzy.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
sync::{Arc, Mutex},
2+
sync::{Arc, Mutex, RwLock},
33
thread,
44
time::Duration,
55
};
@@ -24,7 +24,7 @@ pub struct Fuzzy {
2424
}
2525

2626
impl Fuzzy {
27-
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Arc<Mutex<Self>> {
27+
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Arc<Mutex<Self>> {
2828
let fuzzy = Arc::new(Mutex::new(Self::default()));
2929

3030
thread::spawn({
@@ -38,7 +38,7 @@ impl Fuzzy {
3838
let mut fuzzy = fuzzy.lock().unwrap();
3939

4040
if fuzzy.is_enabled() && !fuzzy.filter.value().is_empty() {
41-
let packets = packets.lock().unwrap();
41+
let packets = packets.read().unwrap();
4242
let current_pattern = fuzzy.filter.value().to_owned();
4343
if current_pattern != pattern {
4444
fuzzy.find(packets.as_slice());

oryx-tui/src/section.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod inspection;
44
pub mod metrics;
55
pub mod stats;
66

7-
use std::sync::{Arc, Mutex};
7+
use std::sync::{Arc, RwLock};
88

99
use alert::Alert;
1010
use crossterm::event::{KeyCode, KeyEvent};
@@ -49,7 +49,7 @@ pub struct Section {
4949

5050
impl Section {
5151
pub fn new(
52-
packets: Arc<Mutex<Vec<AppPacket>>>,
52+
packets: Arc<RwLock<Vec<AppPacket>>>,
5353
firewall_chans: IoChannels<FirewallSignal>,
5454
) -> Self {
5555
Self {

oryx-tui/src/section/alert.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ratatui::{
66
text::{Span, Text},
77
Frame,
88
};
9-
use std::sync::{atomic::Ordering, Arc, Mutex};
9+
use std::sync::{atomic::Ordering, Arc, RwLock};
1010
use syn_flood::SynFlood;
1111

1212
use crate::packet::AppPacket;
@@ -19,7 +19,7 @@ pub struct Alert {
1919
}
2020

2121
impl Alert {
22-
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
22+
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Self {
2323
Self {
2424
syn_flood: SynFlood::new(packets),
2525
flash_count: 1,

oryx-tui/src/section/alert/syn_flood.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
collections::HashMap,
33
net::IpAddr,
4-
sync::{atomic::AtomicBool, Arc, Mutex},
4+
sync::{atomic::AtomicBool, Arc, Mutex, RwLock},
55
thread,
66
time::Duration,
77
};
@@ -29,7 +29,7 @@ pub struct SynFlood {
2929
}
3030

3131
impl SynFlood {
32-
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
32+
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Self {
3333
let map: Arc<Mutex<HashMap<IpAddr, usize>>> = Arc::new(Mutex::new(HashMap::new()));
3434

3535
let detected = Arc::new(AtomicBool::new(false));
@@ -40,14 +40,14 @@ impl SynFlood {
4040
let detected = detected.clone();
4141
move || loop {
4242
let start_index = {
43-
let packets = packets.lock().unwrap();
43+
let packets = packets.read().unwrap();
4444
packets.len().saturating_sub(1)
4545
};
4646

4747
thread::sleep(Duration::from_secs(5));
4848

4949
let app_packets = {
50-
let packets = packets.lock().unwrap();
50+
let packets = packets.read().unwrap();
5151
packets.clone()
5252
};
5353

oryx-tui/src/section/inspection.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::{Arc, Mutex};
1+
use std::sync::{Arc, Mutex, RwLock};
22

33
use crossterm::event::{KeyCode, KeyEvent};
44
use ratatui::{
@@ -26,7 +26,7 @@ use crate::{
2626

2727
#[derive(Debug)]
2828
pub struct Inspection {
29-
pub packets: Arc<Mutex<Vec<AppPacket>>>,
29+
pub packets: Arc<RwLock<Vec<AppPacket>>>,
3030
pub state: TableState,
3131
pub fuzzy: Arc<Mutex<Fuzzy>>,
3232
pub manuall_scroll: bool,
@@ -36,7 +36,7 @@ pub struct Inspection {
3636
}
3737

3838
impl Inspection {
39-
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
39+
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Self {
4040
Self {
4141
packets: packets.clone(),
4242
state: TableState::default(),
@@ -49,7 +49,7 @@ impl Inspection {
4949
}
5050

5151
pub fn can_show_popup(&mut self) -> bool {
52-
let packets = self.packets.lock().unwrap();
52+
let packets = self.packets.read().unwrap();
5353
let fuzzy = self.fuzzy.lock().unwrap();
5454

5555
if fuzzy.is_enabled() {
@@ -134,7 +134,7 @@ impl Inspection {
134134
}
135135

136136
KeyCode::Char('s') => {
137-
let app_packets = self.packets.lock().unwrap();
137+
let app_packets = self.packets.read().unwrap();
138138
if app_packets.is_empty() {
139139
Notification::send(
140140
"There is no packets".to_string(),
@@ -168,7 +168,7 @@ impl Inspection {
168168
}
169169

170170
pub fn scroll_up(&mut self) {
171-
let app_packets = self.packets.lock().unwrap();
171+
let app_packets = self.packets.read().unwrap();
172172
if !self.manuall_scroll {
173173
self.manuall_scroll = true;
174174
// Record the last position. Usefull for selecting the packets to display
@@ -193,7 +193,7 @@ impl Inspection {
193193
}
194194

195195
pub fn scroll_down(&mut self) {
196-
let app_packets = self.packets.lock().unwrap();
196+
let app_packets = self.packets.read().unwrap();
197197

198198
if !self.manuall_scroll {
199199
self.manuall_scroll = true;
@@ -220,7 +220,7 @@ impl Inspection {
220220
}
221221

222222
pub fn render(&mut self, frame: &mut Frame, block: Rect) {
223-
let app_packets = self.packets.lock().unwrap();
223+
let app_packets = self.packets.read().unwrap();
224224
let mut fuzzy = self.fuzzy.lock().unwrap();
225225
let fuzzy_packets = fuzzy.clone().packets.clone();
226226

@@ -638,7 +638,7 @@ impl Inspection {
638638
.split(layout[1])[1];
639639

640640
let fuzzy = self.fuzzy.lock().unwrap();
641-
let packets = self.packets.lock().unwrap();
641+
let packets = self.packets.read().unwrap();
642642

643643
let app_packet = if fuzzy.is_enabled() {
644644
fuzzy.packets[self.packet_index.unwrap()]

oryx-tui/src/section/metrics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
cmp,
33
ops::Range,
4-
sync::{atomic::AtomicBool, Arc, Mutex},
4+
sync::{atomic::AtomicBool, Arc, Mutex, RwLock},
55
thread,
66
time::Duration,
77
};
@@ -39,7 +39,7 @@ struct ListState {
3939
#[derive(Debug)]
4040
pub struct Metrics {
4141
user_input: UserInput,
42-
app_packets: Arc<Mutex<Vec<AppPacket>>>,
42+
app_packets: Arc<RwLock<Vec<AppPacket>>>,
4343
metrics: Vec<Arc<Mutex<PortCountMetric>>>,
4444
terminate: Arc<AtomicBool>,
4545
state: ListState,
@@ -96,7 +96,7 @@ pub struct PortCountMetric {
9696
}
9797

9898
impl Metrics {
99-
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
99+
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Self {
100100
Self {
101101
user_input: UserInput::default(),
102102
app_packets: packets,
@@ -311,7 +311,7 @@ impl Metrics {
311311
'main: loop {
312312
thread::sleep(Duration::from_millis(100));
313313

314-
let app_packets = { packets.lock().unwrap().clone() };
314+
let app_packets = { packets.read().unwrap().clone() };
315315

316316
if app_packets.is_empty() {
317317
continue;

oryx-tui/src/section/stats.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
collections::HashMap,
33
net::IpAddr,
4-
sync::{Arc, Mutex},
4+
sync::{Arc, Mutex, RwLock},
55
thread,
66
time::Duration,
77
};
@@ -41,7 +41,7 @@ pub struct Stats {
4141
}
4242

4343
impl Stats {
44-
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
44+
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Self {
4545
let packet_stats: Arc<Mutex<PacketStats>> = Arc::new(Mutex::new(PacketStats::default()));
4646

4747
thread::spawn({
@@ -51,7 +51,7 @@ impl Stats {
5151
loop {
5252
thread::sleep(Duration::from_millis(500));
5353

54-
let app_packets = { packets.lock().unwrap().clone() };
54+
let app_packets = { packets.read().unwrap().clone() };
5555

5656
if app_packets.is_empty() {
5757
continue;

0 commit comments

Comments
 (0)