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
//! Server health tracking, penalties, connection management, and speed tracking.

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

use tracing::{debug, info, warn};

use crate::config::ServerConfig;

use crate::error::NntpResult;
use crate::pool::{ConnectionPool, PooledConnection};

/// Penalty durations for various error conditions.
const PENALTY_UNKNOWN: Duration = Duration::from_secs(3 * 60);
const PENALTY_TIMEOUT: Duration = Duration::from_secs(10 * 60);
const PENALTY_AUTH: Duration = Duration::from_secs(10 * 60);

/// Number of speed samples to keep for the rolling average.
const SPEED_WINDOW_SIZE: usize = 30;

// ---------------------------------------------------------------------------
// Speed tracker
// ---------------------------------------------------------------------------

/// A sample of bytes downloaded during a time period.
struct SpeedSample {
    bytes: u64,
    timestamp: Instant,
}

/// Tracks download speed as a rolling average over recent samples.
struct SpeedTracker {
    samples: VecDeque<SpeedSample>,
    window: Duration,
}

impl SpeedTracker {
    fn new() -> Self {
        Self {
            samples: VecDeque::with_capacity(SPEED_WINDOW_SIZE + 1),
            window: Duration::from_secs(10),
        }
    }

    /// Record that `bytes` were downloaded at this moment.
    fn record(&mut self, bytes: u64) {
        let now = Instant::now();
        self.samples.push_back(SpeedSample {
            bytes,
            timestamp: now,
        });

        // Remove samples older than the window
        let cutoff = now - self.window;
        while self.samples.front().is_some_and(|s| s.timestamp < cutoff) {
            self.samples.pop_front();
        }
    }

    /// Current speed in bytes per second.
    fn bytes_per_second(&self) -> f64 {
        if self.samples.len() < 2 {
            return 0.0;
        }

        let first = self.samples.front().unwrap();
        let last = self.samples.back().unwrap();

        let elapsed = last.timestamp.duration_since(first.timestamp).as_secs_f64();
        if elapsed < 0.001 {
            return 0.0;
        }

        let total_bytes: u64 = self.samples.iter().map(|s| s.bytes).sum();
        total_bytes as f64 / elapsed
    }
}

// ---------------------------------------------------------------------------
// ServerState
// ---------------------------------------------------------------------------

/// Runtime state for a configured NNTP server.
pub struct ServerState {
    pub config: Arc<ServerConfig>,
    pub active: bool,
    pub connections_active: u16,
    pub articles_tried: u64,
    pub articles_failed: u64,
    pub bytes_downloaded: u64,
    /// When the server penalty expires (None = no penalty).
    pub penalty_until: Option<Instant>,
    pub last_error: Option<String>,
    /// Connection pool for this server.
    pool: ConnectionPool,
    /// Download speed tracker.
    speed: SpeedTracker,
}

impl ServerState {
    /// Create a new server state, including its connection pool.
    pub fn new(config: ServerConfig) -> Self {
        let config = Arc::new(config);
        let pool = ConnectionPool::new(Arc::clone(&config));
        Self {
            active: config.enabled,
            connections_active: 0,
            articles_tried: 0,
            articles_failed: 0,
            bytes_downloaded: 0,
            penalty_until: None,
            last_error: None,
            pool,
            speed: SpeedTracker::new(),
            config,
        }
    }

    /// Check if the server is available (active and not penalized).
    pub fn is_available(&self) -> bool {
        self.active
            && self
                .penalty_until
                .is_none_or(|until| Instant::now() > until)
    }

    /// Apply a penalty to the server.
    pub fn penalize(&mut self, reason: &str, duration: Duration) {
        self.penalty_until = Some(Instant::now() + duration);
        self.last_error = Some(reason.to_string());
        warn!(
            server = %self.config.name,
            reason = %reason,
            duration_secs = duration.as_secs(),
            "Server penalized"
        );
    }

