Skip to main content

dynomite/core/
setting.rs

1//! Global runtime settings.
2//!
3//! The C reference exposes a single tunable, `msgs_per_sec`, accessed
4//! through paired `msgs_per_sec()` / `set_msgs_per_sec()` functions.
5//! The Rust port stores it in an [`AtomicU32`] so the getter and setter
6//! are wait-free and lock-free.
7
8use std::sync::atomic::{AtomicU32, Ordering};
9
10/// Default value for [`msgs_per_sec`].
11///
12/// # Examples
13///
14/// ```
15/// use dynomite::core::setting::DEFAULT_MSGS_PER_SEC;
16/// assert_eq!(DEFAULT_MSGS_PER_SEC, 50_000);
17/// ```
18pub const DEFAULT_MSGS_PER_SEC: u32 = 50_000;
19
20static MSGS_PER_SEC: AtomicU32 = AtomicU32::new(DEFAULT_MSGS_PER_SEC);
21
22/// Return the current per-connection message-rate ceiling.
23///
24/// # Examples
25///
26/// ```
27/// use dynomite::core::setting::{msgs_per_sec, DEFAULT_MSGS_PER_SEC};
28/// assert!(msgs_per_sec() >= 1);
29/// // The default may have been mutated by a previous test; check the constant
30/// // independently to avoid cross-test ordering hazards.
31/// assert_eq!(DEFAULT_MSGS_PER_SEC, 50_000);
32/// ```
33pub fn msgs_per_sec() -> u32 {
34    MSGS_PER_SEC.load(Ordering::Relaxed)
35}
36
37/// Update the per-connection message-rate ceiling.
38///
39/// # Examples
40///
41/// ```
42/// use dynomite::core::setting::{msgs_per_sec, set_msgs_per_sec};
43/// let prev = msgs_per_sec();
44/// set_msgs_per_sec(7);
45/// assert_eq!(msgs_per_sec(), 7);
46/// set_msgs_per_sec(prev);
47/// ```
48pub fn set_msgs_per_sec(value: u32) {
49    MSGS_PER_SEC.store(value, Ordering::Relaxed);
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn round_trip() {
58        let prev = msgs_per_sec();
59        set_msgs_per_sec(123);
60        assert_eq!(msgs_per_sec(), 123);
61        set_msgs_per_sec(prev);
62        assert_eq!(msgs_per_sec(), prev);
63    }
64}