more parallel

This commit is contained in:
Christien Rioux 2023-09-05 19:27:16 -04:00
parent dfcdcf2364
commit b325c82b9a

View File

@ -378,26 +378,30 @@ impl DiscoveryContext {
async fn protocol_process_no_nat( async fn protocol_process_no_nat(
&self, &self,
unord: &mut FuturesUnordered<SendPinBoxFuture<Option<DetectedDialInfo>>>, unord: &mut FuturesUnordered<SendPinBoxFuture<Option<DetectedDialInfo>>>,
) -> DetectedDialInfo { ) {
let external_1 = self.inner.lock().external_1.as_ref().unwrap().clone(); let external_1 = self.inner.lock().external_1.as_ref().unwrap().clone();
let this = self.clone();
let do_no_nat_fut: SendPinBoxFuture<Option<DetectedDialInfo>> = Box::pin(async move {
// Do a validate_dial_info on the external address from a redirected node // Do a validate_dial_info on the external address from a redirected node
if self if this
.validate_dial_info(external_1.node.clone(), external_1.dial_info.clone(), true) .validate_dial_info(external_1.node.clone(), external_1.dial_info.clone(), true)
.await .await
{ {
// Add public dial info with Direct dialinfo class // Add public dial info with Direct dialinfo class
DetectedDialInfo::Detected(DialInfoDetail { Some(DetectedDialInfo::Detected(DialInfoDetail {
dial_info: external_1.dial_info.clone(), dial_info: external_1.dial_info.clone(),
class: DialInfoClass::Direct, class: DialInfoClass::Direct,
}) }))
} else { } else {
// Add public dial info with Blocked dialinfo class // Add public dial info with Blocked dialinfo class
DetectedDialInfo::Detected(DialInfoDetail { Some(DetectedDialInfo::Detected(DialInfoDetail {
dial_info: external_1.dial_info.clone(), dial_info: external_1.dial_info.clone(),
class: DialInfoClass::Blocked, class: DialInfoClass::Blocked,
}) }))
} }
});
unord.push(do_no_nat_fut);
} }
// If we know we are behind NAT check what kind // If we know we are behind NAT check what kind
@ -405,20 +409,24 @@ impl DiscoveryContext {
async fn protocol_process_nat( async fn protocol_process_nat(
&self, &self,
unord: &mut FuturesUnordered<SendPinBoxFuture<Option<DetectedDialInfo>>>, unord: &mut FuturesUnordered<SendPinBoxFuture<Option<DetectedDialInfo>>>,
) -> Option<DetectedDialInfo> { ) {
// Get the external dial info for our use here // Get the external dial info for our use here
let external_1 = self.inner.lock().external_1.as_ref().unwrap().clone(); let (external_1, external_2) = {
let external_2 = self.inner.lock().external_2.as_ref().unwrap().clone(); let inner = self.inner.lock();
(
inner.external_1.as_ref().unwrap().clone(),
inner.external_2.as_ref().unwrap().clone(),
)
};
// If we have two different external addresses, then this is a symmetric NAT // If we have two different external addresses, then this is a symmetric NAT
if external_2.address != external_1.address { if external_2.address != external_1.address {
// No more retries let do_symmetric_nat_fut: SendPinBoxFuture<Option<DetectedDialInfo>> =
return Some(DetectedDialInfo::SymmetricNAT); Box::pin(async move { Some(DetectedDialInfo::SymmetricNAT) });
unord.push(do_symmetric_nat_fut);
return;
} }
// Do these detections in parallel, but with ordering preference
let mut ord = FuturesOrdered::new();
// Manual Mapping Detection // Manual Mapping Detection
/////////// ///////////
let this = self.clone(); let this = self.clone();
@ -454,18 +462,33 @@ impl DiscoveryContext {
None None
}); });
ord.push_back(do_manual_map_fut); unord.push(do_manual_map_fut);
} }
} }
// NAT Detection
///////////
// Full Cone NAT Detection // Full Cone NAT Detection
/////////// ///////////
let this = self.clone(); let this = self.clone();
let do_nat_detect_fut: SendPinBoxFuture<Option<DetectedDialInfo>> = Box::pin(async move {
let mut retry_count = {
let c = this.unlocked_inner.net.config.get();
c.network.restricted_nat_retries
};
// Loop for restricted NAT retries
loop {
let mut ord = FuturesOrdered::new();
let c_this = this.clone();
let c_external_1 = external_1.clone(); let c_external_1 = external_1.clone();
let do_full_cone_fut: SendPinBoxFuture<Option<DetectedDialInfo>> = Box::pin(async move { let do_full_cone_fut: SendPinBoxFuture<Option<DetectedDialInfo>> =
Box::pin(async move {
// Let's see what kind of NAT we have // Let's see what kind of NAT we have
// Does a redirected dial info validation from a different address and a random port find us? // Does a redirected dial info validation from a different address and a random port find us?
if this if c_this
.validate_dial_info( .validate_dial_info(
c_external_1.node.clone(), c_external_1.node.clone(),
c_external_1.dial_info.clone(), c_external_1.dial_info.clone(),
@ -485,34 +508,65 @@ impl DiscoveryContext {
}); });
ord.push_back(do_full_cone_fut); ord.push_back(do_full_cone_fut);
// Run detections in parallel and take the first one, ordered by preference, that returns a result let c_this = this.clone();
while let Some(res) = ord.next().await { let c_external_1 = external_1.clone();
if let Some(ddi) = res { let c_external_2 = external_2.clone();
return Some(ddi); let do_restricted_cone_fut: SendPinBoxFuture<Option<DetectedDialInfo>> =
} Box::pin(async move {
}
// We are restricted, determine what kind of restriction // We are restricted, determine what kind of restriction
// If we're going to end up as a restricted NAT of some sort // If we're going to end up as a restricted NAT of some sort
// Address is the same, so it's address or port restricted // Address is the same, so it's address or port restricted
// Do a validate_dial_info on the external address from a random port // Do a validate_dial_info on the external address from a random port
if self if c_this
.validate_dial_info(external_2.node.clone(), external_1.dial_info.clone(), false) .validate_dial_info(
c_external_2.node.clone(),
c_external_1.dial_info.clone(),
false,
)
.await .await
{ {
// Got a reply from a non-default port, which means we're only address restricted // Got a reply from a non-default port, which means we're only address restricted
return Some(DetectedDialInfo::Detected(DialInfoDetail { return Some(DetectedDialInfo::Detected(DialInfoDetail {
dial_info: external_1.dial_info.clone(), dial_info: c_external_1.dial_info.clone(),
class: DialInfoClass::AddressRestrictedNAT, class: DialInfoClass::AddressRestrictedNAT,
})); }));
} }
// Didn't get a reply from a non-default port, which means we are also port restricted // Didn't get a reply from a non-default port, which means we are also port restricted
Some(DetectedDialInfo::Detected(DialInfoDetail { Some(DetectedDialInfo::Detected(DialInfoDetail {
dial_info: external_1.dial_info.clone(), dial_info: c_external_1.dial_info.clone(),
class: DialInfoClass::PortRestrictedNAT, class: DialInfoClass::PortRestrictedNAT,
})) }))
});
ord.push_back(do_restricted_cone_fut);
// Return the first result we get
let mut some_ddi = None;
while let Some(res) = ord.next().await {
if let Some(ddi) = res {
some_ddi = Some(ddi);
break;
}
}
if let Some(ddi) = some_ddi {
if let DetectedDialInfo::Detected(did) = &ddi {
// If we got something better than restricted NAT or we're done retrying
if did.class < DialInfoClass::AddressRestrictedNAT || retry_count == 0 {
return Some(ddi);
}
}
}
if retry_count == 0 {
break;
}
retry_count -= 1;
}
None
});
unord.push(do_nat_detect_fut);
} }
/// Add discovery futures to an unordered set that may detect dialinfo when they complete /// Add discovery futures to an unordered set that may detect dialinfo when they complete
@ -520,9 +574,9 @@ impl DiscoveryContext {
&self, &self,
unord: &mut FuturesUnordered<SendPinBoxFuture<Option<DetectedDialInfo>>>, unord: &mut FuturesUnordered<SendPinBoxFuture<Option<DetectedDialInfo>>>,
) { ) {
let (mut retry_count, enable_upnp) = { let enable_upnp = {
let c = self.unlocked_inner.net.config.get(); let c = self.unlocked_inner.net.config.get();
(c.network.restricted_nat_retries, c.network.upnp) c.network.upnp
}; };
// Do this right away because it's fast and every detection is going to need it // Do this right away because it's fast and every detection is going to need it
@ -562,28 +616,6 @@ impl DiscoveryContext {
self.protocol_process_no_nat(unord).await; self.protocol_process_no_nat(unord).await;
} else { } else {
self.protocol_process_nat(unord).await; self.protocol_process_nat(unord).await;
// // Loop for restricted NAT retries
// let this = self.clone();
// let do_nat_fut: SendPinBoxFuture<Option<DetectedDialInfo>> = Box::pin(async move {
// loop {
// // There is -some NAT-
// if let Some(ddi) = this.protocol_process_nat().await {
// if let DetectedDialInfo::Detected(did) = &ddi {
// // If we got something better than restricted NAT or we're done retrying
// if did.class < DialInfoClass::AddressRestrictedNAT || retry_count == 0 {
// return Some(ddi);
// }
// }
// }
// if retry_count == 0 {
// break;
// }
// retry_count -= 1;
// }
// None
// });
// unord.push(do_nat_fut);
} }
} }
} }