rqbit/crates/librqbit/src/peer_state.rs

268 lines
7.4 KiB
Rust
Raw Normal View History

2023-11-20 00:55:31 +00:00
use std::sync::atomic::{AtomicU32, Ordering};
2023-11-17 23:30:07 +00:00
use std::time::Duration;
2021-07-04 14:38:44 +01:00
use std::{collections::HashSet, sync::Arc};
2021-06-28 14:23:28 +01:00
2023-11-18 10:08:12 +00:00
use anyhow::Context;
2023-11-17 23:30:07 +00:00
use backoff::{ExponentialBackoff, ExponentialBackoffBuilder};
2021-07-12 21:59:08 +01:00
use librqbit_core::id20::Id20;
2021-07-03 19:10:59 +01:00
use librqbit_core::lengths::{ChunkInfo, ValidPieceIndex};
2023-11-20 00:55:31 +00:00
use serde::Serialize;
2023-11-18 00:20:49 +00:00
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
2021-06-28 14:23:28 +01:00
use tokio::sync::{Notify, Semaphore};
use crate::peer_connection::WriterRequest;
2021-07-03 19:10:59 +01:00
use crate::type_aliases::BF;
2021-06-30 00:00:44 +01:00
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct InflightRequest {
pub piece: ValidPieceIndex,
pub chunk: u32,
}
impl From<&ChunkInfo> for InflightRequest {
fn from(c: &ChunkInfo) -> Self {
Self {
piece: c.piece_index,
chunk: c.chunk_index,
}
}
}
2021-06-28 14:23:28 +01:00
// TODO: Arc can be removed probably, as UnboundedSender should be clone + it can be downgraded to weak.
2023-11-17 22:51:05 +00:00
pub type PeerRx = UnboundedReceiver<WriterRequest>;
2023-11-18 10:08:12 +00:00
pub type PeerTx = UnboundedSender<WriterRequest>;
pub trait SendMany {
fn send_many(&self, requests: impl IntoIterator<Item = WriterRequest>) -> anyhow::Result<()>;
}
impl SendMany for PeerTx {
fn send_many(&self, requests: impl IntoIterator<Item = WriterRequest>) -> anyhow::Result<()> {
requests
.into_iter()
.try_for_each(|r| self.send(r))
.context("peer dropped")
}
}
2023-11-17 23:30:07 +00:00
#[derive(Debug)]
2023-11-17 22:51:05 +00:00
pub struct PeerStats {
2023-11-17 23:30:07 +00:00
pub backoff: ExponentialBackoff,
}
impl Default for PeerStats {
fn default() -> Self {
Self {
backoff: ExponentialBackoffBuilder::new()
.with_initial_interval(Duration::from_secs(10))
.with_multiplier(6.)
.with_max_interval(Duration::from_secs(3600))
.with_max_elapsed_time(Some(Duration::from_secs(86400)))
.build(),
}
}
2023-11-17 22:51:05 +00:00
}
#[derive(Debug, Default)]
pub struct Peer {
2023-11-20 00:55:31 +00:00
pub state: PeerStateNoMut,
2023-11-17 22:51:05 +00:00
pub stats: PeerStats,
}
2023-11-20 00:55:31 +00:00
#[derive(Debug, Default, Serialize)]
pub struct AggregatePeerStatsAtomic {
pub queued: AtomicU32,
pub connecting: AtomicU32,
pub live: AtomicU32,
pub seen: AtomicU32,
pub dead: AtomicU32,
pub not_needed: AtomicU32,
}
pub fn atomic_inc(c: &AtomicU32) -> u32 {
c.fetch_add(1, Ordering::Relaxed)
}
pub fn atomic_dec(c: &AtomicU32) -> u32 {
c.fetch_sub(1, Ordering::Relaxed)
}
impl AggregatePeerStatsAtomic {
pub fn counter(&self, state: &PeerState) -> &AtomicU32 {
match state {
PeerState::Connecting(_) => &self.connecting,
PeerState::Live(_) => &self.live,
PeerState::Queued => &self.queued,
PeerState::Dead => &self.dead,
PeerState::NotNeeded => &self.not_needed,
}
}
pub fn inc(&self, state: &PeerState) {
atomic_inc(self.counter(state));
2023-11-20 00:55:31 +00:00
}
pub fn dec(&self, state: &PeerState) {
atomic_dec(self.counter(state));
2023-11-20 00:55:31 +00:00
}
pub fn incdec(&self, old: &PeerState, new: &PeerState) {
self.dec(old);
self.inc(new);
}
}
#[derive(Debug, Default)]
2021-06-28 14:23:28 +01:00
pub enum PeerState {
#[default]
2023-11-18 00:20:49 +00:00
// Will be tried to be connected as soon as possible.
2021-07-04 12:17:06 +01:00
Queued,
Connecting(PeerTx),
2021-06-28 14:23:28 +01:00
Live(LivePeerState),
2023-11-18 00:20:49 +00:00
// There was an error, and it's waiting for exponential backoff.
2023-11-17 23:30:07 +00:00
Dead,
2023-11-19 13:44:14 +00:00
// We don't need to do anything with the peer any longer.
2023-11-18 00:20:49 +00:00
// The peer has the full torrent, and we have the full torrent, so no need
// to keep talking to it.
2023-11-18 10:08:12 +00:00
NotNeeded,
2023-11-18 00:20:49 +00:00
}
2023-11-20 00:55:31 +00:00
impl std::fmt::Display for PeerState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}
2023-11-18 00:20:49 +00:00
impl PeerState {
2023-11-18 10:08:12 +00:00
pub fn name(&self) -> &'static str {
match self {
PeerState::Queued => "queued",
PeerState::Connecting(_) => "connecting",
PeerState::Live(_) => "live",
PeerState::Dead => "dead",
PeerState::NotNeeded => "not needed",
}
}
2023-11-20 00:55:31 +00:00
pub fn take_live_no_counters(self) -> Option<LivePeerState> {
match self {
PeerState::Live(l) => Some(l),
_ => None,
2023-11-18 00:20:49 +00:00
}
}
2023-11-20 00:55:31 +00:00
}
2023-11-18 00:20:49 +00:00
2023-11-20 00:55:31 +00:00
#[derive(Debug, Default)]
pub struct PeerStateNoMut(PeerState);
impl PeerStateNoMut {
pub fn get(&self) -> &PeerState {
&self.0
}
pub fn take(&mut self, counters: &AggregatePeerStatsAtomic) -> PeerState {
self.set(Default::default(), counters)
}
pub fn set(&mut self, new: PeerState, counters: &AggregatePeerStatsAtomic) -> PeerState {
counters.incdec(&self.0, &new);
std::mem::replace(&mut self.0, new)
2023-11-18 00:20:49 +00:00
}
2023-11-19 15:47:14 +00:00
pub fn get_live(&self) -> Option<&LivePeerState> {
2023-11-20 00:55:31 +00:00
match &self.0 {
2023-11-19 15:47:14 +00:00
PeerState::Live(l) => Some(l),
_ => None,
}
}
2023-11-18 00:20:49 +00:00
pub fn get_live_mut(&mut self) -> Option<&mut LivePeerState> {
2023-11-20 00:55:31 +00:00
match &mut self.0 {
2023-11-18 00:20:49 +00:00
PeerState::Live(l) => Some(l),
_ => None,
}
}
2023-11-20 00:55:31 +00:00
pub fn queued_to_connecting(&mut self, counters: &AggregatePeerStatsAtomic) -> Option<PeerRx> {
if let PeerState::Queued = &self.0 {
2023-11-18 00:20:49 +00:00
let (tx, rx) = unbounded_channel();
2023-11-20 00:55:31 +00:00
self.set(PeerState::Connecting(tx), counters);
2023-11-18 00:20:49 +00:00
Some(rx)
} else {
None
}
}
2023-11-20 00:55:31 +00:00
pub fn connecting_to_live(
&mut self,
peer_id: Id20,
counters: &AggregatePeerStatsAtomic,
) -> Option<&mut LivePeerState> {
if let PeerState::Connecting(_) = &self.0 {
let tx = match self.take(counters) {
PeerState::Connecting(tx) => tx,
_ => unreachable!(),
};
self.set(PeerState::Live(LivePeerState::new(peer_id, tx)), counters);
self.get_live_mut()
} else {
None
}
2023-11-18 00:20:49 +00:00
}
2023-11-20 00:55:31 +00:00
pub fn to_dead(&mut self, counters: &AggregatePeerStatsAtomic) -> PeerState {
self.set(PeerState::Dead, counters)
2023-11-18 00:20:49 +00:00
}
2023-11-20 00:55:31 +00:00
pub fn to_not_needed(&mut self, counters: &AggregatePeerStatsAtomic) -> PeerState {
self.set(PeerState::NotNeeded, counters)
2023-11-18 00:20:49 +00:00
}
2021-06-28 14:23:28 +01:00
}
2021-07-04 12:17:06 +01:00
#[derive(Debug)]
2021-06-28 14:23:28 +01:00
pub struct LivePeerState {
2021-07-12 21:59:08 +01:00
pub peer_id: Id20,
2021-06-28 14:23:28 +01:00
pub i_am_choked: bool,
pub peer_interested: bool,
2023-11-20 09:17:42 +00:00
// This is used to limit the number of chunk requests we send to a peer at a time.
2021-06-28 14:58:53 +01:00
pub requests_sem: Arc<Semaphore>,
2023-11-20 09:17:42 +00:00
// This is used to unpause processes after we were choked.
2021-06-28 14:23:28 +01:00
pub have_notify: Arc<Notify>,
2023-11-20 09:17:42 +00:00
// This is used to track the pieces the peer has.
pub bitfield: BF,
// This is used to only request a piece from a peer once when stealing from others.
// So that you don't steal then re-steal the same piece in a loop.
pub previously_requested_pieces: BF,
// When the peer sends us data this is used to track if we asked for it.
2021-06-28 14:23:28 +01:00
pub inflight_requests: HashSet<InflightRequest>,
2023-11-20 09:17:42 +00:00
// The main channel to send requests to peer.
pub tx: PeerTx,
2021-06-28 14:23:28 +01:00
}
2021-06-28 14:42:19 +01:00
impl LivePeerState {
pub fn new(peer_id: Id20, tx: PeerTx) -> Self {
2021-06-28 14:42:19 +01:00
LivePeerState {
2021-07-02 01:38:07 +01:00
peer_id,
2021-06-28 14:42:19 +01:00
i_am_choked: true,
peer_interested: false,
2023-11-20 09:17:42 +00:00
bitfield: BF::new(),
previously_requested_pieces: BF::new(),
2021-06-28 14:42:19 +01:00
have_notify: Arc::new(Notify::new()),
2021-06-28 14:58:53 +01:00
requests_sem: Arc::new(Semaphore::new(0)),
2021-06-28 14:42:19 +01:00
inflight_requests: Default::default(),
tx,
2021-06-28 14:42:19 +01:00
}
}
2023-11-18 00:20:49 +00:00
pub fn has_full_torrent(&self, total_pieces: usize) -> bool {
2023-11-20 09:17:42 +00:00
self.bitfield
.get(0..total_pieces)
.map_or(false, |s| s.all())
2023-11-18 00:20:49 +00:00
}
2021-06-28 14:42:19 +01:00
}