Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if destination is already connected when infer the next ho… #523

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/core/src/message/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ pub trait PayloadSender {
/// Get access to DHT.
fn dht(&self) -> Arc<PeerRing>;

/// Used to check if destination is already connected when `infer_next_hop`
fn is_connected(&self, did: Did) -> bool;

/// Send a message payload to a specified DID.
async fn do_send_payload(&self, did: Did, payload: MessagePayload) -> Result<()>;

Expand All @@ -249,6 +252,10 @@ pub trait PayloadSender {
return Ok(next_hop);
}

if self.is_connected(destination) {
return Ok(destination);
}

match self.dht().find_successor(destination)? {
PeerRingAction::Some(did) => Ok(did),
PeerRingAction::RemoteAction(did, _) => Ok(did),
Expand Down
8 changes: 8 additions & 0 deletions crates/core/src/swarm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rings_derive::JudgeConnection;
use rings_transport::core::transport::BoxedTransport;
use rings_transport::core::transport::ConnectionInterface;
use rings_transport::core::transport::TransportMessage;
use rings_transport::core::transport::WebrtcConnectionState;
use rings_transport::error::Error as TransportError;
pub use types::MeasureImpl;
pub use types::WrappedDid;
Expand Down Expand Up @@ -322,6 +323,13 @@ impl PayloadSender for Swarm {
Swarm::dht(self)
}

fn is_connected(&self, did: Did) -> bool {
let Some(conn) = self.get_connection(did) else {
return false;
};
conn.webrtc_connection_state() == WebrtcConnectionState::Connected
}

async fn do_send_payload(&self, did: Did, payload: MessagePayload) -> Result<()> {
#[cfg(test)]
{
Expand Down