    /// Apply the appropriate penalty for a given error condition.
    pub fn penalize_for(&mut self, reason: &str) {
        let duration = if reason.contains("auth") || reason.contains("Auth") {
            info!(
                server = %self.config.name,
                host = %self.config.host,
                reason = %reason,
                "Applying AUTH penalty (10 min)"
            );
            PENALTY_AUTH
        } else if reason.contains("timeout") || reason.contains("Timeout") {
            info!(
                server = %self.config.name,
                host = %self.config.host,
                reason = %reason,
                "Applying TIMEOUT penalty (10 min)"
            );
            PENALTY_TIMEOUT
        } else {
            info!(
                server = %self.config.name,
                host = %self.config.host,
                reason = %reason,
                "Applying UNKNOWN penalty (3 min)"
            );
            PENALTY_UNKNOWN
        };
        self.penalize(reason, duration);
    }

    /// Clear the penalty.
    pub fn clear_penalty(&mut self) {
        if self.penalty_until.is_some() {
            info!(
                server = %self.config.name,
                host = %self.config.host,
                "Server penalty cleared"
            );
        }
        self.penalty_until = None;
        self.last_error = None;
    }

    /// Record a successful article download.
    pub fn record_success(&mut self, bytes: u64) {
        self.articles_tried += 1;
        self.bytes_downloaded += bytes;
        self.speed.record(bytes);
    }

    /// Record a failed article download.
    pub fn record_failure(&mut self) {
        self.articles_tried += 1;
        self.articles_failed += 1;
    }

    /// Failure ratio (0.0 to 1.0).
    pub fn failure_ratio(&self) -> f64 {
        if self.articles_tried == 0 {
            0.0
        } else {
            self.articles_failed as f64 / self.articles_tried as f64
        }
    }

    /// Current download speed in bytes per second (rolling average).
    pub fn speed_bps(&self) -> f64 {
        self.speed.bytes_per_second()
    }

    // ------------------------------------------------------------------
    // Pool integration
    // ------------------------------------------------------------------

    /// Acquire a connection from this server's pool.
    pub async fn acquire_connection(&mut self) -> NntpResult<PooledConnection> {
        debug!(
            server = %self.config.name,
            connections_active = self.connections_active,
            max_conns = self.config.connections,
            "Server: acquiring connection"
        );
        let conn = self.pool.acquire().await?;
        self.connections_active += 1;
        debug!(
            server = %self.config.name,
            connections_active = self.connections_active,
            "Server: connection acquired"
        );
        Ok(conn)
    }

    /// Return a connection to this server's pool.
    pub fn release_connection(&mut self, conn: PooledConnection) {
        self.pool.release(conn);
        self.connections_active = self.connections_active.saturating_sub(1);
        debug!(
            server = %self.config.name,
            connections_active = self.connections_active,
            "Server: connection released"
        );
    }

    /// Discard a broken connection (frees the pool slot).
    pub fn discard_connection(&mut self, conn: PooledConnection) {
        info!(
            server = %self.config.name,
            connections_active = self.connections_active,
            "Server: discarding broken connection"
        );
        self.pool.discard(conn);
        self.connections_active = self.connections_active.saturating_sub(1);
    }

    /// Close all idle connections in the pool.
    pub async fn close_idle_connections(&self) {
        self.pool.close_idle().await;
    }

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

    /// Wait for the ramp-up delay (for callers creating connections outside the pool).
    pub async fn wait_for_ramp_up(&self) {
        self.pool.wait_for_ramp_up().await;
    }

