1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::collections::{HashMap, HashSet};
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::task::JoinSet;
use crate::error::Error;
use crate::peer::{PeerConnection, PeerId, PeerMessage};
use super::uni_deque::UniDeque;
/// Per-peer backoff state tracking connection retries.
#[derive(Debug)]
struct BackoffState {
attempts: u32,
cooldown_until: Instant,
}
impl BackoffState {
fn new(cooldown: Duration) -> Self {
BackoffState {
attempts: 0,
cooldown_until: Instant::now() + cooldown,
}
}
fn increment(&mut self, cooldown: Duration) {
self.attempts += 1;
self.cooldown_until = Instant::now() + cooldown;
}
}
/// Manages peer connections for a single torrent.
pub(crate) struct PeerManager {
/// Our peer ID.
peer_id: PeerId,
/// Info hash of the torrent.
info_hash: [u8; 20],
/// Active connections by remote address.
connections: HashMap<SocketAddr, Arc<PeerConnection>>,
/// Pending connection attempts (O(1) contains via internal HashSet).
pending: UniDeque<SocketAddr>,
/// Maximum connections.
max_connections: u32,
/// Per-peer backoff state (retry count + cooldown timer).
backoff: HashMap<SocketAddr, BackoffState>,
/// Per-peer TCP connect timeout.
connect_timeout: Duration,
/// Maximum connection retries before discarding.
max_retries: u32,
/// Cooldown before reconnecting a failed peer.
cooldown: Duration,
}
impl PeerManager {
/// Create a new PeerManager.
pub fn new(
info_hash: [u8; 20], peer_id: PeerId, max_connections: u32, connect_timeout: Duration,
max_retries: u32, cooldown: Duration,
) -> Self {
PeerManager {
peer_id,
info_hash,
connections: HashMap::new(),
pending: UniDeque::new(),
max_connections,
backoff: HashMap::new(),
connect_timeout,
max_retries,
cooldown,
}
}
/// Add peers from tracker/DHT announce.
///
/// Skips addresses already connected. Duplicate entries in the
/// pending queue are silently skipped by [`UniDeque::push_unique`].
pub fn add_peers(&mut self, addrs: Vec<SocketAddr>) {
for addr in addrs {
if !self.connections.contains_key(&addr) {
self.pending.push_unique(addr);
}
}
}
/// Send a message to a specific peer.
pub async fn send_to(&self, addr: &SocketAddr, msg: &PeerMessage) -> Result<(), Error> {
if let Some(conn) = self.connections.get(addr) {
conn.send(msg).await
} else {
Ok(())
}
}
/// Remove a peer (disconnect).
pub fn remove_peer(&mut self, addr: &SocketAddr) {
tracing::debug!("peer disconnected: {}", addr);
self.connections.remove(addr);
self.backoff.remove(addr);
}
/// Get the number of active connections.
pub fn num_connections(&self) -> usize {
self.connections.len()
}
/// Connect to multiple pending peers in parallel.
///
/// Drains a batch of pending peers (up to available slots), spawns
/// concurrent connection attempts via [`JoinSet`], and collects
/// results as they complete — fast peers are not delayed by slow ones.
///
/// Each individual `join_next` call is wrapped in a 500 ms timeout so
/// that the overall method never blocks the download loop for long.
/// Peers whose connection attempt is still in-flight when the timeout
/// fires are re-enqueued into [`Self::pending`] for the next tick.
///
/// Peers still in cooldown are skipped. Failed peers are re-enqueued
/// with a per-peer cooldown, up to [`MAX_RETRIES`] times.
pub async fn connect_pending(&mut self) -> Vec<SocketAddr> {
let batch_size = (self.max_connections as usize).saturating_sub(self.connections.len());
let raw_batch: Vec<SocketAddr> = self.pending.drain_first_n(batch_size);
if raw_batch.is_empty() {
return vec![];
}
let now = Instant::now();
let mut batch = Vec::with_capacity(raw_batch.len());
for addr in raw_batch {
if let Some(state) = self.backoff.get(&addr) {
if state.cooldown_until > now {
let re_enqueued = self.pending.push_unique(addr);
debug_assert!(
re_enqueued,
"cooldown peer {} unexpectedly already in pending set",
addr
);
continue;
}
}
batch.push(addr);
}
if batch.is_empty() {
return vec![];
}
// Spawn all connection attempts concurrently via JoinSet.
let mut joinset = JoinSet::new();
for &addr in &batch {
let info_hash = self.info_hash;
let peer_id = self.peer_id;
joinset.spawn(async move {
let result = PeerConnection::connect(addr, info_hash, peer_id).await;
(addr, result)
});
}
let per_call_timeout = self.connect_timeout;
let mut connected = Vec::new();
let mut processed: HashSet<SocketAddr> = HashSet::new();
loop {
match tokio::time::timeout(per_call_timeout, joinset.join_next()).await {
Ok(Some(Ok((addr, Ok(conn))))) => {
processed.insert(addr);
tracing::info!("peer connected: {}", addr);
self.connections.insert(addr, Arc::new(conn));
self.backoff.remove(&addr);
connected.push(addr);
}
Ok(Some(Ok((addr, Err(_))))) => {
processed.insert(addr);
let state = self
.backoff
.entry(addr)
.or_insert_with(|| BackoffState::new(self.cooldown));
if state.attempts < self.max_retries {
state.increment(self.cooldown);
tracing::debug!(
"re-enqueuing peer {} (attempt {}/{}, cooldown {}s)",
addr,
state.attempts,
self.max_retries,
self.cooldown.as_secs()
);
let re_enqueued = self.pending.push_unique(addr);
debug_assert!(
re_enqueued,
"retried peer {} unexpectedly already in pending set",
addr
);
} else {
tracing::debug!("peer {}: max retries reached, discarding", addr);
self.backoff.remove(&addr);
}
}
Ok(Some(Err(e))) => {
tracing::error!("peer connection task panicked: {}", e);
}
Ok(None) => break, // all tasks completed
Err(_) => break, // per-call timeout — remaining tasks still running
}
}
// Re-enqueue peers whose tasks are still in-flight (timeout path)
// so they are retried on the next tick.
for addr in &batch {
if !processed.contains(addr) {
self.pending.push_unique(*addr);
}
}
connected
}
/// Get a clone of the connection Arc for a peer.
pub fn connection(&self, addr: &SocketAddr) -> Option<Arc<PeerConnection>> {
self.connections.get(addr).cloned()
}
/// Get all connected peer addresses.
pub fn connection_addrs(&self) -> Vec<SocketAddr> {
self.connections.keys().copied().collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test_pm(max_connections: u32) -> PeerManager {
PeerManager::new(
[0u8; 20],
PeerId::random(),
max_connections,
Duration::from_millis(500),
3,
Duration::from_secs(30),
)
}
fn test_addr(n: u8) -> SocketAddr {
SocketAddr::new(
std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, n)),
6881,
)
}
#[test]
fn new_creates_empty() {
let pm = test_pm(10);
assert_eq!(pm.num_connections(), 0);
assert!(pm.connection_addrs().is_empty());
}
#[test]
fn add_peers_to_pending() {
let mut pm = test_pm(10);
pm.add_peers(vec![test_addr(1), test_addr(2)]);
// connect_next() will attempt to connect; at this point they're pending
// We can verify by checking that connection_addrs is still empty
assert_eq!(pm.num_connections(), 0);
}
#[test]
fn at_capacity_precondition() {
let pm = PeerManager {
peer_id: PeerId::random(),
info_hash: [0u8; 20],
connections: HashMap::new(),
pending: {
let mut d = UniDeque::new();
d.push_unique(test_addr(1));
d
},
max_connections: 0,
backoff: HashMap::new(),
connect_timeout: Duration::from_millis(500),
max_retries: 3,
cooldown: Duration::from_secs(30),
};
// Precondition: capacity 0 means no connections will be attempted
assert_eq!(pm.max_connections, 0);
}
#[test]
fn remove_peer_nonexistent() {
let mut pm = test_pm(10);
pm.remove_peer(&test_addr(99)); // should not panic
assert_eq!(pm.num_connections(), 0);
}
#[test]
fn connection_addrs_empty() {
let pm = test_pm(10);
assert!(pm.connection_addrs().is_empty());
}
}