etchdns 0.2.1

Caching DNS proxy and forwarding server with load balancing, failover, domain filtering, and WebAssembly plugins
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
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};
use tokio::sync::Mutex;

/// Unanswered queries in a row before a resolver is parked.
///
/// Even a healthy resolver often can't answer for a broken domain, so one or two misses
/// shouldn't be enough.
pub(crate) const PARK_AFTER_FAILURES: u64 = 3;

/// How long a resolver stays parked.
/// Each new failure doubles it, up to the maximum.
const MIN_PARK_DURATION: Duration = Duration::from_secs(5);
const MAX_PARK_DURATION: Duration = Duration::from_secs(60);

fn park_duration(consecutive_failures: u64) -> Duration {
    let doublings = consecutive_failures
        .saturating_sub(PARK_AFTER_FAILURES)
        .min(16) as u32;
    MIN_PARK_DURATION
        .saturating_mul(1 << doublings)
        .min(MAX_PARK_DURATION)
}

/// Statistics for a single resolver
#[derive(Debug, Clone)]
pub struct ResolverStats {
    /// Moving average response time in milliseconds
    pub avg_response_time_ms: f64,
    /// Number of successful queries
    pub success_count: u64,
    /// Number of failed queries
    pub failure_count: u64,
    /// Number of timed out queries
    pub timeout_count: u64,
    /// Failures and timeouts since the resolver last answered
    pub consecutive_failures: u64,
    /// Last time this resolver was used
    pub last_used: SystemTime,
    /// Weight factor for the moving average (between 0 and 1)
    /// Lower values give more weight to historical data
    weight_factor: f64,
    /// The resolver gets no queries until then
    parked_until: Option<Instant>,
}

impl ResolverStats {
    /// Create a new ResolverStats with default values
    pub fn new() -> Self {
        Self {
            avg_response_time_ms: 0.0,
            success_count: 0,
            failure_count: 0,
            timeout_count: 0,
            consecutive_failures: 0,
            last_used: SystemTime::now(),
            weight_factor: 0.2, // 20% weight to new values
            parked_until: None,
        }
    }

    /// Update the moving average response time
    pub fn update_response_time(&mut self, response_time: Duration) {
        // Keep sub-millisecond precision, since 0.0 means that nothing was measured yet
        let response_time_ms = response_time.as_nanos() as f64 / 1_000_000.0;

        if self.avg_response_time_ms == 0.0 {
            // First measurement
            self.avg_response_time_ms = response_time_ms;
        } else {
            // Update moving average
            self.avg_response_time_ms = (1.0 - self.weight_factor) * self.avg_response_time_ms
                + self.weight_factor * response_time_ms;
        }
    }

    /// Record a successful query
    pub fn record_success(&mut self, response_time: Duration) {
        self.success_count = self.success_count.saturating_add(1);
        self.clear_failures();
        self.update_response_time(response_time);
    }

    /// Record a failed query
    pub fn record_failure(&mut self) {
        self.failure_count = self.failure_count.saturating_add(1);
        self.add_failure();
    }

    /// Record a timed out query
    pub fn record_timeout(&mut self) {
        self.timeout_count = self.timeout_count.saturating_add(1);
        self.add_failure();
    }

    /// Record an answer that can't be timed, such as the reply to a retry
    pub fn clear_failures(&mut self) {
        self.consecutive_failures = 0;
        self.parked_until = None;
        self.last_used = SystemTime::now();
    }

    fn add_failure(&mut self) {
        self.consecutive_failures = self.consecutive_failures.saturating_add(1);
        self.last_used = SystemTime::now();
        if self.consecutive_failures >= PARK_AFTER_FAILURES {
            self.parked_until = Some(Instant::now() + park_duration(self.consecutive_failures));
        }
    }

    fn is_parked(&self) -> bool {
        self.parked_until
            .is_some_and(|until| Instant::now() < until)
    }

    fn record_query_sent(&mut self) {
        if self.parked_until.is_some() && !self.is_parked() {
            self.parked_until = Some(Instant::now() + park_duration(self.consecutive_failures));
        }
    }
}

impl Default for ResolverStats {
    fn default() -> Self {
        Self::new()
    }
}

/// Global statistics for the DNS server
#[derive(Debug, Clone)]
pub struct GlobalStats {
    /// Total number of queries processed
    pub total_queries: u64,
    /// Total number of successful queries
    pub total_successful: u64,
    /// Total number of failed queries
    pub total_failed: u64,
    /// Total number of timed out queries
    pub total_timeouts: u64,
    /// Total number of client queries received
    pub client_queries: u64,
    /// Total number of cache hits
    pub cache_hits: u64,
    /// Total number of cache misses
    pub cache_misses: u64,
    /// Count of UDP receive errors
    pub udp_receive_errors: u64,
    /// Count of TCP accept errors
    pub tcp_accept_errors: u64,
    /// Map of resolver addresses to their stats
    pub resolver_stats: HashMap<SocketAddr, ResolverStats>,
}

