holochain_integrity_types/
rate_limit.rs

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