limitador 0.11.0

Rate limiting library
Documentation
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// To use Redis cluster and some Redis proxies, all the keys used in a
// command or in a pipeline need to be sharded to the same server.
// To help with that, Redis uses "hash tags". When a key contains "{" and
// "}" only what's inside them is hashed.
// To ensure that all the keys involved in a given operation belong to the
// same shard, we can shard by namespace.
// When there are multiple pairs of "{" and "}" only the first one is taken
// into account. Ref: https://redis.io/topics/cluster-spec (key hash tags).
// Reminder: in format!(), "{" is escaped with "{{".

// Note: keep in mind that what's described above is the default in Redis, when
// reusing this module for other storage implementations make sure that using
// "{}" for sharding applies.

use crate::counter::Counter;
use crate::limit::Limit;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

pub fn key_for_counter(counter: &Counter) -> Vec<u8> {
    if counter.id().is_none() {
        // continue to use the legacy text encoding...
        let namespace = counter.namespace().as_ref();
        let key = if counter.remaining().is_some() || counter.expires_in().is_some() {
            format!(
                "namespace:{{{namespace}}},counter:{}",
                serde_json::to_string(&counter.key()).unwrap()
            )
        } else {
            format!(
                "namespace:{{{namespace}}},counter:{}",
                serde_json::to_string(counter).unwrap()
            )
        };
        key.into_bytes()
    } else {
        // if the id is set, use the new binary encoding...
        bin::key_for_counter_v2(counter)
    }
}

pub fn key_for_counters_of_limit(limit: &Limit) -> Vec<u8> {
    if let Some(id) = limit.id() {
        #[derive(PartialEq, Debug, Serialize, Deserialize)]
        struct IdLimitKey<'a> {
            id: &'a str,
        }

        let key = IdLimitKey { id };

        let mut encoded_key = Vec::new();
        encoded_key = postcard::to_extend(&2u8, encoded_key).unwrap();
        encoded_key = postcard::to_extend(&key, encoded_key).unwrap();
        encoded_key
    } else {
        let namespace = limit.namespace().as_ref();
        format!(
            "namespace:{{{namespace}}},counters_of_limit:{}",
            serde_json::to_string(limit).unwrap()
        )
        .into_bytes()
    }
}

pub fn counter_from_counter_key(key: &Vec<u8>, limit: Arc<Limit>) -> Counter {
    let mut counter = partial_counter_from_counter_key(key);
    if !counter.update_to_limit(Arc::clone(&limit)) {
        // this means some kind of data corruption _or_ most probably
        // an out of sync `impl PartialEq for Limit` vs `pub fn key_for_counter(counter: &Counter) -> String`
        panic!(
            "Failed to rebuild Counter's Limit from the provided Limit: {:?} vs {:?}",
            counter.limit(),
            limit
        )
    }
    counter
}

pub fn partial_counter_from_counter_key(key: &Vec<u8>) -> Counter {
    if key.starts_with(b"namespace:") {
        let key = String::from_utf8_lossy(key.as_ref());

        // It's using to the legacy text encoding...
        let namespace_prefix = "namespace:";
        let counter_prefix = ",counter:";

        // Find the start position of the counter portion
        let start_pos_namespace = key
            .find(namespace_prefix)
            .expect("Namespace not found in the key");
        let start_pos_counter = key[start_pos_namespace..]
            .find(counter_prefix)
            .expect("Counter not found in the key")
            + start_pos_namespace
            + counter_prefix.len();

        // Extract counter JSON substring and deserialize it
        let counter_str = &key[start_pos_counter..];
        let counter: Counter =
            serde_json::from_str(counter_str).expect("Failed to deserialize counter JSON");
        counter
    } else {
        // It's using to the new binary encoding...
        bin::partial_counter_from_counter_key_v2(key)
    }
}

#[cfg(test)]
mod tests {
    use super::{key_for_counter, key_for_counters_of_limit, partial_counter_from_counter_key};
    use crate::counter::Counter;
    use crate::Limit;
    use std::collections::HashMap;
    use std::time::Duration;

    #[test]
    fn key_for_limit_format() {
        let limit = Limit::new(
            "example.com",
            10,
            60,
            vec!["req_method == 'GET'".try_into().expect("failed parsing!")],
            vec!["app_id".try_into().expect("failed parsing!")],
        );
        assert_eq!(
            "namespace:{example.com},counters_of_limit:{\"namespace\":\"example.com\",\"seconds\":60,\"conditions\":[\"req_method == 'GET'\"],\"variables\":[\"app_id\"]}".as_bytes(),
            key_for_counters_of_limit(&limit))
    }

    #[test]
    fn key_for_limit_with_id_format() {
        let limit = Limit::with_id(
            "test_id",
            "example.com",
            10,
            60,
            vec!["req_method == 'GET'".try_into().expect("failed parsing!")],
            vec!["app_id".try_into().expect("failed parsing!")],
        );
        assert_eq!(
            "\u{2}\u{7}test_id".as_bytes(),
            key_for_counters_of_limit(&limit)
        )
    }

    #[test]
    fn counter_key_and_counter_are_symmetric() {
        let namespace = "ns_counter:";
        let limit = Limit::new(
            namespace,
            1,
            1,
            vec!["req_method == 'GET'".try_into().expect("failed parsing!")],
            vec!["app_id".try_into().expect("failed parsing!")],
        );
        let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
        let ctx = map.into();
        let counter = Counter::new(limit.clone(), &ctx)
            .expect("counter creation failed!")
            .expect("must have a counter");
        let raw = key_for_counter(&counter);
        assert_eq!(counter, partial_counter_from_counter_key(&raw));
    }

    #[test]
    fn counter_key_does_not_include_transient_state() {
        let namespace = "ns_counter:";
        let limit = Limit::new(
            namespace,
            1,
            1,
            vec!["req_method == 'GET'".try_into().expect("failed parsing!")],
            vec!["app_id".try_into().expect("failed parsing!")],
        );
        let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
        let ctx = map.into();
        let counter = Counter::new(limit.clone(), &ctx)
            .expect("counter creation failed!")
            .expect("must have a counter");
        let mut other = counter.clone();
        other.set_remaining(123);
        other.set_expires_in(Duration::from_millis(456));
        assert_eq!(key_for_counter(&counter), key_for_counter(&other));
    }
}

#[cfg(feature = "disk_storage")]
pub mod bin {
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;

    use crate::counter::Counter;
    use crate::limit::{Limit, Predicate};

