optimize bandwidth usage (#24)

1. stable stun test result
2. stable report peers result
3. do not send same packet to rip peer
This commit is contained in:
Sijie.Sun
2024-03-02 22:29:31 +08:00
committed by GitHub
parent 7918031d8b
commit 9261d0d32d
4 changed files with 51 additions and 13 deletions
+10 -1
View File
@@ -449,7 +449,16 @@ impl StunInfoCollector {
self.tasks.spawn(async move {
loop {
let detector = UdpNatTypeDetector::new(stun_servers.read().await.clone());
let ret = detector.get_udp_nat_type(0).await;
let old_nat_type = udp_nat_type.load().0;
let mut ret = NatType::Unknown;
for _ in 1..5 {
// if nat type degrade, sleep and retry. so result can be relatively stable.
ret = detector.get_udp_nat_type(0).await;
if ret == NatType::Unknown || ret <= old_nat_type {
break;
}
tokio::time::sleep(Duration::from_secs(5)).await;
}
udp_nat_type.store((ret, std::time::Instant::now()));
let sleep_sec = match ret {