kraken_async_rs/rate_limiting/
ttl_cache.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};
use time::OffsetDateTime;

#[inline]
fn now() -> i128 {
    OffsetDateTime::now_utc().unix_timestamp_nanos() / 1000
}

/// A time-to-live entry that should remain available until the provided `ttl` value.
///
/// These are used to store order ids, `id`, and the creation time, `data`, of orders, but was left
/// generic for potential later use.
#[derive(Debug, Clone, Copy)]
pub struct TtlEntry<K, T>
where
    K: Ord + Clone,
    T: Clone,
{
    pub id: K,
    ttl: i128,
    pub data: T,
}

impl<K, T> TtlEntry<K, T>
where
    K: Ord + Clone,
    T: Clone,
{
    pub fn new(id: K, ttl_us: i128, data: T) -> TtlEntry<K, T> {
        TtlEntry {
            id,
            ttl: now() + ttl_us,
            data,
        }
    }
}

impl<K, T> Eq for TtlEntry<K, T>
where
    K: Ord + Clone,
    T: Clone,
{
}

impl<K, T> PartialEq<Self> for TtlEntry<K, T>
where
    K: Ord + Clone,
    T: Clone,
{
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id && self.ttl == other.ttl
    }
}

impl<K, T> PartialOrd<Self> for TtlEntry<K, T>
where
    K: Ord + Clone,
    T: Clone,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.ttl.cmp(&other.ttl))
    }
}

impl<K, T> Ord for TtlEntry<K, T>
where
    K: Ord + Clone,
    T: Clone,
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.ttl.cmp(&other.ttl)
    }
}

/// A time-to-live cache that removes values when they expire. This is used to store and look up
/// orders, to determine how old they are for rate limiting penalties when editing or cancelling.
#[derive(Debug)]
pub struct TtlCache<K, T>
where
    K: Ord + Clone,
    T: Clone,
{
    ids: BTreeMap<K, TtlEntry<K, T>>,
    ttls: BTreeSet<TtlEntry<K, T>>,
}

impl<K, T> Default for TtlCache<K, T>
where
    K: Ord + Clone,
    T: Clone,
{
    fn default() -> Self {
        TtlCache::new()
    }
}

