nzb-nntp 0.2.17

Async NNTP client with TLS, pipelining, connection pooling, and multi-server support
Documentation
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Per-server connection pool.
//!
//! Manages a configurable number of NNTP connections for a single server.
//! Connections are created on demand up to the configured limit, returned
//! to the pool after use, and replaced when they become unhealthy.

use std::sync::Arc;
use std::time::{Duration, Instant};

use parking_lot::Mutex;
use tokio::sync::Semaphore;
use tracing::{debug, info, trace, warn};

use crate::config::ServerConfig;

use crate::connection::{ConnectionState, NntpConnection};
use crate::error::{NntpError, NntpResult};

/// How long an idle connection can sit before we health-check it.
const IDLE_TIMEOUT: Duration = Duration::from_secs(60);

/// How long to wait for a connection from the pool before giving up.
const ACQUIRE_TIMEOUT: Duration = Duration::from_secs(30);

// ---------------------------------------------------------------------------
// Pooled connection wrapper
// ---------------------------------------------------------------------------

/// A connection checked out from the pool, with metadata.
///
/// When dropped without being returned via `release()` or `discard()`, the
/// semaphore permit is automatically freed so the pool slot is not leaked.
pub struct PooledConnection {
    /// The underlying NNTP connection.
    pub conn: NntpConnection,
    /// When this connection last completed an operation.
    pub last_used: Instant,
    /// Semaphore reference for automatic permit release on drop.
    semaphore: Option<Arc<Semaphore>>,
}

impl PooledConnection {
    /// Create a new pooled connection not tied to any pool's semaphore.
    /// Used by the downloader which manages its own connection lifecycle.
    pub fn unmanaged(conn: NntpConnection) -> Self {
        Self {
            conn,
            last_used: Instant::now(),
            semaphore: None,
        }
    }
}

impl Drop for PooledConnection {
    fn drop(&mut self) {
        if let Some(sem) = self.semaphore.take() {
            warn!(
                conn_id = %self.conn.server_id,
                "PooledConnection dropped without release/discard — freeing permit"
            );
            sem.add_permits(1);
        }
    }
}

// ---------------------------------------------------------------------------
// Connection pool
// ---------------------------------------------------------------------------

/// Per-server connection pool.
///
/// The pool holds up to `max_connections` connections. Callers `acquire()` a
/// connection, use it, then `release()` it back. Connections that have entered
/// an error state are discarded rather than returned to the pool.
pub struct ConnectionPool {
    /// Server configuration (immutable reference data).
    config: Arc<ServerConfig>,
    /// Idle connections ready to be handed out.
    idle: Mutex<Vec<PooledConnection>>,
    /// Semaphore limiting total connections (idle + checked-out).
    semaphore: Arc<Semaphore>,
    /// Total connections ever created (for naming/debug).
    created_count: Mutex<u32>,
    /// Timestamp of the last new connection creation (for ramp-up staggering).
    /// Uses `tokio::sync::Mutex` because it is held across an `.await` (the sleep).
    last_connect: tokio::sync::Mutex<Instant>,
    /// Minimum delay between new connection creations.
    ramp_up_delay: Duration,
}

impl ConnectionPool {
    /// Create a new pool for the given server. No connections are opened yet.
    pub fn new(config: Arc<ServerConfig>) -> Self {
        let max_conns = config.connections as usize;
        let ramp_up_delay = Duration::from_millis(config.ramp_up_delay_ms as u64);
        Self {
            idle: Mutex::new(Vec::with_capacity(max_conns)),
            semaphore: Arc::new(Semaphore::new(max_conns)),
            created_count: Mutex::new(0),
            // Allow the first connection immediately
            last_connect: tokio::sync::Mutex::new(Instant::now() - Duration::from_secs(60)),
            ramp_up_delay,
            config,
        }
    }

