Skip to content

Commit db50d96

Browse files
committed
refactor: [#682] handle UDP Tracker timeouts
1 parent f64e8fb commit db50d96

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/console/clients/udp/checker.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub enum ClientError {
2020
NotConnected,
2121
#[error("Unexpected response while connecting the the remote server.")]
2222
UnexpectedConnectionResponse,
23+
#[error("Connection timeout.")]
24+
ConnectionTimeout,
2325
}
2426

2527
/// A UDP Tracker client to make test requests (checks).

src/shared/bit_torrent/tracker/udp/client.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ use std::net::SocketAddr;
44
use std::sync::Arc;
55
use std::time::Duration;
66

7-
use anyhow::{anyhow, Context, Result};
7+
use anyhow::{anyhow, bail, Context, Result};
88
use aquatic_udp_protocol::{ConnectRequest, Request, Response, TransactionId};
99
use log::debug;
1010
use tokio::net::UdpSocket;
1111
use tokio::time;
1212

13+
use crate::console::clients::udp::checker::ClientError;
1314
use crate::shared::bit_torrent::tracker::udp::{source_address, MAX_PACKET_SIZE};
1415

1516
/// Default timeout for sending and receiving packets. And waiting for sockets
@@ -78,15 +79,15 @@ impl UdpClient {
7879
Err(e) => return Err(anyhow!("IO error waiting for the socket to become readable: {e:?}")),
7980
};
8081
}
81-
Err(e) => return Err(anyhow!("Timeout waiting for the socket to become readable: {e:?}")),
82+
Err(_) => bail!(ClientError::ConnectionTimeout),
8283
};
8384

8485
match time::timeout(self.timeout, self.socket.send(bytes)).await {
8586
Ok(send_result) => match send_result {
8687
Ok(size) => Ok(size),
8788
Err(e) => Err(anyhow!("IO error during send: {e:?}")),
8889
},
89-
Err(e) => Err(anyhow!("Send operation timed out: {e:?}")),
90+
Err(_) => bail!(ClientError::ConnectionTimeout),
9091
}
9192
}
9293

@@ -109,15 +110,15 @@ impl UdpClient {
109110
Err(e) => return Err(anyhow!("IO error waiting for the socket to become readable: {e:?}")),
110111
};
111112
}
112-
Err(e) => return Err(anyhow!("Timeout waiting for the socket to become readable: {e:?}")),
113+
Err(_) => bail!(ClientError::ConnectionTimeout),
113114
};
114115

115116
let size_result = match time::timeout(self.timeout, self.socket.recv(bytes)).await {
116117
Ok(recv_result) => match recv_result {
117118
Ok(size) => Ok(size),
118119
Err(e) => Err(anyhow!("IO error during send: {e:?}")),
119120
},
120-
Err(e) => Err(anyhow!("Receive operation timed out: {e:?}")),
121+
Err(_) => bail!(ClientError::ConnectionTimeout),
121122
};
122123

123124
if size_result.is_ok() {

0 commit comments

Comments
 (0)