Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to restarting of dead peers #239

Merged
merged 6 commits into from
Sep 22, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions crates/librqbit/src/torrent_state/live/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ use super::{

#[derive(Debug)]
struct InflightPiece {
peer: PeerHandle,
peer: PeerHandle,
started: Instant,
}

Expand Down Expand Up @@ -399,6 +399,7 @@ impl TorrentStateLive {
// TODO: bump counters for incoming
let handler = PeerHandler {
addr: checked_peer.addr,
incoming: true,
on_bitfield_notify: Default::default(),
unchoke_notify: Default::default(),
locked: RwLock::new(PeerHandlerLocked { i_am_choked: true }),
Expand Down Expand Up @@ -463,6 +464,7 @@ impl TorrentStateLive {

let handler = PeerHandler {
addr,
incoming: false,
on_bitfield_notify: Default::default(),
unchoke_notify: Default::default(),
locked: RwLock::new(PeerHandlerLocked { i_am_choked: true }),
Expand Down Expand Up @@ -818,7 +820,7 @@ struct PeerHandler {
requests_sem: Semaphore,

addr: SocketAddr,

incoming: bool,
tx: PeerTx,

first_message_received: AtomicBool,
Expand Down Expand Up @@ -999,7 +1001,7 @@ impl PeerHandler {
}
};

let _error = match error {
let error = match error {
Some(e) => e,
None => {
trace!("peer died without errors, not re-queueing");
Expand All @@ -1018,17 +1020,27 @@ impl PeerHandler {

pe.value_mut().state.set(PeerState::Dead, &pstats);

if self.incoming {
// do not retry incoming peers
return Ok(());
}

let backoff = pe.value_mut().stats.backoff.next_backoff();

// Prevent deadlocks.
drop(pe);

if let Some(dur) = backoff {
debug!(
"{} peer {} died - {error} and will retry in {dur:?}",
if self.incoming { "incoming" } else { "outgoing" },
self.addr
);
self.state.clone().spawn(
error_span!(
parent: self.state.torrent.span.clone(),
"wait_for_peer",
peer = handle.to_string(),
peer = handle.to_string(),
duration = format!("{dur:?}")
),
async move {
Expand Down
Loading