mctx-core 0.3.0

Runtime-agnostic and portable IPv4 and IPv6 multicast sender library.
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
use crate::{Context, Publication};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant, SystemTime};

#[derive(Debug, Default)]
pub(crate) struct MetricsSequence {
    value: AtomicU64,
}

pub(crate) struct MetricsWriteGuard<'a> {
    sequence: &'a MetricsSequence,
}

impl MetricsSequence {
    pub(crate) fn write(&self) -> MetricsWriteGuard<'_> {
        let mut current = self.value.load(Ordering::Relaxed);
        loop {
            if current % 2 == 1 {
                std::hint::spin_loop();
                current = self.value.load(Ordering::Relaxed);
                continue;
            }

            match self.value.compare_exchange_weak(
                current,
                current.wrapping_add(1),
                Ordering::Acquire,
                Ordering::Relaxed,
            ) {
                Ok(_) => return MetricsWriteGuard { sequence: self },
                Err(observed) => current = observed,
            }
        }
    }

    pub(crate) fn read_consistent<T>(&self, read: impl Fn() -> T) -> T {
        loop {
            let before = self.value.load(Ordering::Acquire);
            if before % 2 == 1 {
                std::hint::spin_loop();
                continue;
            }

            let snapshot = read();
            // Keep all counter reads between the two sequence observations.
            std::sync::atomic::fence(Ordering::AcqRel);
            let after = self.value.load(Ordering::Relaxed);
            if before == after {
                return snapshot;
            }
        }
    }
}

impl Drop for MetricsWriteGuard<'_> {
    fn drop(&mut self) {
        self.sequence.value.fetch_add(1, Ordering::Release);
    }
}

fn rate_per_sec(count: u64, interval_secs: f64) -> f64 {
    if interval_secs > 0.0 {
        count as f64 / interval_secs
    } else {
        0.0
    }
}

/// A point-in-time snapshot of cumulative publication metrics.
///
/// Counter fields in this snapshot are cumulative from the lifetime of the
/// publication and can be compared against an earlier snapshot to compute
/// deltas and rates.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PublicationMetricsSnapshot {
    pub send_calls: u64,
    pub packets_sent: u64,
    pub bytes_sent: u64,
    pub send_errors: u64,
    pub captured_at: SystemTime,
}

/// The difference between two cumulative publication metrics snapshots.
///
/// This contains only counter-based deltas over the sampled interval.
#[derive(Debug, Clone, PartialEq)]
pub struct PublicationMetricsDelta {
    pub interval_secs: f64,
    pub send_calls: u64,
    pub packets_sent: u64,
    pub bytes_sent: u64,
    pub send_errors: u64,
}

impl PublicationMetricsSnapshot {
    /// Computes the counter deltas between this snapshot and an earlier one.
    ///
    /// Returns `None` if:
    /// - `earlier` was captured after `self`
    /// - any cumulative counter appears to have moved backwards
    pub fn delta_since(&self, earlier: &Self) -> Option<PublicationMetricsDelta> {
        let duration = self.captured_at.duration_since(earlier.captured_at).ok()?;
        self.delta_since_duration(earlier, duration)
    }

    /// Computes counter deltas using a caller-supplied monotonic interval.
    pub fn delta_since_duration(
        &self,
        earlier: &Self,
        duration: Duration,
    ) -> Option<PublicationMetricsDelta> {
        Some(PublicationMetricsDelta {
            interval_secs: duration.as_secs_f64(),
            send_calls: self.send_calls.checked_sub(earlier.send_calls)?,
            packets_sent: self.packets_sent.checked_sub(earlier.packets_sent)?,
            bytes_sent: self.bytes_sent.checked_sub(earlier.bytes_sent)?,
            send_errors: self.send_errors.checked_sub(earlier.send_errors)?,
        })
    }
}

impl PublicationMetricsDelta {
    /// Returns the average send call count per second over the sampled interval.
    pub fn send_calls_per_sec(&self) -> f64 {
        rate_per_sec(self.send_calls, self.interval_secs)
    }

    /// Returns the average packets sent per second over the sampled interval.
    pub fn packets_per_sec(&self) -> f64 {
        rate_per_sec(self.packets_sent, self.interval_secs)
    }

    /// Returns the average bytes sent per second over the sampled interval.
    pub fn bytes_per_sec(&self) -> f64 {
        rate_per_sec(self.bytes_sent, self.interval_secs)
    }

    /// Returns the average send error count per second over the sampled interval.
    pub fn send_errors_per_sec(&self) -> f64 {
        rate_per_sec(self.send_errors, self.interval_secs)
    }
}

/// Tracks successive publication metrics snapshots and computes deltas between them.
#[derive(Debug, Clone)]
pub struct PublicationMetricsSampler<'a> {
    publication: &'a Publication,
    previous: Option<PublicationMetricsSnapshot>,
    previous_sampled_at: Option<Instant>,
}

impl<'a> PublicationMetricsSampler<'a> {
    pub fn new(publication: &'a Publication) -> Self {
        Self {
            publication,
            previous: None,
            previous_sampled_at: None,
        }
    }

    pub fn snapshot(&self) -> PublicationMetricsSnapshot {
        self.publication.metrics_snapshot()
    }

    pub fn sample(&mut self) -> Option<PublicationMetricsDelta> {
        let current = self.snapshot();
        self.sample_snapshot_at(current, Instant::now())
    }

    /// Computes a delta from a caller-supplied snapshot using its wall-clock
    /// `captured_at` timestamp. This clears the monotonic baseline used by
    /// `sample()` and `sample_snapshot_at()`.
    pub fn sample_snapshot(
        &mut self,
        current: PublicationMetricsSnapshot,
    ) -> Option<PublicationMetricsDelta> {
        let delta = self
            .previous
            .as_ref()
            .and_then(|previous| current.delta_since(previous));
        self.previous = Some(current);
        self.previous_sampled_at = None;
        delta
    }

    /// Computes a delta from a caller-supplied snapshot and monotonic capture
    /// instant. The first call after `sample_snapshot()` establishes a new
    /// monotonic baseline and returns `None`.
    pub fn sample_snapshot_at(
        &mut self,
        current: PublicationMetricsSnapshot,
        sampled_at: Instant,
    ) -> Option<PublicationMetricsDelta> {
        let delta = match (&self.previous, self.previous_sampled_at) {
            (Some(previous), Some(previous_sampled_at)) => sampled_at
                .checked_duration_since(previous_sampled_at)
                .and_then(|duration| current.delta_since_duration(previous, duration)),
            _ => None,
        };
        self.previous = Some(current);
        self.previous_sampled_at = Some(sampled_at);
        delta
    }

    pub fn reset(&mut self) {
        self.previous = None;
        self.previous_sampled_at = None;
    }

    pub fn previous(&self) -> Option<&PublicationMetricsSnapshot> {
        self.previous.as_ref()
    }

    /// Convenience alias for `sample()`.
    pub fn delta(&mut self) -> Option<PublicationMetricsDelta> {
        self.sample()
    }
}

/// A point-in-time snapshot of cumulative context metrics.
///
/// Counter fields in this snapshot are cumulative from the lifetime of the
/// context for send activity issued through `Context` methods and can be
/// compared against an earlier snapshot to compute deltas and rates.
///
/// Gauge-like fields such as `active_publications` represent the current state
/// at the moment the snapshot was taken and should not be interpreted as
/// cumulative counters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContextMetricsSnapshot {
    pub publications_added: u64,
    pub publications_removed: u64,
    pub active_publications: usize,
    pub total_send_calls: u64,
    pub total_packets_sent: u64,
    pub total_bytes_sent: u64,
    pub total_send_errors: u64,
    pub captured_at: SystemTime,
}

/// The difference between two cumulative context metrics snapshots.
///
/// This contains only counter-based deltas over the sampled interval.
/// Gauge-like values such as active publication counts are intentionally not
/// included here; callers should inspect those directly from the latest
/// snapshot instead.
#[derive(Debug, Clone, PartialEq)]
pub struct ContextMetricsDelta {
    pub interval_secs: f64,
    pub publications_added: u64,
    pub publications_removed: u64,
    pub send_calls: u64,
    pub packets_sent: u64,
    pub bytes_sent: u64,
    pub send_errors: u64,
}

impl ContextMetricsSnapshot {
    /// Computes the counter deltas between this snapshot and an earlier one.
    ///
    /// Returns `None` if:
    /// - `earlier` was captured after `self`
    /// - any cumulative counter appears to have moved backwards
    ///
    /// The resulting delta contains only counter-based values and the elapsed
    /// interval in seconds. Gauge-like values such as active publication counts
    /// should be read directly from the latest snapshot instead.
    pub fn delta_since(&self, earlier: &Self) -> Option<ContextMetricsDelta> {
        let duration = self.captured_at.duration_since(earlier.captured_at).ok()?;
        self.delta_since_duration(earlier, duration)
    }

    /// Computes counter deltas using a caller-supplied monotonic interval.
    pub fn delta_since_duration(
        &self,
        earlier: &Self,
        duration: Duration,
    ) -> Option<ContextMetricsDelta> {
        Some(ContextMetricsDelta {
            interval_secs: duration.as_secs_f64(),
            publications_added: self
                .publications_added
                .checked_sub(earlier.publications_added)?,
            publications_removed: self
                .publications_removed
                .checked_sub(earlier.publications_removed)?,
            send_calls: self
                .total_send_calls
                .checked_sub(earlier.total_send_calls)?,
            packets_sent: self
                .total_packets_sent
                .checked_sub(earlier.total_packets_sent)?,
            bytes_sent: self
                .total_bytes_sent
                .checked_sub(earlier.total_bytes_sent)?,
            send_errors: self
                .total_send_errors
                .checked_sub(earlier.total_send_errors)?,
        })
    }
}

impl ContextMetricsDelta {
    /// Returns the average send call count per second over the sampled interval.
    pub fn send_calls_per_sec(&self) -> f64 {
        rate_per_sec(self.send_calls, self.interval_secs)
    }

    /// Returns the average packets sent per second over the sampled interval.
    pub fn packets_per_sec(&self) -> f64 {
        rate_per_sec(self.packets_sent, self.interval_secs)
    }

    /// Returns the average bytes sent per second over the sampled interval.
    pub fn bytes_per_sec(&self) -> f64 {
        rate_per_sec(self.bytes_sent, self.interval_secs)
    }

    /// Returns the average send error count per second over the sampled interval.
    pub fn send_errors_per_sec(&self) -> f64 {
        rate_per_sec(self.send_errors, self.interval_secs)
    }
}

/// Tracks successive context metrics snapshots and computes deltas between them.
#[derive(Debug, Clone)]
pub struct ContextMetricsSampler<'a> {
    context: &'a Context,
    previous: Option<ContextMetricsSnapshot>,
    previous_sampled_at: Option<Instant>,
}

impl<'a> ContextMetricsSampler<'a> {
    pub fn new(context: &'a Context) -> Self {
        Self {
            context,
            previous: None,
            previous_sampled_at: None,
        }
    }

    pub fn snapshot(&self) -> ContextMetricsSnapshot {
        self.context.metrics_snapshot()
    }

    pub fn sample(&mut self) -> Option<ContextMetricsDelta> {
        let current = self.snapshot();
        self.sample_snapshot_at(current, Instant::now())
    }

    /// Computes a delta from a caller-supplied snapshot using its wall-clock
    /// `captured_at` timestamp. This clears the monotonic baseline used by
    /// `sample()` and `sample_snapshot_at()`.
    pub fn sample_snapshot(
        &mut self,
        current: ContextMetricsSnapshot,
    ) -> Option<ContextMetricsDelta> {
        let delta = self
            .previous
            .as_ref()
            .and_then(|previous| current.delta_since(previous));
        self.previous = Some(current);
        self.previous_sampled_at = None;
        delta
    }

    /// Computes a delta from a caller-supplied snapshot and monotonic capture
    /// instant. The first call after `sample_snapshot()` establishes a new
    /// monotonic baseline and returns `None`.
    pub fn sample_snapshot_at(
        &mut self,
        current: ContextMetricsSnapshot,
        sampled_at: Instant,
    ) -> Option<ContextMetricsDelta> {
        let delta = match (&self.previous, self.previous_sampled_at) {
            (Some(previous), Some(previous_sampled_at)) => sampled_at
                .checked_duration_since(previous_sampled_at)
                .and_then(|duration| current.delta_since_duration(previous, duration)),
            _ => None,
        };
        self.previous = Some(current);
        self.previous_sampled_at = Some(sampled_at);
        delta
    }

    pub fn reset(&mut self) {
        self.previous = None;
        self.previous_sampled_at = None;
    }

    pub fn previous(&self) -> Option<&ContextMetricsSnapshot> {
        self.previous.as_ref()
    }

