http: index peer map by IP and port instead of IP and peer id

This commit is contained in:
Joakim Frostegård 2024-01-24 22:51:53 +01:00
parent 0286d25346
commit 63ae98f67c
3 changed files with 14 additions and 30 deletions

View file

@ -41,6 +41,11 @@
* Support running without TLS * Support running without TLS
* Support running behind reverse proxy * Support running behind reverse proxy
#### Changed
* Index peers by packet source IP and provided port instead of by source ip
and peer id. This is likely slightly faster.
#### Fixed #### Fixed
* Fix bug where clean up after closing connections wasn't always done * Fix bug where clean up after closing connections wasn't always done

View file

@ -245,33 +245,29 @@ impl<I: Ip> TorrentData<I> {
let peer_status = let peer_status =
PeerStatus::from_event_and_bytes_left(request.event, Some(request.bytes_left)); PeerStatus::from_event_and_bytes_left(request.event, Some(request.bytes_left));
let peer_map_key = PeerMapKey { let peer_map_key = ResponsePeer {
peer_id: request.peer_id, ip_address: peer_ip_address,
ip: peer_ip_address, port: request.port,
}; };
let opt_removed_peer = match peer_status { let opt_removed_peer = match peer_status {
PeerStatus::Leeching => { PeerStatus::Leeching => {
let peer = Peer { let peer = Peer {
ip_address: peer_ip_address,
port: request.port,
valid_until, valid_until,
seeder: false, seeder: false,
}; };
self.peers.insert(peer_map_key.clone(), peer) self.peers.insert(peer_map_key, peer)
} }
PeerStatus::Seeding => { PeerStatus::Seeding => {
self.num_seeders += 1; self.num_seeders += 1;
let peer = Peer { let peer = Peer {
ip_address: peer_ip_address,
port: request.port,
valid_until, valid_until,
seeder: true, seeder: true,
}; };
self.peers.insert(peer_map_key.clone(), peer) self.peers.insert(peer_map_key, peer)
} }
PeerStatus::Stopped => self.peers.remove(&peer_map_key), PeerStatus::Stopped => self.peers.remove(&peer_map_key),
}; };
@ -314,7 +310,7 @@ impl<I: Ip> TorrentData<I> {
&self.peers, &self.peers,
max_num_peers_to_take, max_num_peers_to_take,
peer_map_key, peer_map_key,
Peer::to_response_peer, |k, _| *k,
) )
}; };
@ -322,31 +318,14 @@ impl<I: Ip> TorrentData<I> {
} }
} }
type PeerMap<I> = IndexMap<PeerMapKey<I>, Peer<I>>; type PeerMap<I> = IndexMap<ResponsePeer<I>, Peer>;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct PeerMapKey<I: Ip> {
pub peer_id: PeerId,
pub ip: I,
}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
struct Peer<I: Ip> { struct Peer {
pub ip_address: I,
pub port: u16,
pub valid_until: ValidUntil, pub valid_until: ValidUntil,
pub seeder: bool, pub seeder: bool,
} }
impl<I: Ip> Peer<I> {
fn to_response_peer(_: &PeerMapKey<I>, peer: &Self) -> ResponsePeer<I> {
ResponsePeer {
ip_address: peer.ip_address,
port: peer.port,
}
}
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)] #[derive(PartialEq, Eq, Clone, Copy, Debug)]
enum PeerStatus { enum PeerStatus {
Seeding, Seeding,

View file

@ -8,7 +8,7 @@ use std::collections::BTreeMap;
use super::common::*; use super::common::*;
use super::utils::*; use super::utils::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct ResponsePeer<I: Eq> { pub struct ResponsePeer<I: Eq> {
pub ip_address: I, pub ip_address: I,
pub port: u16, pub port: u16,