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
use std::{
    ops::Deref,
    sync::atomic::{AtomicI32, Ordering},
};
use thiserror::Error;

static ACQUIRED_FD: AtomicI32 = AtomicI32::new(0);
#[derive(Debug)]
pub struct FDGuard(i32);

impl FDGuard {
    pub fn acquired(&self) -> i32 {
        self.0
    }
}

impl Deref for FDGuard {
    type Target = i32;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Drop for FDGuard {
    fn drop(&mut self) {
        ACQUIRED_FD.fetch_sub(self.0, Ordering::SeqCst); // todo ordering??
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Error)]
#[error("Exceeded upper bound, acquired: {acquired}, limit: {limit}")]
pub struct Error {
    pub acquired: i32,
    pub limit: i32,
}

pub fn acquire_guard(value: i32) -> Result<FDGuard, Error> {
    loop {
        let acquired = ACQUIRED_FD.load(Ordering::SeqCst); // todo ordering??
        let limit = limit();
        if acquired + value > limit {
            return Err(Error { acquired, limit });
        }
        // todo ordering??
        match ACQUIRED_FD.compare_exchange(acquired, acquired + value, Ordering::SeqCst, Ordering::SeqCst) {
            Ok(_) => return Ok(FDGuard(value)),
            Err(_) => continue, // The global counter was updated by another thread, retry
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
pub fn try_set_fd_limit(limit: u64) -> std::io::Result<u64> {
    cfg_if::cfg_if! {
        if #[cfg(target_os = "windows")] {
                rlimit::setmaxstdio(limit as u32).map(|v| v as u64)
        } else if #[cfg(unix)] {
            rlimit::increase_nofile_limit(limit)
        }
    }
}

pub fn limit() -> i32 {
    cfg_if::cfg_if! {
        if #[cfg(test)] {
            100
        }
        else if #[cfg(target_os = "windows")] {
            rlimit::getmaxstdio() as i32
        }
        else if #[cfg(unix)] {
            rlimit::getrlimit(rlimit::Resource::NOFILE).unwrap().0 as i32
        }
        else {
            512
        }
    }
}

pub fn remainder() -> i32 {
    limit() - ACQUIRED_FD.load(Ordering::Relaxed)
}

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

    #[test]
    fn test_acquire_and_release_guards() {
        let guard = acquire_guard(30).unwrap();
        assert_eq!(guard.acquired(), 30);
        assert_eq!(ACQUIRED_FD.load(Ordering::Relaxed), 30);

        let err = acquire_guard(80).unwrap_err();
        assert_eq!(err, Error { acquired: 30, limit: 100 });
        assert_eq!(ACQUIRED_FD.load(Ordering::Relaxed), 30);

        drop(guard);
        assert_eq!(ACQUIRED_FD.load(Ordering::Relaxed), 0);

        let guard = acquire_guard(100).unwrap();
        assert_eq!(guard.acquired(), 100);
        assert_eq!(ACQUIRED_FD.load(Ordering::Relaxed), 100);
        drop(guard);
        assert_eq!(ACQUIRED_FD.load(Ordering::Relaxed), 0);

        let err = acquire_guard(101).unwrap_err();
        assert_eq!(err, Error { acquired: 0, limit: 100 });
    }
}