From 71487fcf5bdc6b9b2ef59d69ff5889a3023cec05 Mon Sep 17 00:00:00 2001 From: umgefahren <55623006+umgefahren@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:04:13 +0100 Subject: [PATCH] Correct minor things --- protocols/autonatv2/src/client/behaviour.rs | 36 +++++++++---------- .../autonatv2/src/client/handler/request.rs | 4 +-- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/protocols/autonatv2/src/client/behaviour.rs b/protocols/autonatv2/src/client/behaviour.rs index 655c6666b09..d6835507fd9 100644 --- a/protocols/autonatv2/src/client/behaviour.rs +++ b/protocols/autonatv2/src/client/behaviour.rs @@ -191,8 +191,9 @@ where &mut self, _cx: &mut Context<'_>, ) -> Poll::FromBehaviour>> { - if let Some(event) = self.pending_events.pop_front() { - return Poll::Ready(event); + let pending_event = self.poll_pending_events(); + if pending_event.is_ready() { + return pending_event; } self.pending_req_for_peer.retain(|_, reqs| !reqs.is_empty()); for (peer, dial_requests) in &mut self.pending_req_for_peer { @@ -210,8 +211,8 @@ where self.pending_requests.push_front(dial_request); } else { let peer = self.known_servers.choose(&mut self.rng).unwrap(); - self.submit_req_for_peer(*peer, dial_request); + return self.poll_pending_events(); } } Poll::Pending @@ -253,28 +254,23 @@ where .unwrap_or_default() { if let Some(new_peer) = self.known_servers.choose(&mut self.rng) { - if let Some(conn_id) = self.peers_to_handlers.get(new_peer) { - self.pending_events.push_back(ToSwarm::NotifyHandler { - peer_id: *new_peer, - handler: NotifyHandler::One(*conn_id), - event: Either::Left(RequestFromBehaviour::PerformRequest(dial_request)), - }) - } else { - self.pending_events.push_back(ToSwarm::Dial { - opts: DialOpts::peer_id(*new_peer) - .condition(PeerCondition::DisconnectedAndNotDialing) - .build(), - }); - self.pending_req_for_peer - .entry(*new_peer) - .or_default() - .push_back(dial_request); - } + self.submit_req_for_peer(*new_peer, dial_request); } else { self.pending_requests.push_front(dial_request); } } } + + fn poll_pending_events( + &mut self, + ) -> Poll< + ToSwarm<::ToSwarm, ::FromBehaviour>, + > { + if let Some(event) = self.pending_events.pop_front() { + return Poll::Ready(event); + } + Poll::Pending + } } fn addr_is_local(addr: &Multiaddr) -> bool { diff --git a/protocols/autonatv2/src/client/handler/request.rs b/protocols/autonatv2/src/client/handler/request.rs index 21b8879b19a..6a543c31ab6 100644 --- a/protocols/autonatv2/src/client/handler/request.rs +++ b/protocols/autonatv2/src/client/handler/request.rs @@ -46,7 +46,7 @@ pub(crate) enum Error { #[error("server chose not to dial any provided address")] ServerChoseNotToDialAnyAddress, #[error("server ran into an internal error")] - InternalServerError, + InternalServer, #[error("server did not respond correctly to dial request")] InvalidResponse, #[error("server was unable to connect to address: {addr:?}")] @@ -253,7 +253,7 @@ fn test_end_from_dial_response( match (resp.status, resp.dial_status) { (ResponseStatus::E_REQUEST_REJECTED, _) => Err(Error::ServerRejectedDialRequest), (ResponseStatus::E_DIAL_REFUSED, _) => Err(Error::ServerChoseNotToDialAnyAddress), - (ResponseStatus::E_INTERNAL_ERROR, _) => Err(Error::InternalServerError), + (ResponseStatus::E_INTERNAL_ERROR, _) => Err(Error::InternalServer), (ResponseStatus::OK, DialStatus::UNUSED) => Err(Error::InvalidResponse), (ResponseStatus::OK, DialStatus::E_DIAL_ERROR) => { Err(Error::UnableToConnectOnSelectedAddress {