From 2adee7b31a2dc62cbcb0dfb98d8a402994ac516c Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Thu, 18 Feb 2021 13:37:36 -0800 Subject: [PATCH] deduplicate match arms in handle_client_request --- zebra-network/src/peer/connection.rs | 31 ++++++++++++---------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/zebra-network/src/peer/connection.rs b/zebra-network/src/peer/connection.rs index 7be5b8ee4..f225869c3 100644 --- a/zebra-network/src/peer/connection.rs +++ b/zebra-network/src/peer/connection.rs @@ -564,6 +564,16 @@ where use Request::*; let InProgressClientRequest { request, tx, span } = req; + // common logic for client requests without an expected response + fn continue_without_response( + tx: MustUseOneshotSender>, + ) -> Transition { + // Since we're not waiting for further messages, we need to + // send a response before dropping tx. + let _ = tx.send(Ok(Response::Nil)); + Transition::AwaitRequest + } + match request { Peers => match self.peer_tx.send(Message::GetAddr).await { Ok(()) => Transition::AwaitResponse { @@ -657,12 +667,7 @@ where }, PushTransaction(transaction) => { match self.peer_tx.send(Message::Tx(transaction)).await { - Ok(()) => { - // Since we're not waiting for further messages, we need to - // send a response before dropping tx. - let _ = tx.send(Ok(Response::Nil)); - Transition::AwaitRequest - } + Ok(()) => continue_without_response(tx), Err(e) => Transition::CloseResponse { e: e.into(), tx }, } } @@ -672,23 +677,13 @@ where .send(Message::Inv(hashes.iter().map(|h| (*h).into()).collect())) .await { - Ok(()) => { - // Since we're not waiting for further messages, we need to - // send a response before dropping tx. - let _ = tx.send(Ok(Response::Nil)); - Transition::AwaitRequest - } + Ok(()) => continue_without_response(tx), Err(e) => Transition::CloseResponse { e: e.into(), tx }, } } AdvertiseBlock(hash) => { match self.peer_tx.send(Message::Inv(vec![hash.into()])).await { - Ok(()) => { - // Since we're not waiting for further messages, we need to - // send a response before dropping tx. - let _ = tx.send(Ok(Response::Nil)); - Transition::AwaitRequest - } + Ok(()) => continue_without_response(tx), Err(e) => Transition::CloseResponse { e: e.into(), tx }, } }