Downgrade visibility within librqbit

This commit is contained in:
Igor Katson 2023-11-23 16:57:27 +00:00
parent 84766f92fb
commit f45a15c89a
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
9 changed files with 44 additions and 72 deletions

View file

@ -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)

View file

@ -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<Sha1: ISha1>(
Ok(())
}
pub struct FileOps<'a, Sha1> {
pub(crate) struct FileOps<'a, Sha1> {
torrent: &'a TorrentMetaV1Info<ByteString>,
files: &'a [Arc<Mutex<File>>],
lengths: &'a Lengths,

View file

@ -330,7 +330,7 @@ impl TorrentAddQueryParams {
}
// Private HTTP API internals. Agnostic of web framework.
pub struct ApiInternal {
struct ApiInternal {
dht: Option<Dht>,
startup_time: Instant,
torrent_managers: RwLock<Vec<TorrentManagerHandle>>,

View file

@ -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<ValidPieceIndex, InflightPiece>,
inflight_pieces: HashMap<ValidPieceIndex, InflightPiece>,
}
#[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<ByteString>,
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<Self>,
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<Self>,
mut peer_queue_rx: UnboundedReceiver<SocketAddr>,
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<RwLockReadGuard<TorrentStateLocked>> {
TimedExistence::new(timeit(reason, || self.locked.read()), reason)
}
pub fn lock_write(
pub(crate) fn lock_write(
&self,
reason: &'static str,
) -> TimedExistence<RwLockWriteGuard<TorrentStateLocked>> {
@ -417,7 +418,7 @@ impl TorrentState {
);
}
pub fn add_peer_if_not_seen(self: &Arc<Self>, addr: SocketAddr) -> bool {
pub(crate) fn add_peer_if_not_seen(self: &Arc<Self>, 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()

View file

@ -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<WriterRequest>;
pub type PeerTx = UnboundedSender<WriterRequest>;
pub(crate) type PeerRx = UnboundedReceiver<WriterRequest>;
pub(crate) type PeerTx = UnboundedSender<WriterRequest>;
pub trait SendMany {
fn send_many(&self, requests: impl IntoIterator<Item = WriterRequest>) -> 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.

View file

@ -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<PeerCounters>,
pub(crate) struct PeerStats {
pub counters: Arc<PeerCountersAtomic>,
pub backoff: ExponentialBackoff,
}

View file

@ -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(_)))
}
}

View file

@ -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<PeerHandle, Peer>,
}
@ -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<R>(&self, addr: PeerHandle, f: impl FnOnce(&LivePeerState) -> R) -> Option<R> {
self.states
.get(&addr)
.and_then(|e| match &e.value().state.get() {
PeerState::Live(l) => Some(f(l)),
_ => None,
})
}
pub fn with_live_mut<R>(
&self,
addr: PeerHandle,
@ -107,7 +100,7 @@ impl PeerStates {
pub fn mark_peer_not_needed(&self, handle: PeerHandle) -> Option<PeerState> {
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)
}

View file

@ -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,