mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 18:55:32 +00:00
Run rustfmt, clean up aquatic_http_protocol/Cargo.toml
This commit is contained in:
parent
0cc312a78d
commit
d0e716f80b
65 changed files with 1754 additions and 2590 deletions
|
|
@ -1,18 +1,16 @@
|
|||
use std::time::{Duration, Instant};
|
||||
use std::net::IpAddr;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use rand::Rng;
|
||||
|
||||
|
||||
/// Peer or connection valid until this instant
|
||||
///
|
||||
///
|
||||
/// Used instead of "last seen" or similar to hopefully prevent arithmetic
|
||||
/// overflow when cleaning.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ValidUntil(pub Instant);
|
||||
|
||||
|
||||
impl ValidUntil {
|
||||
#[inline]
|
||||
pub fn new(offset_seconds: u64) -> Self {
|
||||
|
|
@ -20,9 +18,8 @@ impl ValidUntil {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// Extract response peers
|
||||
///
|
||||
///
|
||||
/// If there are more peers in map than `max_num_peers_to_take`, do a
|
||||
/// half-random selection of peers from first and second halves of map,
|
||||
/// in order to avoid returning too homogeneous peers.
|
||||
|
|
@ -34,17 +31,18 @@ pub fn extract_response_peers<K, V, R, F>(
|
|||
peer_map: &IndexMap<K, V>,
|
||||
max_num_peers_to_take: usize,
|
||||
sender_peer_map_key: K,
|
||||
peer_conversion_function: F
|
||||
peer_conversion_function: F,
|
||||
) -> Vec<R>
|
||||
where
|
||||
K: Eq + ::std::hash::Hash,
|
||||
F: Fn(&V) -> R
|
||||
where
|
||||
K: Eq + ::std::hash::Hash,
|
||||
F: Fn(&V) -> R,
|
||||
{
|
||||
let peer_map_len = peer_map.len();
|
||||
|
||||
if peer_map_len <= max_num_peers_to_take + 1 {
|
||||
peer_map.iter()
|
||||
.filter_map(|(k, v)|{
|
||||
peer_map
|
||||
.iter()
|
||||
.filter_map(|(k, v)| {
|
||||
if *k == sender_peer_map_key {
|
||||
None
|
||||
} else {
|
||||
|
|
@ -56,12 +54,9 @@ pub fn extract_response_peers<K, V, R, F>(
|
|||
let half_num_to_take = max_num_peers_to_take / 2;
|
||||
let half_peer_map_len = peer_map_len / 2;
|
||||
|
||||
let offset_first_half = rng.gen_range(
|
||||
0..(half_peer_map_len + (peer_map_len % 2)) - half_num_to_take
|
||||
);
|
||||
let offset_second_half = rng.gen_range(
|
||||
half_peer_map_len..peer_map_len - half_num_to_take
|
||||
);
|
||||
let offset_first_half =
|
||||
rng.gen_range(0..(half_peer_map_len + (peer_map_len % 2)) - half_num_to_take);
|
||||
let offset_second_half = rng.gen_range(half_peer_map_len..peer_map_len - half_num_to_take);
|
||||
|
||||
let end_first_half = offset_first_half + half_num_to_take;
|
||||
let end_second_half = offset_second_half + half_num_to_take + (max_num_peers_to_take % 2);
|
||||
|
|
@ -69,14 +64,14 @@ pub fn extract_response_peers<K, V, R, F>(
|
|||
let mut peers: Vec<R> = Vec::with_capacity(max_num_peers_to_take);
|
||||
|
||||
for i in offset_first_half..end_first_half {
|
||||
if let Some((k, peer)) = peer_map.get_index(i){
|
||||
if let Some((k, peer)) = peer_map.get_index(i) {
|
||||
if *k != sender_peer_map_key {
|
||||
peers.push(peer_conversion_function(peer))
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in offset_second_half..end_second_half {
|
||||
if let Some((k, peer)) = peer_map.get_index(i){
|
||||
if let Some((k, peer)) = peer_map.get_index(i) {
|
||||
if *k != sender_peer_map_key {
|
||||
peers.push(peer_conversion_function(peer))
|
||||
}
|
||||
|
|
@ -87,16 +82,15 @@ pub fn extract_response_peers<K, V, R, F>(
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
pub fn convert_ipv4_mapped_ipv6(ip_address: IpAddr) -> IpAddr {
|
||||
if let IpAddr::V6(ip) = ip_address {
|
||||
if let [0, 0, 0, 0, 0, 0xffff, ..] = ip.segments(){
|
||||
if let [0, 0, 0, 0, 0, 0xffff, ..] = ip.segments() {
|
||||
ip.to_ipv4().expect("convert ipv4-mapped ip").into()
|
||||
} else {
|
||||
ip_address
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ip_address
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue