slack_morphism/ratectl/
limit.rs

1use crate::prelude::ThrottlingCounter;
2
3/**
4 * A rate limit definition
5*/
6#[derive(Debug, PartialEq, Eq, Clone)]
7pub struct SlackApiRateControlLimit {
8    pub value: usize,
9    pub per: std::time::Duration,
10}
11
12impl SlackApiRateControlLimit {
13    pub fn new(value: usize, per: std::time::Duration) -> Self {
14        assert!(value > 0, "Limit value should be more than zero");
15        assert!(
16            per.as_millis() > 0,
17            "Limit duration should be more than zero"
18        );
19
20        Self { value, per }
21    }
22
23    pub fn to_rate_limit_in_ms(&self) -> u64 {
24        self.per.as_millis() as u64 / self.value as u64
25    }
26
27    pub fn to_rate_limit_capacity(&self) -> usize {
28        self.per.as_millis() as usize / self.to_rate_limit_in_ms() as usize
29    }
30
31    pub fn to_throttling_counter(&self) -> ThrottlingCounter {
32        ThrottlingCounter::new(self.to_rate_limit_capacity(), self.to_rate_limit_in_ms())
33    }
34}