    /// Acquire a connected, ready connection from the pool.
    ///
    /// If an idle connection is available it is returned immediately (after a
    /// health check). Otherwise a new connection is created, up to the server
    /// limit. If all connection slots are in use, this waits until one is
    /// released.
    pub async fn acquire(&self) -> NntpResult<PooledConnection> {
        let available_permits = self.semaphore.available_permits();
        let idle_count = self.idle.lock().len();
        debug!(
            server = %self.config.name,
            available_permits,
            idle_count,
            max_conns = self.config.connections,
            "Pool acquire: waiting for slot"
        );

        // Wait for a connection slot
        let permit = tokio::time::timeout(ACQUIRE_TIMEOUT, self.semaphore.clone().acquire_owned())
            .await
            .map_err(|_| {
                warn!(
                    server = %self.config.name,
                    available_permits,
                    idle_count,
                    max_conns = self.config.connections,
                    "Pool acquire TIMED OUT after {}s — all {} slots busy",
                    ACQUIRE_TIMEOUT.as_secs(),
                    self.config.connections
                );
                NntpError::Timeout(format!(
                    "Timed out waiting for connection to {}",
                    self.config.name
                ))
            })?
            .map_err(|_| {
                NntpError::Connection(format!(
                    "Connection pool for {} is closed",
                    self.config.name
                ))
            })?;

        // Try to reuse an idle connection
        let maybe_idle = { self.idle.lock().pop() };

        if let Some(mut pooled) = maybe_idle {
            // Arm the drop guard so the permit is freed if this connection
            // is dropped without release/discard.
            pooled.semaphore = Some(Arc::clone(&self.semaphore));
            // Health check: if the connection is in a bad state, discard and make new
            if pooled.conn.state == ConnectionState::Ready && pooled.conn.is_connected() {
                // If idle too long, do a quick liveness check
                if pooled.last_used.elapsed() > IDLE_TIMEOUT {
                    info!(
                        server = %self.config.name,
                        conn_id = %pooled.conn.server_id,
                        idle_secs = pooled.last_used.elapsed().as_secs(),
                        "Pool: idle connection stale — health checking"
                    );
                    // STAT a bogus message-id; 430 = alive, I/O error = dead
                    match pooled.conn.stat_article("<health-check@pool>").await {
                        Ok(_) | Err(NntpError::ArticleNotFound(_)) => {
                            // Connection is alive
                            debug!(
                                server = %self.config.name,
                                conn_id = %pooled.conn.server_id,
                                "Pool: health check passed, reusing"
                            );
                            pooled.last_used = Instant::now();
                            permit.forget(); // slot is now checked out
                            return Ok(pooled);
                        }
                        Err(e) => {
                            warn!(
                                server = %self.config.name,
                                conn_id = %pooled.conn.server_id,
                                error = %e,
                                "Pool: idle connection FAILED health check — creating new"
                            );
                            // Fall through to create a new one
                        }
                    }
                } else {
                    debug!(
                        server = %self.config.name,
                        conn_id = %pooled.conn.server_id,
                        idle_secs = pooled.last_used.elapsed().as_secs(),
                        "Pool: reusing idle connection"
                    );
                    permit.forget();
                    return Ok(pooled);
                }
            } else {
                warn!(
                    server = %self.config.name,
                    conn_id = %pooled.conn.server_id,
                    state = ?pooled.conn.state,
                    connected = pooled.conn.is_connected(),
                    "Pool: idle connection in bad state — creating new"
                );
            }
            // Connection is broken — fall through to create new
        }

        // Create a new connection
        info!(
            server = %self.config.name,
            "Pool: no reusable connection, creating new"
        );
        let conn = self.create_connection().await?;
        permit.forget(); // slot is now checked out

        Ok(PooledConnection {
            conn,
            last_used: Instant::now(),
            semaphore: Some(Arc::clone(&self.semaphore)),
        })
    }

