Downgrade visibility within librqbit
This commit is contained in:
parent
84766f92fb
commit
f45a15c89a
9 changed files with 44 additions and 72 deletions
|
|
@ -4,7 +4,7 @@ use tracing::{debug, info};
|
||||||
|
|
||||||
use crate::type_aliases::BF;
|
use crate::type_aliases::BF;
|
||||||
|
|
||||||
pub struct ChunkTracker {
|
pub(crate) struct ChunkTracker {
|
||||||
// This forms the basis of a "queue" to pull from.
|
// 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 1 if we need a piece, but the moment we start requesting a peer,
|
||||||
// it's set to 0.
|
// it's set to 0.
|
||||||
|
|
@ -51,7 +51,7 @@ fn compute_chunk_status(lengths: &Lengths, needed_pieces: &BF) -> BF {
|
||||||
chunk_bf
|
chunk_bf
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ChunkMarkingResult {
|
pub(crate) enum ChunkMarkingResult {
|
||||||
PreviouslyCompleted,
|
PreviouslyCompleted,
|
||||||
NotCompleted,
|
NotCompleted,
|
||||||
Completed,
|
Completed,
|
||||||
|
|
@ -75,9 +75,7 @@ impl ChunkTracker {
|
||||||
priority_piece_ids,
|
priority_piece_ids,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_needed_pieces(&self) -> &BF {
|
|
||||||
&self.needed_pieces
|
|
||||||
}
|
|
||||||
pub fn get_have_pieces(&self) -> &BF {
|
pub fn get_have_pieces(&self) -> &BF {
|
||||||
&self.have
|
&self.have
|
||||||
}
|
}
|
||||||
|
|
@ -132,13 +130,6 @@ impl ChunkTracker {
|
||||||
self.have.set(idx.get() as usize, true);
|
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 {
|
pub fn is_chunk_ready_to_upload(&self, chunk: &ChunkInfo) -> bool {
|
||||||
self.have
|
self.have
|
||||||
.get(chunk.piece_index.get() as usize)
|
.get(chunk.piece_index.get() as usize)
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ use tracing::{debug, trace, warn};
|
||||||
|
|
||||||
use crate::type_aliases::{PeerHandle, BF};
|
use crate::type_aliases::{PeerHandle, BF};
|
||||||
|
|
||||||
pub struct InitialCheckResults {
|
pub(crate) struct InitialCheckResults {
|
||||||
pub needed_pieces: BF,
|
pub needed_pieces: BF,
|
||||||
pub have_pieces: BF,
|
pub have_pieces: BF,
|
||||||
pub have_bytes: u64,
|
pub have_bytes: u64,
|
||||||
|
|
@ -43,7 +43,7 @@ pub fn update_hash_from_file<Sha1: ISha1>(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FileOps<'a, Sha1> {
|
pub(crate) struct FileOps<'a, Sha1> {
|
||||||
torrent: &'a TorrentMetaV1Info<ByteString>,
|
torrent: &'a TorrentMetaV1Info<ByteString>,
|
||||||
files: &'a [Arc<Mutex<File>>],
|
files: &'a [Arc<Mutex<File>>],
|
||||||
lengths: &'a Lengths,
|
lengths: &'a Lengths,
|
||||||
|
|
|
||||||
|
|
@ -330,7 +330,7 @@ impl TorrentAddQueryParams {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private HTTP API internals. Agnostic of web framework.
|
// Private HTTP API internals. Agnostic of web framework.
|
||||||
pub struct ApiInternal {
|
struct ApiInternal {
|
||||||
dht: Option<Dht>,
|
dht: Option<Dht>,
|
||||||
startup_time: Instant,
|
startup_time: Instant,
|
||||||
torrent_managers: RwLock<Vec<TorrentManagerHandle>>,
|
torrent_managers: RwLock<Vec<TorrentManagerHandle>>,
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,7 @@ use crate::{
|
||||||
use self::{
|
use self::{
|
||||||
peer::{
|
peer::{
|
||||||
stats::{
|
stats::{
|
||||||
atomic::PeerCounters as AtomicPeerCounters,
|
atomic::PeerCountersAtomic as AtomicPeerCounters,
|
||||||
snapshot::{PeerStatsFilter, PeerStatsSnapshot},
|
snapshot::{PeerStatsFilter, PeerStatsSnapshot},
|
||||||
},
|
},
|
||||||
InflightRequest, PeerState, PeerTx, SendMany,
|
InflightRequest, PeerState, PeerTx, SendMany,
|
||||||
|
|
@ -103,18 +103,18 @@ use self::{
|
||||||
|
|
||||||
use super::utils::{timeit, TimedExistence};
|
use super::utils::{timeit, TimedExistence};
|
||||||
|
|
||||||
pub struct InflightPiece {
|
struct InflightPiece {
|
||||||
pub peer: PeerHandle,
|
peer: PeerHandle,
|
||||||
pub started: Instant,
|
started: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TorrentStateLocked {
|
pub(crate) struct TorrentStateLocked {
|
||||||
// What chunks we have and need.
|
// 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.
|
// At a moment in time, we are expecting a piece from only one peer.
|
||||||
// inflight_pieces stores this information.
|
// inflight_pieces stores this information.
|
||||||
pub inflight_pieces: HashMap<ValidPieceIndex, InflightPiece>,
|
inflight_pieces: HashMap<ValidPieceIndex, InflightPiece>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
@ -148,7 +148,7 @@ pub struct TorrentState {
|
||||||
|
|
||||||
impl TorrentState {
|
impl TorrentState {
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn new(
|
pub(crate) fn new(
|
||||||
info: TorrentMetaV1Info<ByteString>,
|
info: TorrentMetaV1Info<ByteString>,
|
||||||
info_hash: Id20,
|
info_hash: Id20,
|
||||||
peer_id: Id20,
|
peer_id: Id20,
|
||||||
|
|
@ -194,7 +194,7 @@ impl TorrentState {
|
||||||
state
|
state
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn task_manage_peer(
|
async fn task_manage_peer(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
spawner: BlockingSpawner,
|
spawner: BlockingSpawner,
|
||||||
|
|
@ -260,7 +260,7 @@ impl TorrentState {
|
||||||
Ok::<_, anyhow::Error>(())
|
Ok::<_, anyhow::Error>(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn task_peer_adder(
|
async fn task_peer_adder(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
mut peer_queue_rx: UnboundedReceiver<SocketAddr>,
|
mut peer_queue_rx: UnboundedReceiver<SocketAddr>,
|
||||||
spawner: BlockingSpawner,
|
spawner: BlockingSpawner,
|
||||||
|
|
@ -292,19 +292,20 @@ impl TorrentState {
|
||||||
pub fn peer_id(&self) -> Id20 {
|
pub fn peer_id(&self) -> Id20 {
|
||||||
self.peer_id
|
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)
|
FileOps::new(&self.info, &self.files, &self.lengths)
|
||||||
}
|
}
|
||||||
pub fn initially_needed(&self) -> u64 {
|
pub fn initially_needed(&self) -> u64 {
|
||||||
self.needed_bytes
|
self.needed_bytes
|
||||||
}
|
}
|
||||||
pub fn lock_read(
|
|
||||||
|
pub(crate) fn lock_read(
|
||||||
&self,
|
&self,
|
||||||
reason: &'static str,
|
reason: &'static str,
|
||||||
) -> TimedExistence<RwLockReadGuard<TorrentStateLocked>> {
|
) -> TimedExistence<RwLockReadGuard<TorrentStateLocked>> {
|
||||||
TimedExistence::new(timeit(reason, || self.locked.read()), reason)
|
TimedExistence::new(timeit(reason, || self.locked.read()), reason)
|
||||||
}
|
}
|
||||||
pub fn lock_write(
|
pub(crate) fn lock_write(
|
||||||
&self,
|
&self,
|
||||||
reason: &'static str,
|
reason: &'static str,
|
||||||
) -> TimedExistence<RwLockWriteGuard<TorrentStateLocked>> {
|
) -> 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) {
|
match self.peers.add_if_not_seen(addr) {
|
||||||
Some(handle) => handle,
|
Some(handle) => handle,
|
||||||
None => return false,
|
None => return false,
|
||||||
|
|
@ -1171,7 +1172,7 @@ impl PeerHandler {
|
||||||
for mut pe in self.state.peers.states.iter_mut() {
|
for mut pe in self.state.peers.states.iter_mut() {
|
||||||
if let PeerState::Live(l) = pe.value().state.get() {
|
if let PeerState::Live(l) = pe.value().state.get() {
|
||||||
if l.has_full_torrent(self.state.lengths.total_pieces() as usize) {
|
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
|
let _ = prev
|
||||||
.take_live_no_counters()
|
.take_live_no_counters()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ use crate::type_aliases::BF;
|
||||||
use super::peers::stats::atomic::AggregatePeerStatsAtomic;
|
use super::peers::stats::atomic::AggregatePeerStatsAtomic;
|
||||||
|
|
||||||
#[derive(Debug, Hash, PartialEq, Eq)]
|
#[derive(Debug, Hash, PartialEq, Eq)]
|
||||||
pub struct InflightRequest {
|
pub(crate) struct InflightRequest {
|
||||||
pub piece: ValidPieceIndex,
|
pub piece: ValidPieceIndex,
|
||||||
pub chunk: u32,
|
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.
|
// TODO: Arc can be removed probably, as UnboundedSender should be clone + it can be downgraded to weak.
|
||||||
pub type PeerRx = UnboundedReceiver<WriterRequest>;
|
pub(crate) type PeerRx = UnboundedReceiver<WriterRequest>;
|
||||||
pub type PeerTx = UnboundedSender<WriterRequest>;
|
pub(crate) type PeerTx = UnboundedSender<WriterRequest>;
|
||||||
|
|
||||||
pub trait SendMany {
|
pub trait SendMany {
|
||||||
fn send_many(&self, requests: impl IntoIterator<Item = WriterRequest>) -> anyhow::Result<()>;
|
fn send_many(&self, requests: impl IntoIterator<Item = WriterRequest>) -> anyhow::Result<()>;
|
||||||
|
|
@ -47,13 +47,13 @@ impl SendMany for PeerTx {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Peer {
|
pub(crate) struct Peer {
|
||||||
pub state: PeerStateNoMut,
|
pub state: PeerStateNoMut,
|
||||||
pub stats: stats::atomic::PeerStats,
|
pub stats: stats::atomic::PeerStats,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub enum PeerState {
|
pub(crate) enum PeerState {
|
||||||
#[default]
|
#[default]
|
||||||
// Will be tried to be connected as soon as possible.
|
// Will be tried to be connected as soon as possible.
|
||||||
Queued,
|
Queued,
|
||||||
|
|
@ -93,7 +93,7 @@ impl PeerState {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct PeerStateNoMut(PeerState);
|
pub(crate) struct PeerStateNoMut(PeerState);
|
||||||
|
|
||||||
impl PeerStateNoMut {
|
impl PeerStateNoMut {
|
||||||
pub fn get(&self) -> &PeerState {
|
pub fn get(&self) -> &PeerState {
|
||||||
|
|
@ -109,13 +109,6 @@ impl PeerStateNoMut {
|
||||||
std::mem::replace(&mut self.0, new)
|
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> {
|
pub fn get_live_mut(&mut self) -> Option<&mut LivePeerState> {
|
||||||
match &mut self.0 {
|
match &mut self.0 {
|
||||||
PeerState::Live(l) => Some(l),
|
PeerState::Live(l) => Some(l),
|
||||||
|
|
@ -153,18 +146,16 @@ impl PeerStateNoMut {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_dead(&mut self, counters: &AggregatePeerStatsAtomic) -> PeerState {
|
pub fn set_not_needed(&mut self, counters: &AggregatePeerStatsAtomic) -> PeerState {
|
||||||
self.set(PeerState::Dead, counters)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn to_not_needed(&mut self, counters: &AggregatePeerStatsAtomic) -> PeerState {
|
|
||||||
self.set(PeerState::NotNeeded, counters)
|
self.set(PeerState::NotNeeded, counters)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct LivePeerState {
|
pub(crate) struct LivePeerState {
|
||||||
pub peer_id: Id20,
|
#[allow(dead_code)]
|
||||||
|
peer_id: Id20,
|
||||||
|
|
||||||
pub peer_interested: bool,
|
pub peer_interested: bool,
|
||||||
|
|
||||||
// This is used to track the pieces the peer has.
|
// This is used to track the pieces the peer has.
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use std::{
|
||||||
use backoff::{ExponentialBackoff, ExponentialBackoffBuilder};
|
use backoff::{ExponentialBackoff, ExponentialBackoffBuilder};
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
pub struct PeerCounters {
|
pub(crate) struct PeerCountersAtomic {
|
||||||
pub fetched_bytes: AtomicU64,
|
pub fetched_bytes: AtomicU64,
|
||||||
pub total_time_connecting_ms: AtomicU64,
|
pub total_time_connecting_ms: AtomicU64,
|
||||||
pub connection_attempts: AtomicU32,
|
pub connection_attempts: AtomicU32,
|
||||||
|
|
@ -21,8 +21,8 @@ pub struct PeerCounters {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PeerStats {
|
pub(crate) struct PeerStats {
|
||||||
pub counters: Arc<PeerCounters>,
|
pub counters: Arc<PeerCountersAtomic>,
|
||||||
pub backoff: ExponentialBackoff,
|
pub backoff: ExponentialBackoff,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,8 @@ pub struct PeerStats {
|
||||||
pub state: &'static str,
|
pub state: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&super::atomic::PeerCounters> for PeerCounters {
|
impl From<&super::atomic::PeerCountersAtomic> for PeerCounters {
|
||||||
fn from(counters: &super::atomic::PeerCounters) -> Self {
|
fn from(counters: &super::atomic::PeerCountersAtomic) -> Self {
|
||||||
Self {
|
Self {
|
||||||
fetched_bytes: counters.fetched_bytes.load(Ordering::Relaxed),
|
fetched_bytes: counters.fetched_bytes.load(Ordering::Relaxed),
|
||||||
total_time_connecting_ms: counters.total_time_connecting_ms.load(Ordering::Relaxed),
|
total_time_connecting_ms: counters.total_time_connecting_ms.load(Ordering::Relaxed),
|
||||||
|
|
@ -59,12 +59,8 @@ pub enum PeerStatsFilterState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PeerStatsFilterState {
|
impl PeerStatsFilterState {
|
||||||
pub fn matches(&self, s: &PeerState) -> bool {
|
pub(crate) fn matches(&self, s: &PeerState) -> bool {
|
||||||
match (self, s) {
|
matches!((self, s), (Self::All, _) | (Self::Live, PeerState::Live(_)))
|
||||||
(Self::All, _) => true,
|
|
||||||
(Self::Live, PeerState::Live(_)) => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ use super::peer::{LivePeerState, Peer, PeerRx, PeerState, PeerTx};
|
||||||
pub mod stats;
|
pub mod stats;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct PeerStates {
|
pub(crate) struct PeerStates {
|
||||||
pub stats: AggregatePeerStatsAtomic,
|
pub stats: AggregatePeerStatsAtomic,
|
||||||
pub states: DashMap<PeerHandle, Peer>,
|
pub states: DashMap<PeerHandle, Peer>,
|
||||||
}
|
}
|
||||||
|
|
@ -52,14 +52,7 @@ impl PeerStates {
|
||||||
timeit(reason, || self.states.get_mut(&addr))
|
timeit(reason, || self.states.get_mut(&addr))
|
||||||
.map(|e| f(TimedExistence::new(e, reason).value_mut()))
|
.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>(
|
pub fn with_live_mut<R>(
|
||||||
&self,
|
&self,
|
||||||
addr: PeerHandle,
|
addr: PeerHandle,
|
||||||
|
|
@ -107,7 +100,7 @@ impl PeerStates {
|
||||||
|
|
||||||
pub fn mark_peer_not_needed(&self, handle: PeerHandle) -> Option<PeerState> {
|
pub fn mark_peer_not_needed(&self, handle: PeerHandle) -> Option<PeerState> {
|
||||||
let prev = self.with_peer_mut(handle, "mark_peer_not_needed", |peer| {
|
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)
|
Some(prev)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use crate::torrent_state::{
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Default, Serialize)]
|
#[derive(Debug, Default, Serialize)]
|
||||||
pub struct AggregatePeerStatsAtomic {
|
pub(crate) struct AggregatePeerStatsAtomic {
|
||||||
pub queued: AtomicU32,
|
pub queued: AtomicU32,
|
||||||
pub connecting: AtomicU32,
|
pub connecting: AtomicU32,
|
||||||
pub live: AtomicU32,
|
pub live: AtomicU32,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue