Skip to content

Commit a5e2baf

Browse files
committed
refactor: extract method
1 parent b4b4515 commit a5e2baf

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/servers/udp/server.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use ringbuf::StaticRb;
3636
use tokio::select;
3737
use tokio::sync::oneshot;
3838
use tokio::task::{AbortHandle, JoinHandle};
39+
use url::Url;
3940

4041
use super::UdpRequest;
4142
use crate::bootstrap::jobs::Started;
@@ -241,6 +242,9 @@ struct BoundSocket {
241242

242243
impl BoundSocket {
243244
async fn new(addr: SocketAddr) -> Result<Self, Box<std::io::Error>> {
245+
let bind_addr = format!("udp://{addr}");
246+
tracing::debug!(target: UDP_TRACKER_LOG_TARGET, bind_addr, "UdpSocket::new (binding)");
247+
244248
let socket = tokio::net::UdpSocket::bind(addr).await;
245249

246250
let socket = match socket {
@@ -259,6 +263,10 @@ impl BoundSocket {
259263
fn local_addr(&self) -> SocketAddr {
260264
self.socket.local_addr().expect("it should get local address")
261265
}
266+
267+
fn url(&self) -> Url {
268+
Url::parse(&format!("udp://{}", self.local_addr())).expect("UDP socket address should be valid")
269+
}
262270
}
263271

264272
impl Deref for BoundSocket {
@@ -363,7 +371,7 @@ impl Udp {
363371
};
364372

365373
let address = bound_socket.local_addr();
366-
let local_udp_url = format!("udp://{address}");
374+
let local_udp_url = bound_socket.url().to_string();
367375

368376
tracing::info!(target: UDP_TRACKER_LOG_TARGET, "{STARTED_ON}: {local_udp_url}");
369377

0 commit comments

Comments
 (0)