Skip to content

Commit 3ebd5dd

Browse files
committed
refactor: improve efficiency and maintainability based on code review
- Optimize start_listener to avoid second hashmap lookup - Simplify stop_listener using remove() directly instead of get_mut + drain + remove - Extract duplicated test code into create_test_listener_config helper function - Improve code readability and performance Signed-off-by: Eeshu-Yadav <eeshuyadav123@gmail.com>
1 parent 34781cd commit 3ebd5dd

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

orion-lib/src/listeners/listeners_manager.rs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,10 @@ impl ListenersManager {
154154
pub fn stop_listener(&mut self, listener_name: &str) -> Result<()> {
155155
if let Some(listeners) = self.listener_handles.get_vec_mut(listener_name) {
156156
info!("Stopping all {} version(s) of listener {}", listeners.len(), listener_name);
157-
for listener_info in listeners.drain(..) {
157+
for listener_info in listeners {
158158
info!("Stopping listener {} version {}", listener_name, listener_info.version);
159159
listener_info.handle.abort();
160160
}
161-
self.listener_handles.remove(listener_name);
162161
} else {
163162
info!("No listeners found with name {}", listener_name);
164163
}
@@ -175,9 +174,22 @@ mod tests {
175174
};
176175

177176
use super::*;
178-
use orion_configuration::config::Listener as ListenerConfig;
177+
use orion_configuration::config::{Listener as ListenerConfig, ListenerAddress};
179178
use tracing_test::traced_test;
180179

180+
fn create_test_listener_config(name: &str, port: u16) -> ListenerConfig {
181+
ListenerConfig {
182+
name: name.into(),
183+
address: ListenerAddress::Socket(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port)),
184+
filter_chains: HashMap::default(),
185+
bind_device: None,
186+
with_tls_inspector: false,
187+
proxy_protocol_config: None,
188+
with_tlv_listener_filter: false,
189+
tlv_listener_filter_config: None,
190+
}
191+
}
192+
181193
#[traced_test]
182194
#[tokio::test]
183195
async fn start_listener_dup() {
@@ -282,50 +294,23 @@ mod tests {
282294
let (routeb_tx1, routeb_rx) = broadcast::channel(chan);
283295
let (_secb_tx1, secb_rx) = broadcast::channel(chan);
284296
let l1 = Listener::test_listener(name, routeb_rx, secb_rx);
285-
let l1_info = ListenerConfig {
286-
name: name.into(),
287-
address: ListenerAddress::Socket(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 1234)),
288-
filter_chains: HashMap::default(),
289-
bind_device: None,
290-
with_tls_inspector: false,
291-
proxy_protocol_config: None,
292-
with_tlv_listener_filter: false,
293-
tlv_listener_filter_config: None,
294-
};
297+
let l1_info = create_test_listener_config(name, 1234);
295298
man.start_listener(l1, l1_info).unwrap();
296299
assert!(routeb_tx1.send(RouteConfigurationChange::Removed("n/a".into())).is_ok());
297300
tokio::task::yield_now().await;
298301

299302
let (routeb_tx2, routeb_rx) = broadcast::channel(chan);
300303
let (_secb_tx2, secb_rx) = broadcast::channel(chan);
301304
let l2 = Listener::test_listener(name, routeb_rx, secb_rx);
302-
let l2_info = ListenerConfig {
303-
name: name.into(),
304-
address: ListenerAddress::Socket(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 1235)), // Different port
305-
filter_chains: HashMap::default(),
306-
bind_device: None,
307-
with_tls_inspector: false,
308-
proxy_protocol_config: None,
309-
with_tlv_listener_filter: false,
310-
tlv_listener_filter_config: None,
311-
};
305+
let l2_info = create_test_listener_config(name, 1235);
312306
man.start_listener(l2, l2_info).unwrap();
313307
assert!(routeb_tx2.send(RouteConfigurationChange::Removed("n/a".into())).is_ok());
314308
tokio::task::yield_now().await;
315309

316310
let (routeb_tx3, routeb_rx) = broadcast::channel(chan);
317311
let (_secb_tx3, secb_rx) = broadcast::channel(chan);
318312
let l3 = Listener::test_listener(name, routeb_rx, secb_rx);
319-
let l3_info = ListenerConfig {
320-
name: name.into(),
321-
address: ListenerAddress::Socket(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 1236)), // Different port
322-
filter_chains: HashMap::default(),
323-
bind_device: None,
324-
with_tls_inspector: false,
325-
proxy_protocol_config: None,
326-
with_tlv_listener_filter: false,
327-
tlv_listener_filter_config: None,
328-
};
313+
let l3_info = create_test_listener_config(name, 1236);
329314
man.start_listener(l3, l3_info).unwrap();
330315
assert!(routeb_tx3.send(RouteConfigurationChange::Removed("n/a".into())).is_ok());
331316
tokio::task::yield_now().await;

0 commit comments

Comments
 (0)