nautilus_common/throttler/
callbacks.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{cell::RefCell, rc::Rc};
17
18use super::inner::InnerThrottler;
19use crate::timer::{TimeEvent, TimeEventCallback};
20
21/// Stop rate limiting messages
22pub struct ThrottlerResume<T, F> {
23    inner: Rc<RefCell<InnerThrottler<T, F>>>,
24}
25
26impl<T, F> ThrottlerResume<T, F> {
27    /// Creates a new [`ThrottlerResume`] instance.
28    pub const fn new(inner: Rc<RefCell<InnerThrottler<T, F>>>) -> Self {
29        Self { inner }
30    }
31}
32
33impl<T, F> From<ThrottlerResume<T, F>> for TimeEventCallback
34where
35    T: 'static,
36    F: Fn(T) + 'static,
37{
38    fn from(value: ThrottlerResume<T, F>) -> Self {
39        Self::Rust(Rc::new(move |_event: TimeEvent| {
40            value.inner.borrow_mut().is_limiting = false;
41        }))
42    }
43}
44
45/// Process buffered messages.
46#[derive(Clone)]
47pub struct ThrottlerProcess<T, F> {
48    inner: Rc<RefCell<InnerThrottler<T, F>>>,
49}
50
51impl<T, F> ThrottlerProcess<T, F> {
52    /// Creates a new [`ThrottlerProcess`] instance.
53    pub const fn new(inner: Rc<RefCell<InnerThrottler<T, F>>>) -> Self {
54        Self { inner }
55    }
56}
57
58impl<T, F> From<ThrottlerProcess<T, F>> for TimeEventCallback
59where
60    T: 'static,
61    F: Fn(T) + 'static,
62{
63    fn from(value: ThrottlerProcess<T, F>) -> Self {
64        Self::Rust(Rc::new(move |_event: TimeEvent| {
65            let process_clone = ThrottlerProcess {
66                inner: value.inner.clone(),
67            };
68            let mut core = value.inner.borrow_mut();
69            while let Some(msg) = core.buffer.pop_back() {
70                core.send_msg(msg);
71
72                // Set timer to process more buffered messages
73                // if interval limit reached and there are more
74                // buffered messages to process
75                if !core.buffer.is_empty() && core.delta_next() > 0 {
76                    core.is_limiting = true;
77                    core.set_timer(Some(process_clone.into()));
78                    return;
79                }
80            }
81
82            core.is_limiting = false;
83        }))
84    }
85}