减少注册和探测nat的频率

This commit is contained in:
lubeilin
2023-09-24 14:53:37 +08:00
parent 1cfb188845
commit 056036c4d2
3 changed files with 16 additions and 3 deletions
+1 -1
View File
@@ -664,7 +664,7 @@ impl ChannelDataHandler {
service_packet::Protocol::RegistrationResponse => {
let response = RegistrationResponse::parse_from_bytes(net_packet.payload())?;
{
if self.nat_test.can_update() {
let context = context.clone();
let nat_test = self.nat_test.clone();
std::thread::spawn(move || {
+1 -1
View File
@@ -229,7 +229,7 @@ impl Register {
}
pub fn fast_register(&self, ip: Ipv4Addr) -> crate::Result<()> {
let last = self.time.load();
if last.elapsed() < Duration::from_secs(2)
if last.elapsed() < Duration::from_secs(3)
|| self.time.compare_exchange(last, Instant::now()).is_err()
{
//短时间不重复注册
+14 -1
View File
@@ -1,7 +1,9 @@
use crossbeam_utils::atomic::AtomicCell;
use std::io;
use std::net::UdpSocket;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
use std::sync::Arc;
use std::time::{Duration, Instant};
use parking_lot::Mutex;
@@ -54,6 +56,7 @@ pub fn local_ipv6_addr(port: u16) -> SocketAddrV6 {
pub struct NatTest {
stun_server: Vec<String>,
info: Arc<Mutex<NatInfo>>,
time: Arc<AtomicCell<Instant>>,
}
impl From<NatType> for PunchNatType {
@@ -93,7 +96,16 @@ impl NatTest {
NatType::Cone,
);
let info = Arc::new(Mutex::new(nat_info));
NatTest { stun_server, info }
NatTest {
stun_server,
info,
time: Arc::new(AtomicCell::new(Instant::now())),
}
}
pub fn can_update(&self) -> bool {
let last = self.time.load();
last.elapsed() > Duration::from_secs(10)
&& self.time.compare_exchange(last, Instant::now()).is_ok()
}
pub fn nat_info(&self) -> NatInfo {
self.info.lock().clone()
@@ -128,6 +140,7 @@ impl NatTest {
ipv6_addr,
)
.await;
log::info!("探测nat类型={:?}", info);
*self.info.lock() = info.clone();
info
}