use super::{ChokeAction, ChokingAlgorithm, IdentityChokeAction, PeerIdentity};
use crate::constants;
pub(super) fn rotate_choke_by_identity(algo: &mut ChokingAlgorithm) -> Vec<IdentityChokeAction> {
check_snubbed_peers_internal(algo);
if algo.peers.is_empty() {
return Vec::new();
}
let max_slots = algo.config.max_upload_slots;
let mut scored: Vec<(PeerIdentity, f64)> = algo
.peers
.iter()
.map(|peer| {
let identity = peer.into();
(
identity,
calculate_peer_score(peer, algo.snubbed_peers.contains(&identity)),
)
})
.collect();
scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
scored
.into_iter()
.enumerate()
.map(|(rank, (identity, _))| {
let peer = algo
.peers
.iter_mut()
.find(|peer| PeerIdentity::from(&**peer) == identity)
.expect("scored peer must remain registered");
if rank < max_slots {
if peer.am_choking {
peer.record_unchoke();
IdentityChokeAction::Unchoke(identity)
} else {
IdentityChokeAction::NoChange(identity)
}
} else if !peer.am_choking {
peer.record_choke();
IdentityChokeAction::Choke(identity)
} else {
IdentityChokeAction::NoChange(identity)
}
})
.collect()
}
pub(super) fn rotate_choke(algo: &mut ChokingAlgorithm) -> Vec<ChokeAction> {
check_snubbed_peers_internal(algo);
if algo.peers.is_empty() {
return vec![];
}
let max_slots = algo.config.max_upload_slots;
let mut scored_peers: Vec<(usize, f64)> = algo
.peers
.iter()
.enumerate()
.map(|(i, peer)| {
let is_snubbed = algo.snubbed_peers.contains(&peer.into());
(i, calculate_peer_score(peer, is_snubbed))
})
.collect();
scored_peers.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
let mut actions = Vec::new();
let mut _new_unchoked_indices = std::collections::HashSet::new();
for (rank, &(idx, _)) in scored_peers.iter().enumerate() {
if rank < max_slots {
if algo.peers[idx].am_choking {
actions.push(ChokeAction::Unchoke(idx));
algo.peers[idx].record_unchoke();
} else {
actions.push(ChokeAction::NoChange(idx));
}
_new_unchoked_indices.insert(idx);
} else {
if !algo.peers[idx].am_choking {
actions.push(ChokeAction::Choke(idx));
algo.peers[idx].record_choke();
} else {
actions.push(ChokeAction::NoChange(idx));
}
}
}
actions
}
pub(super) fn check_snubbed_peers_internal(algo: &mut ChokingAlgorithm) -> Vec<usize> {
let mut snubbed = vec![];
for (i, peer) in algo.peers.iter_mut().enumerate() {
if peer.check_snubbed(algo.config.snubbed_timeout_secs) {
algo.snubbed_peers.insert((&*peer).into());
snubbed.push(i);
}
}
snubbed
}
pub(super) fn calculate_peer_score(
peer: &crate::engine::peer_stats::PeerStats,
is_explicitly_snubbed: bool,
) -> f64 {
let mut score = 0.0;
score += peer.download_speed * constants::CHOKING_DOWNLOAD_SPEED_WEIGHT;
score += peer.upload_speed * constants::CHOKING_UPLOAD_SPEED_WEIGHT;
if peer.is_snubbed || is_explicitly_snubbed {
score -= constants::CHOKING_SNUBBED_PENALTY;
}
if peer.peer_interested {
score += constants::CHOKING_INTEREST_BONUS;
}
if !peer.am_choking
&& peer.time_since_last_unchoke().as_secs() < constants::CHOKING_ANTI_CHURN_THRESHOLD_SECS
{
score += constants::CHOKING_ANTI_CHURN_BONUS;
}
score
}