Skip to content

Commit

Permalink
refactor: [torrust#682] handle UDP Tracker timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
hungfnt committed May 3, 2024
1 parent f64e8fb commit db50d96
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/console/clients/udp/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub enum ClientError {
NotConnected,
#[error("Unexpected response while connecting the the remote server.")]
UnexpectedConnectionResponse,
#[error("Connection timeout.")]
ConnectionTimeout,
}

/// A UDP Tracker client to make test requests (checks).
Expand Down
11 changes: 6 additions & 5 deletions src/shared/bit_torrent/tracker/udp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, bail, Context, Result};
use aquatic_udp_protocol::{ConnectRequest, Request, Response, TransactionId};
use log::debug;
use tokio::net::UdpSocket;
use tokio::time;

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

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

match time::timeout(self.timeout, self.socket.send(bytes)).await {
Ok(send_result) => match send_result {
Ok(size) => Ok(size),
Err(e) => Err(anyhow!("IO error during send: {e:?}")),
},
Err(e) => Err(anyhow!("Send operation timed out: {e:?}")),
Err(_) => bail!(ClientError::ConnectionTimeout),
}
}

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

let size_result = match time::timeout(self.timeout, self.socket.recv(bytes)).await {
Ok(recv_result) => match recv_result {
Ok(size) => Ok(size),
Err(e) => Err(anyhow!("IO error during send: {e:?}")),
},
Err(e) => Err(anyhow!("Receive operation timed out: {e:?}")),
Err(_) => bail!(ClientError::ConnectionTimeout),
};

if size_result.is_ok() {
Expand Down

0 comments on commit db50d96

Please sign in to comment.