holochain_integrity_types/
rate_limit.rs

1//! Rate limiting data types
2
3use holochain_serialized_bytes::prelude::*;
4
5/// A bucket ID, for rate limiting
6pub type RateBucketId = u8;
7
8/// The weight of this action, for rate limiting
9pub type RateUnits = u8;
10
11/// The normalized total size of this action, for rate limiting
12pub type RateBytes = u8;
13
14/// The amount that a bucket is "filled"
15pub type RateBucketCapacity = u32;
16
17/// Combination of two rate limiting data types, for convenience
18#[derive(
19    Debug,
20    Clone,
21    serde::Serialize,
22    serde::Deserialize,
23    PartialEq,
24    Eq,
25    SerializedBytes,
26    Hash,
27    PartialOrd,
28    Ord,
29)]
30#[allow(missing_docs)]
31pub struct RateWeight {
32    pub bucket_id: RateBucketId,
33    pub units: RateUnits,
34}
35
36impl Default for RateWeight {
37    fn default() -> Self {
38        Self {
39            bucket_id: 255,
40            units: 0,
41        }
42    }
43}
44
45/// Combination of the three main rate limiting data types, for convenience
46#[derive(
47    Debug,
48    Clone,
49    serde::Serialize,
50    serde::Deserialize,
51    PartialEq,
52    Eq,
53    SerializedBytes,
54    Hash,
55    PartialOrd,
56    Ord,
57)]
58#[allow(missing_docs)]
59pub struct EntryRateWeight {
60    pub bucket_id: RateBucketId,
61    pub units: RateUnits,
62    pub rate_bytes: RateBytes,
63}
64
65impl Default for EntryRateWeight {
66    fn default() -> Self {
67        Self {
68            bucket_id: 255,
69            units: 0,
70            rate_bytes: 0,
71        }
72    }
73}
74
75impl From<EntryRateWeight> for RateWeight {
76    fn from(w: EntryRateWeight) -> Self {
77        Self {
78            bucket_id: w.bucket_id,
79            units: w.units,
80        }
81    }
82}