1pub(crate) struct TrackerLocked {
2 pub(crate) rl: rl_core::Tracker<tokio::time::Instant>,
3 pub(crate) queue: Option<crate::TrackerQueue>,
4}
5
6pub struct Tracker {
7 pub(crate) locked: std::sync::Mutex<TrackerLocked>,
8}
9
10impl Tracker {
11 fn from_core(rl: rl_core::Tracker<tokio::time::Instant>) -> Self {
12 Tracker {
13 locked: TrackerLocked {
14 rl,
15 queue: None,
16 }.into(),
17 }
18 }
19
20 pub fn empty() -> Self {
21 Self::from_core(rl_core::Tracker::empty())
22 }
23
24 pub fn full() -> Self {
25 Self::from_core(rl_core::Tracker::full())
26 }
27
28 pub fn acquire_range<'a>(
29 &'a self,
30 config: &'a crate::Config,
31 request: std::ops::RangeInclusive<u32>,
32 ) -> crate::Acquire {
33 crate::Acquire {
34 config,
35 tracker: self,
36 request,
37 state: Default::default(),
38 }
39 }
40
41 pub async fn acquire(
42 &self,
43 config: &crate::Config,
44 count: u32,
45 ) -> Result<(), crate::Error> {
46 match self.acquire_range(config, count..=count).await {
47 Ok(acquired) => {
48 debug_assert_eq!(acquired, count);
49 Ok(())
50 }
51 Err(e) => Err(e),
52 }
53 }
54}