#[cfg(feature = "store")]
use crate::Error;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Limit {
pub name: &'static str,
pub bound: u64,
}
impl Limit {
#[cfg(feature = "store")]
pub(crate) fn refuse(self, wanted: u64, what: impl Into<String>) -> Error {
self.run_out(what).naming("wanted", wanted.to_string())
}
#[cfg(feature = "store")]
pub(crate) fn run_out(self, what: impl Into<String>) -> Error {
Error::limit(what).naming("limit", self.name).naming("bound", self.bound.to_string())
}
}
#[cfg(feature = "kv")]
pub const VALUE_BYTES: Limit = Limit { name: "value bytes", bound: 1 << 20 };
#[cfg(feature = "config")]
pub const SETTING_BYTES: Limit = Limit { name: "setting bytes", bound: 16 << 10 };
#[cfg(feature = "config")]
pub const CONFIG_BYTES: Limit = Limit { name: "config bytes", bound: 256 << 10 };
#[cfg(feature = "locks")]
pub const LOCK_WAITERS: Limit = Limit { name: "lock waiters", bound: 10_000 };
#[cfg(feature = "channels")]
pub const SUBSCRIPTIONS: Limit = Limit { name: "subscriptions", bound: 10_000 };
#[cfg(feature = "jobs")]
pub const JOB_BYTES: Limit = Limit { name: "job bytes", bound: 1 << 20 };
#[cfg(feature = "jobs")]
pub const PROGRESS_BYTES: Limit = Limit { name: "progress bytes", bound: 4 << 10 };
#[cfg(feature = "sql")]
pub const ROWS_BYTES: Limit = Limit { name: "bytes of a call's rows", bound: 64 << 20 };
#[cfg(feature = "records")]
pub const EVENT_BYTES: Limit = Limit { name: "event bytes", bound: 256 << 10 };
#[cfg(feature = "records")]
pub const APPEND_BYTES: Limit = Limit { name: "append bytes", bound: 4 << 20 };
#[cfg(feature = "records")]
pub const AGGREGATE_ROWS: Limit = Limit { name: "aggregate rows", bound: 10_000 };
#[cfg(feature = "records")]
pub const COUNT_BYTES: Limit = Limit { name: "count bytes", bound: 64 << 20 };
#[cfg(feature = "metrics")]
pub const INGEST_SAMPLES: Limit = Limit { name: "ingest samples", bound: 10_000 };
#[cfg(feature = "metrics")]
pub const INGEST_BYTES: Limit = Limit { name: "ingest bytes", bound: 4 << 20 };
#[cfg(feature = "metrics")]
pub const TEXT_SAMPLES: Limit = Limit { name: "text samples", bound: 100_000 };
#[cfg(feature = "metrics")]
pub const TEXT_BYTES: Limit = Limit { name: "text bytes", bound: 32 << 20 };
#[cfg(feature = "metrics")]
pub const QUERY_SERIES: Limit = Limit { name: "series", bound: 1000 };
#[cfg(feature = "metrics")]
pub const QUERY_BYTES: Limit = Limit { name: "bytes read", bound: 16 << 20 };
#[cfg(feature = "metrics")]
pub const QUERY_SAMPLES: Limit = Limit { name: "samples decoded", bound: 1 << 20 };
#[cfg(feature = "metrics")]
pub const QUERY_ANSWER: Limit = Limit { name: "answer", bound: 100_000 };
#[cfg(feature = "search")]
pub const SET_MANY_DOCUMENTS: Limit = Limit { name: "set_many documents", bound: 1000 };
#[cfg(feature = "search")]
pub const SET_MANY_BYTES: Limit = Limit { name: "set_many bytes", bound: 16 << 20 };
#[cfg(feature = "search")]
pub const DOCUMENT_BYTES: Limit = Limit { name: "document bytes", bound: 1 << 20 };
#[cfg(feature = "search")]
pub const SEARCH_HITS: Limit = Limit { name: "search hits", bound: 1000 };
#[cfg(feature = "search")]
pub const SEARCH_TEXT: Limit = Limit { name: "search text", bound: 1 << 10 };
#[cfg(feature = "search")]
pub const FILTER_CONDITIONS: Limit = Limit { name: "filter conditions", bound: 8 };
#[cfg(feature = "search")]
pub const FILTER_VALUES: Limit = Limit { name: "filter values", bound: 100 };
#[cfg(feature = "search")]
pub const FILTER_TEXT: Limit = Limit { name: "filter text", bound: 256 };
#[cfg(feature = "search")]
pub const VECTOR_DIMENSIONS: Limit = Limit { name: "vector dimensions", bound: 4096 };
#[cfg(feature = "wire")]
pub const REQUEST_BYTES: Limit = Limit { name: "request bytes", bound: 2 << 20 };
#[cfg(feature = "wire")]
pub const ANSWER_BYTES: Limit = Limit { name: "answer bytes", bound: 2 << 20 };
#[cfg(all(
test,
feature = "config",
feature = "locks",
feature = "channels",
feature = "jobs",
feature = "sql",
feature = "records",
feature = "metrics",
feature = "search",
feature = "wire"
))]
const ALL: &[Limit] = &[
VALUE_BYTES,
SETTING_BYTES,
CONFIG_BYTES,
LOCK_WAITERS,
SUBSCRIPTIONS,
JOB_BYTES,
PROGRESS_BYTES,
ROWS_BYTES,
EVENT_BYTES,
APPEND_BYTES,
AGGREGATE_ROWS,
COUNT_BYTES,
INGEST_SAMPLES,
INGEST_BYTES,
TEXT_SAMPLES,
TEXT_BYTES,
QUERY_SERIES,
QUERY_BYTES,
QUERY_SAMPLES,
QUERY_ANSWER,
SET_MANY_DOCUMENTS,
SET_MANY_BYTES,
DOCUMENT_BYTES,
SEARCH_HITS,
SEARCH_TEXT,
FILTER_CONDITIONS,
FILTER_VALUES,
FILTER_TEXT,
VECTOR_DIMENSIONS,
REQUEST_BYTES,
ANSWER_BYTES,
];
#[cfg(all(test, feature = "store"))]
mod tests {
use super::*;
#[test]
fn a_refusal_names_its_bound_what_was_wanted_and_the_bound() {
let refused = Limit { name: "widgets", bound: 3 }.refuse(4, "4 widgets, past 3");
assert_eq!(refused.to_string(), "4 widgets, past 3: limit");
let facts = (refused.fact("limit"), refused.fact("wanted"), refused.fact("bound"));
assert_eq!(facts, (Some("widgets"), Some("4"), Some("3")));
let gone = Limit { name: "ids", bound: 9 }.run_out("ids run out");
assert_eq!((gone.fact("limit"), gone.fact("wanted"), gone.fact("bound")), (Some("ids"), None, Some("9")));
}
#[test]
#[cfg(all(
feature = "config",
feature = "locks",
feature = "channels",
feature = "jobs",
feature = "sql",
feature = "records",
feature = "metrics",
feature = "search",
feature = "wire"
))]
fn the_table_every_sdk_reads_is_every_bound() {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../../testdata/wire/limits.json");
let mut written = String::from("{\n");
for (at, limit) in ALL.iter().enumerate() {
let comma = if at + 1 < ALL.len() { "," } else { "" };
written.push_str(&format!(" {:?}: {}{comma}\n", limit.name, limit.bound));
}
written.push_str("}\n");
let kept = std::fs::read_to_string(path).unwrap_or_default().replace("\r\n", "\n");
assert_eq!(kept, written, "testdata/wire/limits.json is every bound: write the right side to it");
let names: std::collections::HashSet<_> = ALL.iter().map(|limit| limit.name).collect();
assert_eq!(names.len(), ALL.len(), "each bound has a name of its own");
}
}