Skip to content

Commit

Permalink
deduplicate match arms in handle_client_request
Browse files Browse the repository at this point in the history
  • Loading branch information
yaahc committed Feb 19, 2021
1 parent cfc4717 commit 2adee7b
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions zebra-network/src/peer/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<Response, SharedPeerError>>,
) -> 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 {
Expand Down Expand Up @@ -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 },
}
}
Expand All @@ -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 },
}
}
Expand Down

0 comments on commit 2adee7b

Please sign in to comment.