    /// Return a connection to the pool after use.
    ///
    /// If the connection is still healthy it goes back to the idle list.
    /// If it is in an error state it is dropped and the slot is freed.
    pub fn release(&self, mut pooled: PooledConnection) {
        // Disarm the drop guard — we're handling the permit ourselves.
        pooled.semaphore = None;
        if pooled.conn.state == ConnectionState::Ready && pooled.conn.is_connected() {
            pooled.last_used = Instant::now();
            let idle_after = {
                let mut idle = self.idle.lock();
                idle.push(pooled);
                idle.len()
            };
            // Restore the semaphore permit so the next acquire() can proceed.
            self.semaphore.add_permits(1);
            debug!(
                server = %self.config.name,
                idle_count = idle_after,
                "Pool: connection released back to idle"
            );
        } else {
            warn!(
                server = %self.config.name,
                conn_id = %pooled.conn.server_id,
                state = ?pooled.conn.state,
                connected = pooled.conn.is_connected(),
                "Pool: discarding unhealthy connection on release"
            );
            // Drop the connection; free the semaphore slot
            drop(pooled);
            self.semaphore.add_permits(1);
        }
    }

    /// Discard a connection (e.g. after a fatal error) and free the slot.
    pub fn discard(&self, mut pooled: PooledConnection) {
        // Disarm the drop guard — we'll add the permit back explicitly.
        pooled.semaphore = None;
        info!(
            server = %self.config.name,
            conn_id = %pooled.conn.server_id,
            state = ?pooled.conn.state,
            "Pool: discarding connection (fatal error)"
        );
        drop(pooled);
        self.semaphore.add_permits(1);
    }

    /// Number of idle connections currently in the pool.
    pub fn idle_count(&self) -> usize {
        self.idle.lock().len()
    }

    /// Close all idle connections. In-use connections are unaffected.
    pub async fn close_idle(&self) {
        let conns: Vec<PooledConnection> = {
            let mut idle = self.idle.lock();
            idle.drain(..).collect()
        };
        let count = conns.len();
        for mut c in conns {
            let _ = c.conn.quit().await;
            self.semaphore.add_permits(1);
        }
        if count > 0 {
            debug!(server = %self.config.name, count, "Closed idle connections");
        }
    }

    /// The server configuration for this pool.
    pub fn config(&self) -> &ServerConfig {
        &self.config
    }

    /// Number of available semaphore permits (for testing).
    #[cfg(test)]
    pub(crate) fn available_permits(&self) -> usize {
        self.semaphore.available_permits()
    }

    /// Wait for the ramp-up delay to elapse since the last connection was opened.
    ///
    /// Call this before creating a connection outside the pool (e.g. in download
    /// engine workers) to respect ramp-up timing and avoid connection bursts.
    pub async fn wait_for_ramp_up(&self) {
        if self.ramp_up_delay.is_zero() {
            return;
        }
        let mut last = self.last_connect.lock().await;
        let elapsed = last.elapsed();
        if elapsed < self.ramp_up_delay {
            let wait = self.ramp_up_delay - elapsed;
            trace!(
                server = %self.config.name,
                wait_ms = wait.as_millis(),
                "Ramp-up: waiting before new connection"
            );
            tokio::time::sleep(wait).await;
        }
        *last = Instant::now();
    }

    /// The configured ramp-up delay between new connections.
    pub fn ramp_up_delay(&self) -> Duration {
        self.ramp_up_delay
    }

    // ------------------------------------------------------------------
    // Internal
    // ------------------------------------------------------------------