impl<K, T> TtlCache<K, T>
where
    K: Ord + Clone,
    T: Clone,
{
    /// Create a new, empty cache.
    pub fn new() -> TtlCache<K, T> {
        TtlCache {
            ids: Default::default(),
            ttls: Default::default(),
        }
    }

    /// Insert the provided [TtlEntry] by it's id for future lookup. Entries beyond their ttl are
    /// removed automatically any time the `remove`, `get`, or `contains` methods are called.
    pub fn insert(&mut self, ttl_entry: TtlEntry<K, T>) -> Option<TtlEntry<K, T>> {
        self.ttls.insert(ttl_entry.clone());
        self.ids.insert(ttl_entry.id.clone(), ttl_entry)
    }

    /// Removes an entry manually, returning if the entry was removed.
    ///
    /// The cache is cleaned of any expired values after checking if this value was removed.
    ///
    /// This follows the same semantics as [BTreeSet]'s `remove` method.
    pub fn remove(&mut self, ttl_entry: &TtlEntry<K, T>) -> bool {
        self.ids.remove(&ttl_entry.id);
        let removed = self.ttls.remove(ttl_entry);
        self.remove_expired_values();

        removed
    }

    /// Returns if the provided key is in the cache, after removing any expired values.
    pub fn contains(&mut self, id: &K) -> bool {
        self.remove_expired_values();
        self.ids.contains_key(id)
    }

    /// Gets a [TtlEntry] by id after removing any expired values.
    pub fn get(&mut self, id: &K) -> Option<&TtlEntry<K, T>> {
        self.remove_expired_values();
        self.ids.get(id)
    }

    fn remove_expired_values(&mut self) {
        let now = now();
        let mut to_remove = Vec::new();

        for entry in &self.ttls {
            if entry.ttl < now {
                to_remove.push(entry.clone());
            }
        }

        for entry in to_remove {
            self.ids.remove(&entry.id);
            self.ttls.remove(&entry);
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::rate_limiting::ttl_cache::{TtlCache, TtlEntry};
    use std::cmp::Ordering::{Equal, Greater, Less};
    use std::thread::sleep;
    use std::time::Duration as StdDuration;
    use time::Duration;

    #[test]
    fn test_ttl_entry_eq_partial_cmp() {
        let entry_0 = TtlEntry {
            id: "0x1",
            ttl: 0,
            data: 0,
        };
        let entry_1 = TtlEntry {
            id: "0x1",
            ttl: 1,
            data: 0,
        };
        let entry_2 = TtlEntry {
            id: "0x1",
            ttl: 1,
            data: 0,
        };

        assert_ne!(entry_0, entry_1);
        assert_ne!(entry_0, entry_2);
        assert_eq!(entry_1, entry_2);

        assert_eq!(Less, entry_0.partial_cmp(&entry_1).unwrap());
        assert_eq!(Less, entry_0.partial_cmp(&entry_2).unwrap());
        assert_eq!(Equal, entry_1.partial_cmp(&entry_2).unwrap());
    }

    #[test]
    fn test_ttl_entry_ord() {
        let entry_0 = TtlEntry {
            id: "0x1",
            ttl: 0,
            data: 0,
        };
        let entry_1 = TtlEntry {
            id: "0x1",
            ttl: 1,
            data: 0,
        };
        let entry_2 = TtlEntry {
            id: "0x2",
            ttl: 2,
            data: 0,
        };

        assert_eq!(Less, entry_0.cmp(&entry_1));
        assert_eq!(Less, entry_0.cmp(&entry_2));
        assert_eq!(Less, entry_1.cmp(&entry_2));

        assert_eq!(Greater, entry_1.cmp(&entry_0));
        assert_eq!(Greater, entry_2.cmp(&entry_1));
        assert_eq!(Greater, entry_2.cmp(&entry_1));

        assert_eq!(Equal, entry_0.cmp(&entry_0));
        assert_eq!(Equal, entry_1.cmp(&entry_1));
        assert_eq!(Equal, entry_2.cmp(&entry_2));
    }

    #[test]
    fn test_ttl_cache_insert_remove() {
        let ttl = Duration::seconds(1).whole_microseconds();
        let entry_1 = TtlEntry::new("0x1".to_string(), ttl, 0);
        let entry_2 = TtlEntry::new("0x2".to_string(), ttl, 0);

        let mut cache = TtlCache::new();

        cache.insert(entry_1.clone());

        assert!(cache.contains(&entry_1.id));
        assert!(!cache.contains(&entry_2.id));

        assert!(cache.remove(&entry_1));

        assert!(!cache.contains(&entry_1.id));
        assert!(!cache.contains(&entry_2.id));
    }

    #[test]
    fn test_ttl_cache_insert_get() {
        let ttl = Duration::seconds(1).whole_microseconds();
        let entry_1 = TtlEntry::new("0x1".to_string(), ttl, 0);

        let mut cache = TtlCache::new();

        cache.insert(entry_1.clone());

        assert!(cache.contains(&entry_1.id));

        let result = cache.get(&entry_1.id);
        assert!(result.is_some());
        assert_eq!(entry_1, *result.unwrap())
    }

    #[test]
    fn test_ttl_cache_expiry() {
        let entry_1 = TtlEntry::new(
            "0x1".to_string(),
            Duration::milliseconds(250).whole_microseconds(),
            "",
        );
        let entry_2 = TtlEntry::new(
            "0x2".to_string(),
            Duration::milliseconds(500).whole_microseconds(),
            "",
        );

        let mut cache = TtlCache::new();

        cache.insert(entry_1.clone());
        cache.insert(entry_2.clone());

        assert!(cache.contains(&entry_1.id));
        assert!(cache.contains(&entry_2.id));

        // let first entry expire
        sleep(StdDuration::from_millis(300));
        assert!(!cache.contains(&entry_1.id));
        assert!(cache.contains(&entry_2.id));

        // let second entry expire
        sleep(StdDuration::from_millis(300));
        assert!(!cache.contains(&entry_1.id));
        assert!(!cache.contains(&entry_2.id));
    }
}