impl GlobalStats {
    /// Create a new GlobalStats with default values
    pub fn new() -> Self {
        Self {
            total_queries: 0,
            total_successful: 0,
            total_failed: 0,
            total_timeouts: 0,
            client_queries: 0,
            cache_hits: 0,
            cache_misses: 0,
            udp_receive_errors: 0,
            tcp_accept_errors: 0,
            resolver_stats: HashMap::new(),
        }
    }

    /// Record a successful query for a resolver
    pub fn record_success(&mut self, resolver: SocketAddr, response_time: Duration) {
        self.total_queries = self.total_queries.saturating_add(1);
        self.total_successful = self.total_successful.saturating_add(1);

        let stats = self.resolver_stats.entry(resolver).or_default();
        stats.record_success(response_time);
    }

    /// Record a failed query for a resolver
    pub fn record_failure(&mut self, resolver: SocketAddr) {
        self.total_queries = self.total_queries.saturating_add(1);
        self.total_failed = self.total_failed.saturating_add(1);

        let stats = self.resolver_stats.entry(resolver).or_default();
        stats.record_failure();
    }

    /// Record a timed out query for a resolver
    pub fn record_timeout(&mut self, resolver: SocketAddr) {
        self.total_queries = self.total_queries.saturating_add(1);
        self.total_timeouts = self.total_timeouts.saturating_add(1);

        let stats = self.resolver_stats.entry(resolver).or_default();
        stats.record_timeout();
    }

    /// Record an answer that can't be timed, such as the reply to a retry
    pub fn clear_failures(&mut self, resolver: SocketAddr) {
        if let Some(stats) = self.resolver_stats.get_mut(&resolver) {
            stats.clear_failures();
        }
    }

    /// Record that a query is about to be sent to a resolver.
    ///
    /// If the resolver's parking time is over, this query is a trial.
    /// Other queries stay away until we know how it went, in case the resolver is still down.
    pub fn record_query_sent(&mut self, resolver: SocketAddr) {
        if let Some(stats) = self.resolver_stats.get_mut(&resolver) {
            stats.record_query_sent();
        }
    }

    /// Get statistics for a specific resolver
    pub fn get_resolver_stats(&self, resolver: &SocketAddr) -> Option<&ResolverStats> {
        self.resolver_stats.get(resolver)
    }

    /// Record a client query
    pub fn record_client_query(&mut self) {
        self.client_queries = self.client_queries.saturating_add(1);
    }

    /// Record a cache hit
    pub fn record_cache_hit(&mut self) {
        self.cache_hits = self.cache_hits.saturating_add(1);
    }

    /// Record a cache miss
    pub fn record_cache_miss(&mut self) {
        self.cache_misses = self.cache_misses.saturating_add(1);
    }

    /// Increment the UDP receive errors counter
    pub fn increment_udp_receive_errors(&mut self) {
        self.udp_receive_errors = self.udp_receive_errors.saturating_add(1);
    }

    /// Increment the TCP accept errors counter
    pub fn increment_tcp_accept_errors(&mut self) {
        self.tcp_accept_errors = self.tcp_accept_errors.saturating_add(1);
    }

    /// Get a list of resolvers sorted by response time (fastest first)
    ///
    /// Resolvers that keep failing are left out for a while.
    pub fn get_resolvers_by_speed(&self) -> Vec<(SocketAddr, f64)> {
        let mut resolvers: Vec<(SocketAddr, f64)> = self
            .resolver_stats
            .iter()
            .filter(|(_, stats)| stats.success_count > 0 && !stats.is_parked())
            .map(|(addr, stats)| (*addr, stats.avg_response_time_ms))
            .collect();

        // Sort by response time (ascending)
        resolvers.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

        resolvers
    }
}

impl Default for GlobalStats {
    fn default() -> Self {
        Self::new()
    }
}

/// Thread-safe wrapper for GlobalStats
#[derive(Debug, Clone)]
pub struct SharedStats {
    inner: Arc<Mutex<GlobalStats>>,
}

impl SharedStats {
    /// Create a new SharedStats
    pub fn new() -> Self {
        Self {
            inner: Arc::new(Mutex::new(GlobalStats::new())),
        }
    }

    /// Record a successful query
    pub async fn record_success(&self, resolver: SocketAddr, response_time: Duration) {
        let mut stats = self.inner.lock().await;
        stats.record_success(resolver, response_time);
    }

    /// Record a failed query
    pub async fn record_failure(&self, resolver: SocketAddr) {
        let mut stats = self.inner.lock().await;
        stats.record_failure(resolver);
    }

    /// Record a timed out query
    pub async fn record_timeout(&self, resolver: SocketAddr) {
        let mut stats = self.inner.lock().await;
        stats.record_timeout(resolver);
    }

