Check the chunk before uploading

This commit is contained in:
Igor Katson 2021-06-28 22:21:21 +01:00
parent df282ae9d8
commit b26af687b7
4 changed files with 22 additions and 9 deletions

View file

@ -10,4 +10,6 @@ build-release:
cargo build --release
install: build-release
$(MAKE) build-release
$(MAKE) sign-release
cp target/release/rqbit "$(HOME)/bin/"

View file

@ -116,6 +116,13 @@ impl ChunkTracker {
.unwrap()
}
pub fn is_chunk_ready_to_upload(&self, chunk: &ChunkInfo) -> bool {
self.have
.get(chunk.piece_index.get() as usize)
.map(|b| *b)
.unwrap_or(false)
}
// return true if the whole piece is marked downloaded
pub fn mark_chunk_downloaded<ByteBuf>(
&mut self,

View file

@ -263,18 +263,22 @@ impl PeerConnection {
}
};
let tx = self
.state
.locked
.read()
.peers
.clone_tx(peer_handle)
.ok_or_else(|| {
let tx = {
let g = self.state.locked.read();
if !g.chunks.is_chunk_ready_to_upload(&chunk_info) {
anyhow::bail!(
"got request for a chunk that is not ready to upload. chunk {:?}",
&chunk_info
);
}
g.peers.clone_tx(peer_handle).ok_or_else(|| {
anyhow::anyhow!(
"peer {} died, dropping chunk that it requested",
peer_handle
)
})?;
})?
};
// TODO: this is not super efficient as it does copying multiple times.
// Theoretically, this could be done in the sending code, so that it reads straight into

View file

@ -12,7 +12,7 @@ use std::{
use anyhow::Context;
use futures::{stream::FuturesUnordered, StreamExt};
use log::{debug, error, info, warn};
use log::{debug, info, warn};
use parking_lot::{Mutex, RwLock};
use reqwest::Url;
use size_format::SizeFormatterBinary as SF;