Writing still broken

This commit is contained in:
Igor Katson 2021-06-27 12:51:19 +01:00
parent 3f068883c2
commit 0ac1116aba

View file

@ -812,15 +812,15 @@ impl TorrentManager {
}) })
} }
fn try_steal_piece(&self) -> Option<ValidPieceIndex> { fn try_steal_piece(&self, handle: PeerHandle) -> Option<ValidPieceIndex> {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
use rand::seq::IteratorRandom; use rand::seq::IteratorRandom;
self.inner let g = self.inner.locked.read();
.locked let pl = g.peers.get_live(handle)?;
.read() g.peers
.peers
.inflight_pieces .inflight_pieces
.iter() .iter()
.filter(|p| !pl.inflight_requests.iter().any(|req| req.piece == **p))
.choose(&mut rng) .choose(&mut rng)
.copied() .copied()
} }
@ -859,9 +859,7 @@ impl TorrentManager {
return Ok(()); return Ok(());
} }
if let Some(piece) = self.try_steal_piece() { if let Some(piece) = self.try_steal_piece(handle) {
// TODO: ok, so there's a problem here. It can keep looping here requesting the same
// pieces over and over again.
info!("{}: stole a piece {}", handle, piece); info!("{}: stole a piece {}", handle, piece);
(piece, true) (piece, true)
} else { } else {
@ -895,8 +893,10 @@ impl TorrentManager {
.inflight_requests .inflight_requests
.insert(InflightRequest::from(&chunk)) .insert(InflightRequest::from(&chunk))
{ {
// we already requested this chunk. warn!(
// this should not happen, but due do a bug above it actually does. "{}: probably a bug, we already requested {:?}",
handle, chunk
);
continue; continue;
} }