diff --git a/src/relay/udprelay/socks5_local.rs b/src/relay/udprelay/socks5_local.rs index d9c5d35b..508985f0 100644 --- a/src/relay/udprelay/socks5_local.rs +++ b/src/relay/udprelay/socks5_local.rs @@ -354,9 +354,6 @@ async fn listen(context: SharedContext, l: UdpSocket) -> io::Result<()> { continue; } - // Pick a server - let svr_cfg = balancer.pick_server(); - // Check or (re)create an association let mut assoc = { // Locks the whole association map @@ -365,11 +362,16 @@ async fn listen(context: SharedContext, l: UdpSocket) -> io::Result<()> { // Get or create an association let assoc = match assoc_map.entry(src.to_string()) { Entry::Occupied(oc) => oc.into_mut(), - Entry::Vacant(vc) => vc.insert( - UdpAssociation::associate(context.clone(), svr_cfg.clone(), src, tx.clone()) - .await - .expect("Failed to create udp association"), - ), + Entry::Vacant(vc) => { + // Pick a server + let svr_cfg = balancer.pick_server(); + + vc.insert( + UdpAssociation::associate(context.clone(), svr_cfg.clone(), src, tx.clone()) + .await + .expect("Failed to create udp association"), + ) + } }; // Clone the handle and release the lock. diff --git a/src/relay/udprelay/tunnel_local.rs b/src/relay/udprelay/tunnel_local.rs index fbb3a3bf..a79e264f 100644 --- a/src/relay/udprelay/tunnel_local.rs +++ b/src/relay/udprelay/tunnel_local.rs @@ -326,9 +326,6 @@ async fn listen(context: SharedContext, l: UdpSocket) -> io::Result<()> { continue; } - // Pick a server - let svr_cfg = balancer.pick_server(); - // Check or (re)create an association let mut assoc = { // Locks the whole association map @@ -337,11 +334,16 @@ async fn listen(context: SharedContext, l: UdpSocket) -> io::Result<()> { // Get or create an association let assoc = match assoc_map.entry(src.to_string()) { Entry::Occupied(oc) => oc.into_mut(), - Entry::Vacant(vc) => vc.insert( - UdpAssociation::associate(context.clone(), svr_cfg.clone(), src, tx.clone()) - .await - .expect("Failed to create udp association"), - ), + Entry::Vacant(vc) => { + // Pick a server + let svr_cfg = balancer.pick_server(); + + vc.insert( + UdpAssociation::associate(context.clone(), svr_cfg.clone(), src, tx.clone()) + .await + .expect("Failed to create udp association"), + ) + } }; // Clone the handle and release the lock.