log noise, chunk size 32MB
This commit is contained in:
@@ -563,10 +563,12 @@ pub enum PeerCommand {
|
||||
}
|
||||
|
||||
async fn initial_peer_alive_check(conn: &mut Connection) -> bool {
|
||||
let remote_addr = conn.remote_addr().ok();
|
||||
|
||||
let stream = match conn.open_bidirectional_stream().await {
|
||||
Ok(stream) => stream,
|
||||
Err(e) => {
|
||||
log::error!("failed to open stream: {e}");
|
||||
log::error!("{remote_addr:?} failed to open stream: {e}");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -575,7 +577,7 @@ async fn initial_peer_alive_check(conn: &mut Connection) -> bool {
|
||||
|
||||
// send ping
|
||||
if let Err(e) = tx.send(Request::Ping.encode()).await {
|
||||
log::error!("failed to send ping to peer: {e}");
|
||||
log::error!("{remote_addr:?} failed to send ping to peer: {e}");
|
||||
return false;
|
||||
}
|
||||
let _ = tx.close().await;
|
||||
@@ -585,11 +587,11 @@ async fn initial_peer_alive_check(conn: &mut Connection) -> bool {
|
||||
let response = Response::decode(response);
|
||||
match response {
|
||||
Response::Pong => {
|
||||
log::info!("peer is alive");
|
||||
log::trace!("{remote_addr:?} peer is alive");
|
||||
return true;
|
||||
}
|
||||
_ => {
|
||||
log::error!("peer sent invalid response to ping: {response:?}");
|
||||
log::error!("{remote_addr:?} peer sent invalid response to ping: {response:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -597,7 +599,7 @@ async fn initial_peer_alive_check(conn: &mut Connection) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
const CHUNK_SIZE: u64 = 512 * 1024;
|
||||
const CHUNK_SIZE: u64 = 32 * 1024 * 1024;
|
||||
const MAX_RETRY_COUNT: usize = 3;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -1644,8 +1646,8 @@ async fn handle_peer_stream(
|
||||
offset,
|
||||
length,
|
||||
} => {
|
||||
log::info!(
|
||||
"Received GetGameFileChunk request for {relative_path} from peer"
|
||||
log::trace!(
|
||||
"{remote_addr:?} received GetGameFileChunk request for {relative_path} (offset {offset}, length {length})"
|
||||
);
|
||||
|
||||
let maybe_game_dir = ctx.game_dir.read().await.clone();
|
||||
@@ -2103,3 +2105,43 @@ async fn get_game_file_descriptions(
|
||||
|
||||
Ok(file_descriptions)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn loopback_addr(port: u16) -> SocketAddr {
|
||||
SocketAddr::from(([127, 0, 0, 1], port))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_peer_plans_handles_partial_final_chunk() {
|
||||
let peers = vec![loopback_addr(12000), loopback_addr(12001)];
|
||||
let file_size = CHUNK_SIZE * 2 + CHUNK_SIZE / 4;
|
||||
let file_descs = vec![GameFileDescription {
|
||||
game_id: "test".to_string(),
|
||||
relative_path: "game/file.dat".to_string(),
|
||||
is_dir: false,
|
||||
size: file_size,
|
||||
}];
|
||||
|
||||
let plans = build_peer_plans(&peers, &file_descs);
|
||||
let mut chunks: Vec<_> = plans.values().flat_map(|plan| plan.chunks.iter()).collect();
|
||||
|
||||
assert_eq!(chunks.len(), 3, "expected three chunks for 2.25 blocks");
|
||||
|
||||
chunks.sort_by_key(|chunk| chunk.offset);
|
||||
let last_chunk = chunks.last().expect("last chunk exists");
|
||||
|
||||
assert_eq!(last_chunk.offset, CHUNK_SIZE * 2);
|
||||
assert_eq!(last_chunk.length, file_size - last_chunk.offset);
|
||||
assert_eq!(last_chunk.length, CHUNK_SIZE / 4);
|
||||
assert_eq!(
|
||||
last_chunk.offset + last_chunk.length,
|
||||
file_size,
|
||||
"last chunk should finish the file"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user