Send cancellations on steal

This commit is contained in:
Igor Katson 2024-04-07 15:33:07 +04:00
parent 841c84ff25
commit 6a23f311e1
4 changed files with 62 additions and 44 deletions

View file

@ -3,8 +3,11 @@ use std::net::SocketAddr;
use anyhow::Context;
use backoff::backoff::Backoff;
use dashmap::DashMap;
use librqbit_core::lengths::ValidPieceIndex;
use peer_binary_protocol::{Message, Request};
use crate::{
peer_connection::WriterRequest,
torrent_state::utils::{atomic_inc, TimedExistence},
type_aliases::{PeerHandle, BF},
};
@ -109,4 +112,25 @@ impl PeerStates {
})?;
Some(prev)
}
pub(crate) fn send_cancellations(&self, from_peer: SocketAddr, stolen_idx: ValidPieceIndex) {
self.with_live_mut(from_peer, "send_cancellations", |live| {
let to_remove = live
.inflight_requests
.iter()
.filter(|r| r.piece_index == stolen_idx)
.copied()
.collect::<Vec<_>>();
for req in to_remove {
let _ = live
.tx
.send(WriterRequest::Message(Message::Cancel(Request {
index: stolen_idx.get(),
begin: req.offset,
length: req.size,
})));
live.inflight_requests.remove(&req);
}
});
}
}