    /// Access the underlying pool directly.
    pub fn pool(&self) -> &ConnectionPool {
        &self.pool
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::ServerConfig;

    fn make_config() -> ServerConfig {
        ServerConfig {
            id: "srv-1".into(),
            name: "Test NNTP".into(),
            host: "127.0.0.1".into(),
            port: 563,
            ssl: false,
            ssl_verify: false,
            username: None,
            password: None,
            connections: 4,
            priority: 0,
            enabled: true,
            retention: 0,
            pipelining: 1,
            optional: false,
            compress: false,
            ramp_up_delay_ms: 0,
            recv_buffer_size: 0,
            proxy_url: None,
            trusted_fingerprint: None,
        }
    }

    #[test]
    fn test_new_server_state() {
        let state = ServerState::new(make_config());
        assert!(state.active);
        assert_eq!(state.connections_active, 0);
        assert_eq!(state.articles_tried, 0);
        assert_eq!(state.articles_failed, 0);
        assert_eq!(state.bytes_downloaded, 0);
        assert!(state.penalty_until.is_none());
        assert!(state.last_error.is_none());
        assert_eq!(state.config.id, "srv-1");
    }

    #[test]
    fn test_new_disabled_server() {
        let mut cfg = make_config();
        cfg.enabled = false;
        let state = ServerState::new(cfg);
        assert!(!state.active);
        assert!(!state.is_available());
    }

    #[test]
    fn test_is_available_active_no_penalty() {
        let state = ServerState::new(make_config());
        assert!(state.is_available());
    }

    #[test]
    fn test_is_available_penalized() {
        let mut state = ServerState::new(make_config());
        state.penalize("test error", Duration::from_secs(60));
        assert!(!state.is_available());
        assert!(state.penalty_until.is_some());
        assert_eq!(state.last_error.as_deref(), Some("test error"));
    }

    #[test]
    fn test_penalize_expired() {
        let mut state = ServerState::new(make_config());
        // Penalize with zero duration — immediately expired
        state.penalty_until = Some(Instant::now() - Duration::from_secs(1));
        assert!(state.is_available());
    }

    #[test]
    fn test_penalize_for_auth() {
        let mut state = ServerState::new(make_config());
        state.penalize_for("Authentication failed");
        assert!(!state.is_available());
        // Auth penalty should be 10 minutes
        let until = state.penalty_until.unwrap();
        let remaining = until.duration_since(Instant::now());
        assert!(remaining > Duration::from_secs(500)); // ~10 min
    }

    #[test]
    fn test_penalize_for_timeout() {
        let mut state = ServerState::new(make_config());
        state.penalize_for("Connection timeout");
        let until = state.penalty_until.unwrap();
        let remaining = until.duration_since(Instant::now());
        assert!(remaining > Duration::from_secs(500));
    }

    #[test]
    fn test_penalize_for_unknown() {
        let mut state = ServerState::new(make_config());
        state.penalize_for("some random error");
        let until = state.penalty_until.unwrap();
        let remaining = until.duration_since(Instant::now());
        // Unknown = 3 minutes
        assert!(remaining > Duration::from_secs(150));
        assert!(remaining < Duration::from_secs(200));
    }

    #[test]
    fn test_clear_penalty() {
        let mut state = ServerState::new(make_config());
        state.penalize("error", Duration::from_secs(600));
        assert!(!state.is_available());

        state.clear_penalty();
        assert!(state.is_available());
        assert!(state.penalty_until.is_none());
        assert!(state.last_error.is_none());
    }

    #[test]
    fn test_record_success() {
        let mut state = ServerState::new(make_config());
        state.record_success(50000);
        assert_eq!(state.articles_tried, 1);
        assert_eq!(state.articles_failed, 0);
        assert_eq!(state.bytes_downloaded, 50000);

        state.record_success(30000);
        assert_eq!(state.articles_tried, 2);
        assert_eq!(state.bytes_downloaded, 80000);
    }

    #[test]
    fn test_record_failure() {
        let mut state = ServerState::new(make_config());
        state.record_failure();
        assert_eq!(state.articles_tried, 1);
        assert_eq!(state.articles_failed, 1);
    }

    #[test]
    fn test_failure_ratio() {
        let mut state = ServerState::new(make_config());
        assert_eq!(state.failure_ratio(), 0.0);

        state.record_success(1000);
        state.record_success(1000);
        state.record_failure();
        // 1 failure out of 3 tries
        let ratio = state.failure_ratio();
        assert!((ratio - 1.0 / 3.0).abs() < 0.001);
    }

    #[test]
    fn test_failure_ratio_all_failed() {
        let mut state = ServerState::new(make_config());
        state.record_failure();
        state.record_failure();
        assert_eq!(state.failure_ratio(), 1.0);
    }

    #[test]
    fn test_speed_bps_no_samples() {
        let state = ServerState::new(make_config());
        assert_eq!(state.speed_bps(), 0.0);
    }

    #[test]
    fn test_idle_connection_count() {
        let state = ServerState::new(make_config());
        assert_eq!(state.idle_connection_count(), 0);
    }
}