fallback for inbound relaying

This commit is contained in:
Christien Rioux 2024-03-25 21:15:03 -05:00
parent 4d7e7ab3bd
commit 6ed458260a

View File

@ -11,17 +11,14 @@ impl NetworkManager {
/// ///
/// Sending to a node requires determining a NetworkClass compatible contact method /// Sending to a node requires determining a NetworkClass compatible contact method
/// between the source and destination node /// between the source and destination node
pub(crate) fn send_data( pub(crate) async fn send_data(
&self, &self,
destination_node_ref: NodeRef, destination_node_ref: NodeRef,
data: Vec<u8>, data: Vec<u8>,
) -> SendPinBoxFuture<EyreResult<NetworkResult<SendDataMethod>>> { ) -> EyreResult<NetworkResult<SendDataMethod>> {
let this = self.clone();
Box::pin(
async move {
// First try to send data to the last flow we've seen this peer on // First try to send data to the last flow we've seen this peer on
let data = if let Some(flow) = destination_node_ref.last_flow() { let data = if let Some(flow) = destination_node_ref.last_flow() {
match this match self
.net() .net()
.send_data_to_existing_flow(flow, data) .send_data_to_existing_flow(flow, data)
.await? .await?
@ -51,7 +48,19 @@ impl NetworkManager {
// No existing connection was found or usable, so we proceed to see how to make a new one // No existing connection was found or usable, so we proceed to see how to make a new one
// Get the best way to contact this node // Get the best way to contact this node
let possibly_relayed_contact_method = this.get_node_contact_method(destination_node_ref.clone())?; let possibly_relayed_contact_method = self.get_node_contact_method(destination_node_ref.clone())?;
self.try_possibly_relayed_contact_method(possibly_relayed_contact_method, destination_node_ref, data).await
}
pub(crate) fn try_possibly_relayed_contact_method(&self,
possibly_relayed_contact_method: NodeContactMethod,
destination_node_ref: NodeRef,
data: Vec<u8>,
) -> SendPinBoxFuture<EyreResult<NetworkResult<SendDataMethod>>> {
let this = self.clone();
Box::pin(
async move {
// If we need to relay, do it // If we need to relay, do it
let (contact_method, target_node_ref, opt_relayed_contact_method) = match possibly_relayed_contact_method.clone() { let (contact_method, target_node_ref, opt_relayed_contact_method) = match possibly_relayed_contact_method.clone() {
@ -96,16 +105,28 @@ impl NetworkManager {
) )
} }
NodeContactMethod::SignalReverse(relay_nr, target_node_ref) => { NodeContactMethod::SignalReverse(relay_nr, target_node_ref) => {
network_result_try!( let nres =
this.send_data_ncm_signal_reverse(relay_nr, target_node_ref, data) this.send_data_ncm_signal_reverse(relay_nr.clone(), target_node_ref.clone(), data.clone())
.await? .await?;
) if matches!(nres, NetworkResult::Timeout) {
// Failed to holepunch, fallback to inbound relay
log_network_result!(debug "Reverse connection failed to {}, falling back to inbound relay via {}", target_node_ref, relay_nr);
network_result_try!(this.try_possibly_relayed_contact_method(NodeContactMethod::InboundRelay(relay_nr), destination_node_ref, data).await?)
} else {
network_result_try!(nres)
}
} }
NodeContactMethod::SignalHolePunch(relay_nr, target_node_ref) => { NodeContactMethod::SignalHolePunch(relay_nr, target_node_ref) => {
network_result_try!( let nres =
this.send_data_ncm_signal_hole_punch(relay_nr, target_node_ref, data) this.send_data_ncm_signal_hole_punch(relay_nr.clone(), target_node_ref.clone(), data.clone())
.await? .await?;
) if matches!(nres, NetworkResult::Timeout) {
// Failed to holepunch, fallback to inbound relay
log_network_result!(debug "Hole punch failed to {}, falling back to inbound relay via {}", target_node_ref, relay_nr);
network_result_try!(this.try_possibly_relayed_contact_method(NodeContactMethod::InboundRelay(relay_nr), destination_node_ref, data).await?)
} else {
network_result_try!(nres)
}
} }
NodeContactMethod::Existing => { NodeContactMethod::Existing => {
network_result_try!( network_result_try!(