    #[derive(PartialEq, Debug, Serialize, Deserialize)]
    struct IdCounterKey<'a> {
        id: &'a str,
        variables: Vec<(&'a str, &'a str)>,
    }

    impl<'a> From<&'a Counter> for IdCounterKey<'a> {
        fn from(counter: &'a Counter) -> Self {
            IdCounterKey {
                id: counter.id().unwrap(),
                variables: counter.variables_for_key(),
            }
        }
    }

    #[derive(PartialEq, Debug, Serialize, Deserialize)]
    struct CounterKey<'a> {
        ns: &'a str,
        seconds: u64,
        conditions: Vec<String>,
        variables: Vec<(&'a str, &'a str)>,
    }

    impl<'a> From<&'a Counter> for CounterKey<'a> {
        fn from(counter: &'a Counter) -> Self {
            let set = counter.limit().conditions();
            let mut conditions = Vec::with_capacity(set.len());
            for cond in &set {
                conditions.push(cond.clone());
            }
            conditions.sort();

            CounterKey {
                ns: counter.namespace().as_ref(),
                seconds: counter.window().as_secs(),
                conditions,
                variables: counter.variables_for_key(),
            }
        }
    }

    pub fn key_for_counter_v2(counter: &Counter) -> Vec<u8> {
        let mut encoded_key = Vec::new();
        if counter.id().is_none() {
            let key: CounterKey = counter.into();
            encoded_key = postcard::to_extend(&1u8, encoded_key).unwrap();
            encoded_key = postcard::to_extend(&key, encoded_key).unwrap()
        } else {
            let key: IdCounterKey = counter.into();
            encoded_key = postcard::to_extend(&2u8, encoded_key).unwrap();
            encoded_key = postcard::to_extend(&key, encoded_key).unwrap();
        }
        encoded_key
    }

    pub fn partial_counter_from_counter_key_v2(key: &[u8]) -> Counter {
        let (version, key) = postcard::take_from_bytes::<u8>(key).unwrap();
        match version {
            1u8 => {
                let CounterKey {
                    ns,
                    seconds,
                    conditions,
                    variables,
                } = postcard::from_bytes(key).unwrap();

                let map: HashMap<String, String> = variables
                    .into_iter()
                    .map(|(var, value)| (var.to_string(), value.to_string()))
                    .collect();
                let limit = Limit::new(
                    ns,
                    u64::default(),
                    seconds,
                    conditions
                        .into_iter()
                        .map(|p| p.try_into().expect("condition corrupted!")),
                    map.keys()
                        .map(|var| var.as_str().try_into().expect("variable corrupted!")),
                );
                Counter::resolved_vars(limit, map).expect("counter creation failed!")
            }
            2u8 => {
                let IdCounterKey { id, variables } = postcard::from_bytes(key).unwrap();
                let map: HashMap<String, String> = variables
                    .into_iter()
                    .map(|(var, value)| (var.to_string(), value.to_string()))
                    .collect();

                // we are not able to rebuild the full limit since we only have the id and variables.
                let limit = Limit::with_id::<&str, &str>(
                    id,
                    "",
                    u64::default(),
                    0,
                    vec![],
                    map.keys()
                        .map(|var| var.as_str().try_into().expect("variable corrupted!")),
                );
                Counter::resolved_vars(limit, map).expect("counter creation failed!")
            }
            _ => panic!("Unknown version: {}", version),
        }
    }

    pub fn key_for_counter(counter: &Counter) -> Vec<u8> {
        let key: CounterKey = counter.into();
        postcard::to_stdvec(&key).unwrap()
    }

    pub fn prefix_for_namespace(namespace: &str) -> Vec<u8> {
        postcard::to_stdvec(namespace).unwrap()
    }

    pub fn partial_counter_from_counter_key(key: &[u8]) -> Counter {
        let key: CounterKey = postcard::from_bytes(key).unwrap();
        let CounterKey {
            ns,
            seconds,
            conditions,
            variables,
        } = key;

        let map: HashMap<String, String> = variables
            .into_iter()
            .map(|(var, value)| (var.to_string(), value.to_string()))
            .collect();
        let limit = Limit::new(
            ns,
            u64::default(),
            seconds,
            conditions
                .into_iter()
                .map(|p| p.try_into().expect("condition corrupted!"))
                .collect::<Vec<Predicate>>(),
            map.keys()
                .map(|p| p.as_str().try_into().expect("variable corrupted!")),
        );
        Counter::resolved_vars(limit, map).unwrap()
    }

    #[cfg(test)]
    mod tests {
        use super::{
            key_for_counter, key_for_counter_v2, partial_counter_from_counter_key,
            prefix_for_namespace, CounterKey,
        };
        use crate::counter::Counter;
        use crate::Limit;
        use std::collections::HashMap;

        #[test]
        fn counter_key_serializes_and_back() {
            let namespace = "ns_counter:";
            let limit = Limit::new(
                namespace,
                1,
                2,
                vec!["foo == 'bar'".try_into().expect("failed parsing!")],
                vec![
                    "app_id".try_into().expect("failed parsing!"),
                    "role".try_into().expect("failed parsing!"),
                    "wat".try_into().expect("failed parsing!"),
                ],
            );
            let mut vars = HashMap::default();
            vars.insert("role".to_string(), "admin".to_string());
            vars.insert("app_id".to_string(), "123".to_string());
            vars.insert("wat".to_string(), "dunno".to_string());
            let ctx = vars.into();
            let counter = Counter::new(limit.clone(), &ctx)
                .expect("counter creation failed!")
                .expect("must have a counter");

            let raw = key_for_counter(&counter);
            let key_back: CounterKey =
                postcard::from_bytes(&raw).expect("This should deserialize back!");
            let key: CounterKey = (&counter).into();
            assert_eq!(key_back, key);
        }

        #[test]
        fn counter_key_and_counter_are_symmetric() {
            let namespace = "ns_counter:";
            let limit = Limit::new(
                namespace,
                1,
                1,
                vec!["req_method == 'GET'".try_into().expect("failed parsing!")],
                vec!["app_id".try_into().expect("failed parsing!")],
            );
            let mut variables = HashMap::default();
            variables.insert("app_id".to_string(), "123".to_string());
            let ctx = variables.into();
            let counter = Counter::new(limit.clone(), &ctx)
                .expect("counter creation failed!")
                .expect("must have a counter");
            let raw = key_for_counter(&counter);
            assert_eq!(counter, partial_counter_from_counter_key(&raw));
        }

        #[test]
        fn counter_key_starts_with_namespace_prefix() {
            let namespace = "ns_counter:";
            let limit = Limit::new(
                namespace,
                1,
                1,
                vec!["req_method == 'GET'".try_into().expect("failed parsing!")],
                vec!["app_id".try_into().expect("failed parsing!")],
            );
            let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
            let ctx = map.into();
            let counter = Counter::new(limit, &ctx)
                .expect("counter creation failed!")
                .expect("must have a counter");
            let serialized_counter = key_for_counter(&counter);

            let prefix = prefix_for_namespace(namespace);
            assert_eq!(&serialized_counter[..prefix.len()], &prefix);
        }

        #[test]
        fn counters_with_id() {
            let namespace = "ns_counter:";
            let limit_without_id = Limit::new(
                namespace,
                1,
                1,
                vec!["req_method == 'GET'".try_into().expect("failed parsing!")],
                vec!["app_id".try_into().expect("failed parsing!")],
            );
            let limit_with_id = Limit::with_id(
                "id200",
                namespace,
                1,
                1,
                vec!["req_method == 'GET'".try_into().expect("failed parsing!")],
                vec!["app_id".try_into().expect("failed parsing!")],
            );

            let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
            let ctx = map.into();
            let counter_with_id = Counter::new(limit_with_id, &ctx)
                .expect("counter creation failed!")
                .expect("must have a counter");
            let serialized_with_id_counter = key_for_counter(&counter_with_id);

            let counter_without_id = Counter::new(limit_without_id, &ctx)
                .expect("counter creation failed!")
                .expect("must have a counter");
            let serialized_without_id_counter = key_for_counter(&counter_without_id);

            // the original key_for_counter continues to encode kinda big
            assert_eq!(serialized_without_id_counter.len(), 46);
            assert_eq!(serialized_with_id_counter.len(), 46);

            // serialized_counter_v2 will only encode the id.... so it will be smaller for
            // counters with an id.
            let serialized_counter_with_id_v2 = key_for_counter_v2(&counter_with_id);
            assert_eq!(serialized_counter_with_id_v2.clone().len(), 19);

            // but continues to be large for counters without an id.
            let serialized_counter_without_id_v2 = key_for_counter_v2(&counter_without_id);
            assert_eq!(serialized_counter_without_id_v2.clone().len(), 47);
        }
    }
}