protocols/req-resp: Make is_pending_outbound return true on pending connection (#1928)

`is_pending_outbound` should return true if the connection
to the mentioned peer hasn't been established, yet.

Closes issue: #1885

Signed-off-by: Tejas Sanap <sanap.tejas@gmail.com>
This commit is contained in:
Tejas Sanap
2021-01-21 19:04:58 +05:30
committed by GitHub
parent bbd3df64ed
commit 5d22e30cc9
2 changed files with 10 additions and 2 deletions

View File

@@ -431,9 +431,16 @@ where
/// [`PeerId`] initiated by [`RequestResponse::send_request`] is still
/// pending, i.e. waiting for a response.
pub fn is_pending_outbound(&self, peer: &PeerId, request_id: &RequestId) -> bool {
self.connected.get(peer)
// Check if request is already sent on established connection.
let est_conn = self.connected.get(peer)
.map(|cs| cs.iter().any(|c| c.pending_inbound_responses.contains(request_id)))
.unwrap_or(false)
.unwrap_or(false);
// Check if request is still pending to be sent.
let pen_conn = self.pending_outbound_requests.get(peer)
.map(|rps| rps.iter().any(|rp| {rp.request_id == *request_id}))
.unwrap_or(false);
est_conn || pen_conn
}
/// Checks whether an inbound request from the peer with the provided

View File

@@ -93,6 +93,7 @@ fn ping_protocol() {
let addr = rx.next().await.unwrap();
swarm2.add_address(&peer1_id, addr.clone());
let mut req_id = swarm2.send_request(&peer1_id, ping.clone());
assert!(swarm2.is_pending_outbound(&peer1_id, &req_id));
loop {
match swarm2.next().await {