push/internal/communications/
rate_limiter.rs

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::internal::storage::Storage;
use std::{
    str::FromStr,
    time::{SystemTime, UNIX_EPOCH},
};

// DB persisted rate limiter.
// Implementation notes: This saves the timestamp of our latest call and the number of times we have
// called `Self::check` within the `Self::periodic_interval` interval of time.
pub struct PersistedRateLimiter {
    op_name: String,
    periodic_interval: u64, // In seconds.
    max_requests_in_interval: u16,
}

impl PersistedRateLimiter {
    pub fn new(op_name: &str, periodic_interval: u64, max_requests_in_interval: u16) -> Self {
        Self {
            op_name: op_name.to_owned(),
            periodic_interval,
            max_requests_in_interval,
        }
    }

    pub fn check<S: Storage>(&self, store: &S) -> bool {
        let (mut timestamp, mut count) = self.impl_get_counters(store);

        let now = now_secs();
        if (now - timestamp) >= self.periodic_interval {
            log::info!(
                "Resetting. now({}) - {} < {} for {}.",
                now,
                timestamp,
                self.periodic_interval,
                &self.op_name
            );
            count = 0;
            timestamp = now;
        } else {
            log::info!(
                "No need to reset inner timestamp and count for {}.",
                &self.op_name
            )
        }

        count += 1;
        self.impl_persist_counters(store, timestamp, count);

        // within interval counter
        if count > self.max_requests_in_interval {
            log::info!(
                "Not allowed: count({}) > {} for {}.",
                count,
                self.max_requests_in_interval,
                &self.op_name
            );
            return false;
        }

        log::info!("Allowed to pass through for {}!", &self.op_name);

        true
    }

    pub fn reset<S: Storage>(&self, store: &S) {
        self.impl_persist_counters(store, now_secs(), 0)
    }

    fn db_meta_keys(&self) -> (String, String) {
        (
            format!("ratelimit_{}_timestamp", &self.op_name),
            format!("ratelimit_{}_count", &self.op_name),
        )
    }

    fn impl_get_counters<S: Storage>(&self, store: &S) -> (u64, u16) {
        let (timestamp_key, count_key) = self.db_meta_keys();
        (
            Self::get_meta_integer(store, &timestamp_key),
            Self::get_meta_integer(store, &count_key),
        )
    }

    #[cfg(test)]
    pub(crate) fn get_counters<S: Storage>(&self, store: &S) -> (u64, u16) {
        self.impl_get_counters(store)
    }

    fn get_meta_integer<S: Storage, T: FromStr + Default>(store: &S, key: &str) -> T {
        store
            .get_meta(key)
            .ok()
            .flatten()
            .map(|s| s.parse())
            .transpose()
            .ok()
            .flatten()
            .unwrap_or_default()
    }

    fn impl_persist_counters<S: Storage>(&self, store: &S, timestamp: u64, count: u16) {
        let (timestamp_key, count_key) = self.db_meta_keys();
        let r1 = store.set_meta(&timestamp_key, &timestamp.to_string());
        let r2 = store.set_meta(&count_key, &count.to_string());
        if r1.is_err() || r2.is_err() {
            log::warn!("Error updating persisted counters for {}.", &self.op_name);
        }
    }

    #[cfg(test)]
    pub(crate) fn persist_counters<S: Storage>(&self, store: &S, timestamp: u64, count: u16) {
        self.impl_persist_counters(store, timestamp, count)
    }
}

fn now_secs() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("Current date before unix epoch.")
        .as_secs()
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::error::Result;
    use crate::Store;

    static PERIODIC_INTERVAL: u64 = 24 * 3600;
    static VERIFY_NOW_INTERVAL: u64 = PERIODIC_INTERVAL + 3600;
    static MAX_REQUESTS: u16 = 500;

    #[test]
    fn test_persisted_rate_limiter_store_counters_roundtrip() -> Result<()> {
        let limiter = PersistedRateLimiter::new("op1", PERIODIC_INTERVAL, MAX_REQUESTS);
        let store = Store::open_in_memory()?;
        limiter.impl_persist_counters(&store, 123, 321);
        assert_eq!((123, 321), limiter.impl_get_counters(&store));
        Ok(())
    }

    #[test]
    fn test_persisted_rate_limiter_after_interval_counter_resets() -> Result<()> {
        let limiter = PersistedRateLimiter::new("op1", PERIODIC_INTERVAL, MAX_REQUESTS);
        let store = Store::open_in_memory()?;
        limiter.impl_persist_counters(&store, now_secs() - VERIFY_NOW_INTERVAL, 50);
        assert!(limiter.check(&store));
        assert_eq!(1, limiter.impl_get_counters(&store).1);
        Ok(())
    }

    #[test]
    fn test_persisted_rate_limiter_false_above_rate_limit() -> Result<()> {
        let limiter = PersistedRateLimiter::new("op1", PERIODIC_INTERVAL, MAX_REQUESTS);
        let store = Store::open_in_memory()?;
        limiter.impl_persist_counters(&store, now_secs(), MAX_REQUESTS + 1);
        assert!(!limiter.check(&store));
        assert_eq!(MAX_REQUESTS + 2, limiter.impl_get_counters(&store).1);
        Ok(())
    }

    #[test]
    fn test_persisted_rate_limiter_reset_above_rate_limit_and_interval() -> Result<()> {
        let limiter = PersistedRateLimiter::new("op1", PERIODIC_INTERVAL, MAX_REQUESTS);
        let store = Store::open_in_memory()?;
        limiter.impl_persist_counters(&store, now_secs() - VERIFY_NOW_INTERVAL, 501);
        assert!(limiter.check(&store));
        assert_eq!(1, limiter.impl_get_counters(&store).1);
        Ok(())
    }

    #[test]
    fn test_persisted_rate_limiter_no_reset_with_rate_limits() -> Result<()> {
        let limiter = PersistedRateLimiter::new("op1", PERIODIC_INTERVAL, MAX_REQUESTS);
        let store = Store::open_in_memory()?;
        assert!(limiter.check(&store));
        Ok(())
    }
}