diff --git a/crates/librqbit/src/chunk_tracker.rs b/crates/librqbit/src/chunk_tracker.rs index e223d06..880ce53 100644 --- a/crates/librqbit/src/chunk_tracker.rs +++ b/crates/librqbit/src/chunk_tracker.rs @@ -4,7 +4,7 @@ use tracing::{debug, info}; use crate::type_aliases::BF; -pub struct ChunkTracker { +pub(crate) struct ChunkTracker { // This forms the basis of a "queue" to pull from. // It's set to 1 if we need a piece, but the moment we start requesting a peer, // it's set to 0. @@ -51,7 +51,7 @@ fn compute_chunk_status(lengths: &Lengths, needed_pieces: &BF) -> BF { chunk_bf } -pub enum ChunkMarkingResult { +pub(crate) enum ChunkMarkingResult { PreviouslyCompleted, NotCompleted, Completed, @@ -75,9 +75,7 @@ impl ChunkTracker { priority_piece_ids, } } - pub fn get_needed_pieces(&self) -> &BF { - &self.needed_pieces - } + pub fn get_have_pieces(&self) -> &BF { &self.have } @@ -132,13 +130,6 @@ impl ChunkTracker { self.have.set(idx.get() as usize, true); } - pub fn is_chunk_downloaded(&self, chunk: &ChunkInfo) -> bool { - *self - .chunk_status - .get(chunk.absolute_index as usize) - .unwrap() - } - pub fn is_chunk_ready_to_upload(&self, chunk: &ChunkInfo) -> bool { self.have .get(chunk.piece_index.get() as usize) diff --git a/crates/librqbit/src/file_ops.rs b/crates/librqbit/src/file_ops.rs index 8136c9f..e413dc4 100644 --- a/crates/librqbit/src/file_ops.rs +++ b/crates/librqbit/src/file_ops.rs @@ -18,7 +18,7 @@ use tracing::{debug, trace, warn}; use crate::type_aliases::{PeerHandle, BF}; -pub struct InitialCheckResults { +pub(crate) struct InitialCheckResults { pub needed_pieces: BF, pub have_pieces: BF, pub have_bytes: u64, @@ -43,7 +43,7 @@ pub fn update_hash_from_file( Ok(()) } -pub struct FileOps<'a, Sha1> { +pub(crate) struct FileOps<'a, Sha1> { torrent: &'a TorrentMetaV1Info, files: &'a [Arc>], lengths: &'a Lengths, diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index 11395d8..2249c4f 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -330,7 +330,7 @@ impl TorrentAddQueryParams { } // Private HTTP API internals. Agnostic of web framework. -pub struct ApiInternal { +struct ApiInternal { dht: Option, startup_time: Instant, torrent_managers: RwLock>, diff --git a/crates/librqbit/src/torrent_state/live/mod.rs b/crates/librqbit/src/torrent_state/live/mod.rs index fa49040..b2be121 100644 --- a/crates/librqbit/src/torrent_state/live/mod.rs +++ b/crates/librqbit/src/torrent_state/live/mod.rs @@ -92,7 +92,7 @@ use crate::{ use self::{ peer::{ stats::{ - atomic::PeerCounters as AtomicPeerCounters, + atomic::PeerCountersAtomic as AtomicPeerCounters, snapshot::{PeerStatsFilter, PeerStatsSnapshot}, }, InflightRequest, PeerState, PeerTx, SendMany, @@ -103,18 +103,18 @@ use self::{ use super::utils::{timeit, TimedExistence}; -pub struct InflightPiece { - pub peer: PeerHandle, - pub started: Instant, +struct InflightPiece { + peer: PeerHandle, + started: Instant, } -pub struct TorrentStateLocked { +pub(crate) struct TorrentStateLocked { // What chunks we have and need. - pub chunks: ChunkTracker, + pub(crate) chunks: ChunkTracker, // At a moment in time, we are expecting a piece from only one peer. // inflight_pieces stores this information. - pub inflight_pieces: HashMap, + inflight_pieces: HashMap, } #[derive(Default)] @@ -148,7 +148,7 @@ pub struct TorrentState { impl TorrentState { #[allow(clippy::too_many_arguments)] - pub fn new( + pub(crate) fn new( info: TorrentMetaV1Info, info_hash: Id20, peer_id: Id20, @@ -194,7 +194,7 @@ impl TorrentState { state } - pub async fn task_manage_peer( + async fn task_manage_peer( self: Arc, addr: SocketAddr, spawner: BlockingSpawner, @@ -260,7 +260,7 @@ impl TorrentState { Ok::<_, anyhow::Error>(()) } - pub async fn task_peer_adder( + async fn task_peer_adder( self: Arc, mut peer_queue_rx: UnboundedReceiver, spawner: BlockingSpawner, @@ -292,19 +292,20 @@ impl TorrentState { pub fn peer_id(&self) -> Id20 { self.peer_id } - pub fn file_ops(&self) -> FileOps<'_, Sha1> { + pub(crate) fn file_ops(&self) -> FileOps<'_, Sha1> { FileOps::new(&self.info, &self.files, &self.lengths) } pub fn initially_needed(&self) -> u64 { self.needed_bytes } - pub fn lock_read( + + pub(crate) fn lock_read( &self, reason: &'static str, ) -> TimedExistence> { TimedExistence::new(timeit(reason, || self.locked.read()), reason) } - pub fn lock_write( + pub(crate) fn lock_write( &self, reason: &'static str, ) -> TimedExistence> { @@ -417,7 +418,7 @@ impl TorrentState { ); } - pub fn add_peer_if_not_seen(self: &Arc, addr: SocketAddr) -> bool { + pub(crate) fn add_peer_if_not_seen(self: &Arc, addr: SocketAddr) -> bool { match self.peers.add_if_not_seen(addr) { Some(handle) => handle, None => return false, @@ -1171,7 +1172,7 @@ impl PeerHandler { for mut pe in self.state.peers.states.iter_mut() { if let PeerState::Live(l) = pe.value().state.get() { if l.has_full_torrent(self.state.lengths.total_pieces() as usize) { - let prev = pe.value_mut().state.to_not_needed(&self.state.peers.stats); + let prev = pe.value_mut().state.set_not_needed(&self.state.peers.stats); let _ = prev .take_live_no_counters() .unwrap() diff --git a/crates/librqbit/src/torrent_state/live/peer/mod.rs b/crates/librqbit/src/torrent_state/live/peer/mod.rs index 9b16d5e..675d762 100644 --- a/crates/librqbit/src/torrent_state/live/peer/mod.rs +++ b/crates/librqbit/src/torrent_state/live/peer/mod.rs @@ -15,7 +15,7 @@ use crate::type_aliases::BF; use super::peers::stats::atomic::AggregatePeerStatsAtomic; #[derive(Debug, Hash, PartialEq, Eq)] -pub struct InflightRequest { +pub(crate) struct InflightRequest { pub piece: ValidPieceIndex, pub chunk: u32, } @@ -30,8 +30,8 @@ impl From<&ChunkInfo> for InflightRequest { } // TODO: Arc can be removed probably, as UnboundedSender should be clone + it can be downgraded to weak. -pub type PeerRx = UnboundedReceiver; -pub type PeerTx = UnboundedSender; +pub(crate) type PeerRx = UnboundedReceiver; +pub(crate) type PeerTx = UnboundedSender; pub trait SendMany { fn send_many(&self, requests: impl IntoIterator) -> anyhow::Result<()>; @@ -47,13 +47,13 @@ impl SendMany for PeerTx { } #[derive(Debug, Default)] -pub struct Peer { +pub(crate) struct Peer { pub state: PeerStateNoMut, pub stats: stats::atomic::PeerStats, } #[derive(Debug, Default)] -pub enum PeerState { +pub(crate) enum PeerState { #[default] // Will be tried to be connected as soon as possible. Queued, @@ -93,7 +93,7 @@ impl PeerState { } #[derive(Debug, Default)] -pub struct PeerStateNoMut(PeerState); +pub(crate) struct PeerStateNoMut(PeerState); impl PeerStateNoMut { pub fn get(&self) -> &PeerState { @@ -109,13 +109,6 @@ impl PeerStateNoMut { std::mem::replace(&mut self.0, new) } - pub fn get_live(&self) -> Option<&LivePeerState> { - match &self.0 { - PeerState::Live(l) => Some(l), - _ => None, - } - } - pub fn get_live_mut(&mut self) -> Option<&mut LivePeerState> { match &mut self.0 { PeerState::Live(l) => Some(l), @@ -153,18 +146,16 @@ impl PeerStateNoMut { } } - pub fn to_dead(&mut self, counters: &AggregatePeerStatsAtomic) -> PeerState { - self.set(PeerState::Dead, counters) - } - - pub fn to_not_needed(&mut self, counters: &AggregatePeerStatsAtomic) -> PeerState { + pub fn set_not_needed(&mut self, counters: &AggregatePeerStatsAtomic) -> PeerState { self.set(PeerState::NotNeeded, counters) } } #[derive(Debug)] -pub struct LivePeerState { - pub peer_id: Id20, +pub(crate) struct LivePeerState { + #[allow(dead_code)] + peer_id: Id20, + pub peer_interested: bool, // This is used to track the pieces the peer has. diff --git a/crates/librqbit/src/torrent_state/live/peer/stats/atomic.rs b/crates/librqbit/src/torrent_state/live/peer/stats/atomic.rs index bca260f..6c9b80a 100644 --- a/crates/librqbit/src/torrent_state/live/peer/stats/atomic.rs +++ b/crates/librqbit/src/torrent_state/live/peer/stats/atomic.rs @@ -9,7 +9,7 @@ use std::{ use backoff::{ExponentialBackoff, ExponentialBackoffBuilder}; #[derive(Default, Debug)] -pub struct PeerCounters { +pub(crate) struct PeerCountersAtomic { pub fetched_bytes: AtomicU64, pub total_time_connecting_ms: AtomicU64, pub connection_attempts: AtomicU32, @@ -21,8 +21,8 @@ pub struct PeerCounters { } #[derive(Debug)] -pub struct PeerStats { - pub counters: Arc, +pub(crate) struct PeerStats { + pub counters: Arc, pub backoff: ExponentialBackoff, } diff --git a/crates/librqbit/src/torrent_state/live/peer/stats/snapshot.rs b/crates/librqbit/src/torrent_state/live/peer/stats/snapshot.rs index 81c00cb..48db933 100644 --- a/crates/librqbit/src/torrent_state/live/peer/stats/snapshot.rs +++ b/crates/librqbit/src/torrent_state/live/peer/stats/snapshot.rs @@ -21,8 +21,8 @@ pub struct PeerStats { pub state: &'static str, } -impl From<&super::atomic::PeerCounters> for PeerCounters { - fn from(counters: &super::atomic::PeerCounters) -> Self { +impl From<&super::atomic::PeerCountersAtomic> for PeerCounters { + fn from(counters: &super::atomic::PeerCountersAtomic) -> Self { Self { fetched_bytes: counters.fetched_bytes.load(Ordering::Relaxed), total_time_connecting_ms: counters.total_time_connecting_ms.load(Ordering::Relaxed), @@ -59,12 +59,8 @@ pub enum PeerStatsFilterState { } impl PeerStatsFilterState { - pub fn matches(&self, s: &PeerState) -> bool { - match (self, s) { - (Self::All, _) => true, - (Self::Live, PeerState::Live(_)) => true, - _ => false, - } + pub(crate) fn matches(&self, s: &PeerState) -> bool { + matches!((self, s), (Self::All, _) | (Self::Live, PeerState::Live(_))) } } diff --git a/crates/librqbit/src/torrent_state/live/peers/mod.rs b/crates/librqbit/src/torrent_state/live/peers/mod.rs index 7973a27..df359b8 100644 --- a/crates/librqbit/src/torrent_state/live/peers/mod.rs +++ b/crates/librqbit/src/torrent_state/live/peers/mod.rs @@ -16,7 +16,7 @@ use super::peer::{LivePeerState, Peer, PeerRx, PeerState, PeerTx}; pub mod stats; #[derive(Default)] -pub struct PeerStates { +pub(crate) struct PeerStates { pub stats: AggregatePeerStatsAtomic, pub states: DashMap, } @@ -52,14 +52,7 @@ impl PeerStates { timeit(reason, || self.states.get_mut(&addr)) .map(|e| f(TimedExistence::new(e, reason).value_mut())) } - pub fn with_live(&self, addr: PeerHandle, f: impl FnOnce(&LivePeerState) -> R) -> Option { - self.states - .get(&addr) - .and_then(|e| match &e.value().state.get() { - PeerState::Live(l) => Some(f(l)), - _ => None, - }) - } + pub fn with_live_mut( &self, addr: PeerHandle, @@ -107,7 +100,7 @@ impl PeerStates { pub fn mark_peer_not_needed(&self, handle: PeerHandle) -> Option { let prev = self.with_peer_mut(handle, "mark_peer_not_needed", |peer| { - peer.state.to_not_needed(&self.stats) + peer.state.set_not_needed(&self.stats) })?; Some(prev) } diff --git a/crates/librqbit/src/torrent_state/live/peers/stats/atomic.rs b/crates/librqbit/src/torrent_state/live/peers/stats/atomic.rs index a9188c3..9c9605e 100644 --- a/crates/librqbit/src/torrent_state/live/peers/stats/atomic.rs +++ b/crates/librqbit/src/torrent_state/live/peers/stats/atomic.rs @@ -8,7 +8,7 @@ use crate::torrent_state::{ }; #[derive(Debug, Default, Serialize)] -pub struct AggregatePeerStatsAtomic { +pub(crate) struct AggregatePeerStatsAtomic { pub queued: AtomicU32, pub connecting: AtomicU32, pub live: AtomicU32,