    /// Create and connect a new NNTP connection.
    ///
    /// Applies ramp-up delay to stagger connection establishment and avoid
    /// bursting the server with simultaneous TCP+TLS handshakes.
    async fn create_connection(&self) -> NntpResult<NntpConnection> {
        // Enforce ramp-up delay between new connections
        self.wait_for_ramp_up().await;

        let idx = {
            let mut count = self.created_count.lock();
            *count += 1;
            *count
        };

        let conn_id = format!("{}#{}", self.config.id, idx);
        info!(
            server = %self.config.name,
            conn_id = %conn_id,
            host = %self.config.host,
            port = self.config.port,
            total_created = idx,
            "Pool: creating new NNTP connection"
        );

        let mut conn = NntpConnection::new(conn_id.clone());
        conn.connect(&self.config).await.inspect_err(|e| {
            warn!(
                server = %self.config.name,
                conn_id = %conn_id,
                error = %e,
                "Pool: new connection FAILED"
            );
            // Free the semaphore slot since we failed
            self.semaphore.add_permits(1);
        })?;

        info!(
            server = %self.config.name,
            conn_id = %conn_id,
            "Pool: new connection ready"
        );
        Ok(conn)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testutil::{MockConfig, MockNntpServer, test_config};

    #[tokio::test]
    async fn test_pool_new() {
        let config = Arc::new(test_config(12345));
        let pool = ConnectionPool::new(config.clone());
        assert_eq!(pool.idle_count(), 0);
        assert_eq!(pool.config().id, "test-server");
        assert_eq!(pool.available_permits(), 4); // connections = 4
    }

    #[tokio::test]
    async fn test_pool_acquire_creates_connection() {
        let server = MockNntpServer::start(MockConfig::default()).await;
        let config = Arc::new(test_config(server.port()));
        let pool = ConnectionPool::new(config);

        let pooled = pool.acquire().await.unwrap();
        assert_eq!(pooled.conn.state, ConnectionState::Ready);
        assert!(pooled.conn.is_connected());
        assert_eq!(pool.idle_count(), 0);

        // Release it back
        pool.release(pooled);
        assert_eq!(pool.idle_count(), 1);
    }

    #[tokio::test]
    async fn test_pool_release_and_reuse() {
        let server = MockNntpServer::start(MockConfig::default()).await;
        let config = Arc::new(test_config(server.port()));
        let pool = ConnectionPool::new(config);

        // Acquire and release
        let pooled = pool.acquire().await.unwrap();
        let first_id = pooled.conn.server_id.clone();
        pool.release(pooled);
        assert_eq!(pool.idle_count(), 1);

        // Acquire again — should reuse the idle connection
        let pooled = pool.acquire().await.unwrap();
        assert_eq!(pooled.conn.server_id, first_id);
        assert_eq!(pool.idle_count(), 0);

        pool.release(pooled);
    }

    #[tokio::test]
    async fn test_pool_discard_frees_slot() {
        let server = MockNntpServer::start(MockConfig::default()).await;
        let mut cfg = test_config(server.port());
        cfg.connections = 2;
        let pool = ConnectionPool::new(Arc::new(cfg));

        let c1 = pool.acquire().await.unwrap();
        let c2 = pool.acquire().await.unwrap();
        assert_eq!(pool.available_permits(), 0);

        // Discard one — frees a permit
        pool.discard(c1);
        assert_eq!(pool.available_permits(), 1);
        assert_eq!(pool.idle_count(), 0);

        pool.release(c2);
    }

    #[tokio::test]
    async fn test_pool_close_idle() {
        let server = MockNntpServer::start(MockConfig::default()).await;
        let config = Arc::new(test_config(server.port()));
        let pool = ConnectionPool::new(config);

        // Create and release two connections
        let c1 = pool.acquire().await.unwrap();
        let c2 = pool.acquire().await.unwrap();
        pool.release(c1);
        pool.release(c2);
        assert_eq!(pool.idle_count(), 2);

        // Close all idle
        pool.close_idle().await;
        assert_eq!(pool.idle_count(), 0);
    }

    #[tokio::test]
    async fn test_pool_idle_count_tracking() {
        let server = MockNntpServer::start(MockConfig::default()).await;
        let config = Arc::new(test_config(server.port()));
        let pool = ConnectionPool::new(config);

        assert_eq!(pool.idle_count(), 0);

        let c1 = pool.acquire().await.unwrap();
        assert_eq!(pool.idle_count(), 0);

        let c2 = pool.acquire().await.unwrap();
        assert_eq!(pool.idle_count(), 0);

        pool.release(c1);
        assert_eq!(pool.idle_count(), 1);

        pool.release(c2);
        assert_eq!(pool.idle_count(), 2);
    }
}