use std::fmt;
use super::super::ensure;
use super::super::protocol::JsonValue;
use super::BrokerError;
const MAX_WIDGETS: usize = 1_024;
const MAX_WIDGETS_PER_TENANT: usize = 128;
const MAX_ML_PEERS: usize = 64;
const MAX_ML_PEERS_PER_TENANT: usize = 8;
const MAX_SUBSCRIPTIONS: usize = 4_096;
const MAX_SUBSCRIPTIONS_PER_TENANT: usize = 512;
const MAX_TAPS: usize = 64;
const MAX_TAPS_PER_TENANT: usize = 8;
const MAX_PAYLOAD_BYTES: usize = 65_536;
const NUMBER_BYTES: usize = 24;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Limit {
Widgets,
WidgetsPerTenant,
MlPeers,
MlPeersPerTenant,
Subscriptions,
SubscriptionsPerTenant,
Taps,
TapsPerTenant,
PayloadBytes,
}
impl Limit {
#[must_use]
pub const fn cap(self) -> usize {
match self {
Self::Widgets => MAX_WIDGETS,
Self::WidgetsPerTenant => MAX_WIDGETS_PER_TENANT,
Self::MlPeers => MAX_ML_PEERS,
Self::MlPeersPerTenant => MAX_ML_PEERS_PER_TENANT,
Self::Subscriptions => MAX_SUBSCRIPTIONS,
Self::SubscriptionsPerTenant => MAX_SUBSCRIPTIONS_PER_TENANT,
Self::Taps => MAX_TAPS,
Self::TapsPerTenant => MAX_TAPS_PER_TENANT,
Self::PayloadBytes => MAX_PAYLOAD_BYTES,
}
}
}
impl fmt::Display for Limit {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match *self {
Self::Widgets => "widgets",
Self::WidgetsPerTenant => "widgets per tenant",
Self::MlPeers => "ML peers",
Self::MlPeersPerTenant => "ML peers per tenant",
Self::Subscriptions => "subscriptions",
Self::SubscriptionsPerTenant => "subscriptions per tenant",
Self::Taps => "taps",
Self::TapsPerTenant => "taps per tenant",
Self::PayloadBytes => "payload bytes",
};
write!(formatter, "{name} (bound {})", self.cap())
}
}
pub(super) fn within(used: usize, limit: Limit) -> Result<(), BrokerError> {
ensure(used < limit.cap(), || BrokerError::LimitExceeded(limit))
}
fn within_cap(used: usize, cap: usize) -> Result<(), BrokerError> {
ensure(used <= cap, || {
BrokerError::LimitExceeded(Limit::PayloadBytes)
})
}
pub(super) fn tenant_count<'owners>(
owners: impl Iterator<Item = &'owners str>,
tenant: &str,
) -> usize {
owners.filter(|owner| *owner == tenant).count()
}
fn string_bytes(text: &str) -> usize {
text.chars().fold(2_usize, |total, character| {
let width = match character {
'"' | '\\' | '\n' | '\r' | '\t' | '\u{8}' | '\u{c}' => 2,
control if control < ' ' => 6,
other => other.len_utf8(),
};
total.saturating_add(width)
})
}
pub(super) fn payload_within(payload: &JsonValue) -> Result<(), BrokerError> {
let cap = Limit::PayloadBytes.cap();
let mut used = 0_usize;
let mut pending = vec![payload];
while let Some(next) = pending.pop() {
let own = match *next {
JsonValue::Null | JsonValue::Bool(true) => 4,
JsonValue::Bool(false) => 5,
JsonValue::Number(_) => NUMBER_BYTES,
JsonValue::String(ref text) => string_bytes(text),
JsonValue::Array(ref items) => {
pending.extend(items.iter());
items.len().saturating_sub(1).saturating_add(2)
}
JsonValue::Object(ref members) => {
let mut keys = members.len().saturating_sub(1).saturating_add(2);
for (key, member) in members {
keys = keys.saturating_add(string_bytes(key).saturating_add(1));
pending.push(member);
}
keys
}
};
used = used.saturating_add(own);
within_cap(used, cap)?;
}
Ok(())
}