Skip to content

Commit 848eef8

Browse files
committed
Simplify unix route monitor
1 parent c07739b commit 848eef8

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed

talpid-routing/src/unix/mod.rs

+20-41
Original file line numberDiff line numberDiff line change
@@ -289,51 +289,29 @@ impl RouteManager {
289289
pub async fn stop(&mut self) {
290290
if let Some(tx) = self.manage_tx.take() {
291291
let (wait_tx, wait_rx) = oneshot::channel();
292-
293-
if tx
294-
.unbounded_send(RouteManagerCommand::Shutdown(wait_tx))
295-
.is_err()
296-
{
297-
log::error!("RouteManager already down!");
298-
return;
299-
}
300-
301-
if wait_rx.await.is_err() {
302-
log::error!("{}", Error::ManagerChannelDown);
303-
}
292+
let _ = tx.unbounded_send(RouteManagerCommand::Shutdown(wait_tx));
293+
let _ = wait_rx.await;
304294
}
305295
}
306296

307297
/// Applies the given routes until [`RouteManager::stop`] is called.
308298
pub async fn add_routes(&mut self, routes: HashSet<RequiredRoute>) -> Result<(), Error> {
309-
if let Some(tx) = &self.manage_tx {
310-
let (result_tx, result_rx) = oneshot::channel();
311-
if tx
312-
.unbounded_send(RouteManagerCommand::AddRoutes(routes, result_tx))
313-
.is_err()
314-
{
315-
return Err(Error::RouteManagerDown);
316-
}
317-
318-
result_rx
319-
.await
320-
.map_err(|_| Error::ManagerChannelDown)?
321-
.map_err(Error::PlatformError)
322-
} else {
323-
Err(Error::RouteManagerDown)
324-
}
299+
let tx = self.get_command_tx()?;
300+
let (result_tx, result_rx) = oneshot::channel();
301+
tx.unbounded_send(RouteManagerCommand::AddRoutes(routes, result_tx))
302+
.map_err(|_| Error::RouteManagerDown)?;
303+
304+
result_rx
305+
.await
306+
.map_err(|_| Error::ManagerChannelDown)?
307+
.map_err(Error::PlatformError)
325308
}
326309

327310
/// Removes all routes previously applied in [`RouteManager::add_routes`].
328311
pub fn clear_routes(&mut self) -> Result<(), Error> {
329-
if let Some(tx) = &self.manage_tx {
330-
if tx.unbounded_send(RouteManagerCommand::ClearRoutes).is_err() {
331-
return Err(Error::RouteManagerDown);
332-
}
333-
Ok(())
334-
} else {
335-
Err(Error::RouteManagerDown)
336-
}
312+
let tx = self.get_command_tx()?;
313+
tx.unbounded_send(RouteManagerCommand::ClearRoutes)
314+
.map_err(|_| Error::RouteManagerDown)
337315
}
338316

339317
/// Ensure that packets are routed using the correct tables.
@@ -350,11 +328,12 @@ impl RouteManager {
350328

351329
/// Retrieve a sender directly to the command channel.
352330
pub fn handle(&self) -> Result<RouteManagerHandle, Error> {
353-
if let Some(tx) = &self.manage_tx {
354-
Ok(RouteManagerHandle { tx: tx.clone() })
355-
} else {
356-
Err(Error::RouteManagerDown)
357-
}
331+
let tx = self.get_command_tx()?;
332+
Ok(RouteManagerHandle { tx: tx.clone() })
333+
}
334+
335+
fn get_command_tx(&self) -> Result<&Arc<UnboundedSender<RouteManagerCommand>>, Error> {
336+
self.manage_tx.as_ref().ok_or(Error::RouteManagerDown)
358337
}
359338
}
360339

0 commit comments

Comments
 (0)