    /// Convenience alias for `sample()`.
    pub fn delta(&mut self) -> Option<ContextMetricsDelta> {
        self.sample()
    }
}

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

    fn publication_snapshot(
        send_calls: u64,
        packets_sent: u64,
        bytes_sent: u64,
        send_errors: u64,
        captured_at: SystemTime,
    ) -> PublicationMetricsSnapshot {
        PublicationMetricsSnapshot {
            send_calls,
            packets_sent,
            bytes_sent,
            send_errors,
            captured_at,
        }
    }

    #[test]
    fn context_delta_since_uses_lifetime_total_fields() {
        let earlier = ContextMetricsSnapshot {
            publications_added: 1,
            publications_removed: 0,
            active_publications: 1,
            total_send_calls: 10,
            total_packets_sent: 8,
            total_bytes_sent: 800,
            total_send_errors: 2,
            captured_at: SystemTime::UNIX_EPOCH,
        };
        let later = ContextMetricsSnapshot {
            publications_added: 2,
            publications_removed: 1,
            active_publications: 1,
            total_send_calls: 14,
            total_packets_sent: 11,
            total_bytes_sent: 1250,
            total_send_errors: 3,
            captured_at: SystemTime::UNIX_EPOCH + Duration::from_secs(2),
        };

        let delta = later.delta_since(&earlier).unwrap();

        assert_eq!(delta.interval_secs, 2.0);
        assert_eq!(delta.publications_added, 1);
        assert_eq!(delta.publications_removed, 1);
        assert_eq!(delta.send_calls, 4);
        assert_eq!(delta.packets_sent, 3);
        assert_eq!(delta.bytes_sent, 450);
        assert_eq!(delta.send_errors, 1);
        assert_eq!(delta.packets_per_sec(), 1.5);
        assert_eq!(delta.bytes_per_sec(), 225.0);
    }

    #[test]
    fn publication_delta_since_uses_interval_and_rates() {
        let earlier = publication_snapshot(4, 3, 300, 1, SystemTime::UNIX_EPOCH);
        let later = publication_snapshot(
            7,
            5,
            620,
            2,
            SystemTime::UNIX_EPOCH + Duration::from_secs(4),
        );

        let delta = later.delta_since(&earlier).unwrap();

        assert_eq!(delta.interval_secs, 4.0);
        assert_eq!(delta.send_calls, 3);
        assert_eq!(delta.packets_sent, 2);
        assert_eq!(delta.bytes_sent, 320);
        assert_eq!(delta.send_errors, 1);
        assert_eq!(delta.send_calls_per_sec(), 0.75);
        assert_eq!(delta.packets_per_sec(), 0.5);
        assert_eq!(delta.bytes_per_sec(), 80.0);
        assert_eq!(delta.send_errors_per_sec(), 0.25);
    }

    #[test]
    fn context_sampler_uses_monotonic_interval_when_wall_clock_moves_backwards() {
        let context = Context::new();
        let mut sampler = ContextMetricsSampler::new(&context);
        let sampled_at = Instant::now();
        let earlier = ContextMetricsSnapshot {
            publications_added: 1,
            publications_removed: 0,
            active_publications: 1,
            total_send_calls: 1,
            total_packets_sent: 1,
            total_bytes_sent: 10,
            total_send_errors: 0,
            captured_at: SystemTime::UNIX_EPOCH + Duration::from_secs(10),
        };
        let later = ContextMetricsSnapshot {
            total_send_calls: 2,
            total_packets_sent: 2,
            total_bytes_sent: 20,
            captured_at: SystemTime::UNIX_EPOCH + Duration::from_secs(5),
            ..earlier.clone()
        };

        assert!(sampler.sample_snapshot_at(earlier, sampled_at).is_none());
        let delta = sampler
            .sample_snapshot_at(later, sampled_at + Duration::from_secs(2))
            .unwrap();

        assert_eq!(delta.interval_secs, 2.0);
        assert_eq!(delta.packets_sent, 1);
        assert_eq!(delta.bytes_sent, 10);
    }

    #[test]
    fn context_sampler_uses_supplied_snapshot_timestamps() {
        let context = Context::new();
        let mut sampler = ContextMetricsSampler::new(&context);
        let earlier = ContextMetricsSnapshot {
            publications_added: 1,
            publications_removed: 0,
            active_publications: 1,
            total_send_calls: 1,
            total_packets_sent: 1,
            total_bytes_sent: 10,
            total_send_errors: 0,
            captured_at: SystemTime::UNIX_EPOCH + Duration::from_secs(10),
        };
        let later = ContextMetricsSnapshot {
            total_send_calls: 2,
            total_packets_sent: 2,
            total_bytes_sent: 20,
            captured_at: SystemTime::UNIX_EPOCH + Duration::from_secs(15),
            ..earlier.clone()
        };

        assert!(sampler.sample_snapshot(earlier).is_none());
        let delta = sampler.sample_snapshot(later).unwrap();

        assert_eq!(delta.interval_secs, 5.0);
        assert_eq!(delta.packets_sent, 1);
    }
}