    /// Record an answer that can't be timed, such as the reply to a retry
    pub async fn clear_failures(&self, resolver: SocketAddr) {
        let mut stats = self.inner.lock().await;
        stats.clear_failures(resolver);
    }

    /// Record that a query is about to be sent to a resolver
    pub async fn record_query_sent(&self, resolver: SocketAddr) {
        let mut stats = self.inner.lock().await;
        stats.record_query_sent(resolver);
    }

    /// Get a snapshot of the global stats
    pub async fn get_stats(&self) -> GlobalStats {
        let stats = self.inner.lock().await;
        stats.clone()
    }

    /// Record a client query
    pub async fn record_client_query(&self) {
        let mut stats = self.inner.lock().await;
        stats.record_client_query();
    }

    /// Record a cache hit
    pub async fn record_cache_hit(&self) {
        let mut stats = self.inner.lock().await;
        stats.record_cache_hit();
    }

    /// Record a cache miss
    pub async fn record_cache_miss(&self) {
        let mut stats = self.inner.lock().await;
        stats.record_cache_miss();
    }

    /// Get a list of resolvers sorted by response time (fastest first)
    pub async fn get_resolvers_by_speed(&self) -> Vec<(SocketAddr, f64)> {
        let stats = self.inner.lock().await;
        stats.get_resolvers_by_speed()
    }

    /// Increment the UDP receive errors counter
    pub async fn increment_udp_receive_errors(&self) {
        let mut stats = self.inner.lock().await;
        stats.increment_udp_receive_errors();
    }

    /// Increment the TCP accept errors counter
    pub async fn increment_tcp_accept_errors(&self) {
        let mut stats = self.inner.lock().await;
        stats.increment_tcp_accept_errors();
    }
}

impl Default for SharedStats {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_stats_counter_saturation() {
        let mut stats = GlobalStats::new();

        // Set counters near maximum
        stats.total_queries = u64::MAX - 1;
        stats.cache_hits = u64::MAX - 1;

        // These should saturate instead of overflowing
        stats.record_client_query();
        stats.record_cache_hit();

        assert_eq!(stats.client_queries, 1);
        assert_eq!(stats.cache_hits, u64::MAX);

        // One more increment should still be at MAX
        stats.record_cache_hit();
        assert_eq!(stats.cache_hits, u64::MAX);
    }

    #[test]
    fn test_resolver_stats_saturation() {
        let mut stats = ResolverStats::new();

        // Set counters near maximum
        stats.success_count = u64::MAX - 1;
        stats.failure_count = u64::MAX - 1;
        stats.timeout_count = u64::MAX - 1;

        // These should saturate instead of overflowing
        stats.record_success(Duration::from_secs(1));
        stats.record_failure();
        stats.record_timeout();

        assert_eq!(stats.success_count, u64::MAX);
        assert_eq!(stats.failure_count, u64::MAX);
        assert_eq!(stats.timeout_count, u64::MAX);
    }

    #[test]
    fn test_sub_millisecond_samples_are_averaged() {
        let mut stats = ResolverStats::new();
        for _ in 0..100 {
            stats.record_success(Duration::from_micros(400));
        }
        stats.record_success(Duration::from_millis(100));

        assert!((stats.avg_response_time_ms - 20.32).abs() < 0.01);
    }

    #[test]
    fn test_failing_resolver_is_parked_for_a_while() {
        let mut stats = GlobalStats::new();
        let fast: SocketAddr = "192.0.2.1:53".parse().unwrap();
        let slow: SocketAddr = "192.0.2.2:53".parse().unwrap();
        stats.record_success(fast, Duration::from_millis(10));
        stats.record_success(slow, Duration::from_millis(50));
        let without_fast = vec![(slow, 50.0)];

        for _ in 1..PARK_AFTER_FAILURES {
            stats.record_timeout(fast);
        }
        assert_eq!(stats.get_resolvers_by_speed().len(), 2);
        stats.record_timeout(fast);
        assert_eq!(stats.get_resolvers_by_speed(), without_fast);

        // When the time is up, it gets one trial query, not all of them
        stats.resolver_stats.get_mut(&fast).unwrap().parked_until = Some(Instant::now());
        assert_eq!(stats.get_resolvers_by_speed().len(), 2);
        stats.record_query_sent(slow);
        assert_eq!(stats.get_resolvers_by_speed().len(), 2);
        stats.record_query_sent(fast);
        assert_eq!(stats.get_resolvers_by_speed(), without_fast);

        stats.record_success(fast, Duration::from_millis(10));
        assert_eq!(stats.get_resolvers_by_speed().len(), 2);

        // Each failed trial doubles the wait
        assert_eq!(park_duration(PARK_AFTER_FAILURES), MIN_PARK_DURATION);
        assert_eq!(
            park_duration(PARK_AFTER_FAILURES + 1),
            MIN_PARK_DURATION * 2
        );
        assert_eq!(park_duration(u64::MAX), MAX_PARK_DURATION);
    }
}