Skip to content

Commit

Permalink
refactor!: Return RpcError from Endpoint::is_reachable
Browse files Browse the repository at this point in the history
The `is_reachable` method performs an endpoint echo operation against a
given peer address. `RpcError` covers all the possible errors that can
occur during this operation, and no more.

BREAKING CHANGE: `Endpoint::is_reachable` now returns `Result<(),
RpcError>`, rather than `Result<(), Error>`.
  • Loading branch information
Chris Connelly authored and connec committed Aug 27, 2021
1 parent e528134 commit 674ad2d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
16 changes: 4 additions & 12 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,30 +351,22 @@ impl<I: ConnId> Endpoint<I> {
/// Verify if an address is publicly reachable. This will attempt to create
/// a new connection and use it to exchange a message and verify that the node
/// can be reached.
pub async fn is_reachable(&self, peer_addr: &SocketAddr) -> Result<(), Error> {
pub async fn is_reachable(&self, peer_addr: &SocketAddr) -> Result<(), RpcError> {
trace!("Checking is reachable");
let connection = self.get_or_connect_to(peer_addr).await?;
let (mut send_stream, mut recv_stream) = connection.open_bi(0).await?;

send_stream.send(WireMsg::EndpointEchoReq).await?;

match timeout(ECHO_SERVICE_QUERY_TIMEOUT, recv_stream.next_wire_msg()).await {
Ok(Ok(WireMsg::EndpointEchoResp(_))) => Ok(()),
Ok(Ok(other)) => {
match timeout(ECHO_SERVICE_QUERY_TIMEOUT, recv_stream.next_wire_msg()).await?? {
WireMsg::EndpointEchoResp(_) => Ok(()),
other => {
info!(
"Unexpected message type when verifying reachability: {}",
&other
);
Ok(())
}
Ok(Err(err)) => {
info!("Unable to contact peer: {:?}", err);
Err(err.into())
}
Err(err) => {
info!("Unable to contact peer: {:?}", err);
Err(Error::NoEchoServiceResponse)
}
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ pub enum Error {
/// quinn connection error
#[error("Connection lost due to error: {0}")]
ConnectionError(#[from] ConnectionError),
/// Timeout occurred when awaiting for a response from
/// any of the peers contacted for the echo service.
#[error("No response received from echo services")]
NoEchoServiceResponse,
/// Missing connection
#[error("No connection to the dest peer")]
MissingConnection,
Expand Down

0 comments on commit 674ad2d

Please sign in to comment.