From ae5c6c1ea62b74bfde4abdc6683b1aab854ea3f2 Mon Sep 17 00:00:00 2001 From: Omer Yacine Date: Thu, 26 Dec 2024 12:29:54 +0200 Subject: [PATCH] chore(eth-websocket): remove some unnecessary wrappers (#2291) * remove unnecessary Arc< The inners are already `Arc<`ed * eth websocket: avoid locking control message sender and clone it instead * review(sami): move tx declaration close to where it's being used * review(sami): free rx end out of the Arc and arc the entire controller channel instead --- .../eth/web3_transport/websocket_transport.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/mm2src/coins/eth/web3_transport/websocket_transport.rs b/mm2src/coins/eth/web3_transport/websocket_transport.rs index 6d19573781..36b13bdb78 100644 --- a/mm2src/coins/eth/web3_transport/websocket_transport.rs +++ b/mm2src/coins/eth/web3_transport/websocket_transport.rs @@ -52,8 +52,8 @@ pub struct WebsocketTransport { #[derive(Debug)] struct ControllerChannel { - tx: Arc>>, - rx: Arc>>, + tx: UnboundedSender, + rx: AsyncMutex>, } enum ControllerMessage { @@ -86,11 +86,10 @@ impl WebsocketTransport { node, event_handlers, request_id: Arc::new(AtomicUsize::new(1)), - controller_channel: ControllerChannel { - tx: Arc::new(AsyncMutex::new(req_tx)), - rx: Arc::new(AsyncMutex::new(req_rx)), - } - .into(), + controller_channel: Arc::new(ControllerChannel { + tx: req_tx, + rx: AsyncMutex::new(req_rx), + }), connection_guard: Arc::new(AsyncMutex::new(())), proxy_sign_keypair: None, last_request_failed: Arc::new(AtomicBool::new(false)), @@ -298,7 +297,7 @@ impl WebsocketTransport { } pub(crate) async fn stop_connection_loop(&self) { - let mut tx = self.controller_channel.tx.lock().await; + let mut tx = self.controller_channel.tx.clone(); tx.send(ControllerMessage::Close) .await .expect("receiver channel must be alive"); @@ -357,12 +356,11 @@ async fn send_request( serialized_request = serde_json::to_string(&wrapper)?; } - let mut tx = transport.controller_channel.tx.lock().await; - let (notification_sender, notification_receiver) = oneshot::channel::>(); event_handlers.on_outgoing_request(&request_bytes); + let mut tx = transport.controller_channel.tx.clone(); tx.send(ControllerMessage::Request(WsRequest { request_id, serialized_request,