use std::collections::HashMap;
use std::time::Duration;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TenantLimits {
pub max_concurrent_tasks: Option<usize>,
pub executor_timeout: Option<Duration>,
pub event_queue_capacity: Option<usize>,
#[deprecated(
since = "0.10.0",
note = "never enforced; use TenantAwareInMemoryTaskStore::with_tenant_override \
to give a tenant its own TaskStoreConfig::max_capacity"
)]
pub max_stored_tasks: Option<usize>,
pub rate_limit_rps: Option<u32>,
}
impl TenantLimits {
#[mutants::skip]
#[must_use]
pub fn builder() -> TenantLimitsBuilder {
TenantLimitsBuilder::default()
}
#[must_use]
pub const fn with_max_concurrent_tasks(mut self, n: Option<usize>) -> Self {
self.max_concurrent_tasks = n;
self
}
#[must_use]
pub const fn with_executor_timeout(mut self, d: Option<Duration>) -> Self {
self.executor_timeout = d;
self
}
#[must_use]
pub const fn with_event_queue_capacity(mut self, n: Option<usize>) -> Self {
self.event_queue_capacity = n;
self
}
#[must_use]
#[deprecated(
since = "0.12.0",
note = "never enforced; use TenantAwareInMemoryTaskStore::with_tenant_override \
to give a tenant its own TaskStoreConfig::max_capacity"
)]
#[allow(deprecated)]
pub const fn with_max_stored_tasks(mut self, n: Option<usize>) -> Self {
self.max_stored_tasks = n;
self
}
#[must_use]
pub const fn with_rate_limit_rps(mut self, rps: Option<u32>) -> Self {
self.rate_limit_rps = rps;
self
}
}
#[derive(Debug, Clone, Default)]
pub struct TenantLimitsBuilder {
max_concurrent_tasks: Option<usize>,
executor_timeout: Option<Duration>,
event_queue_capacity: Option<usize>,
max_stored_tasks: Option<usize>,
rate_limit_rps: Option<u32>,
}
impl TenantLimitsBuilder {
#[must_use]
pub const fn max_concurrent_tasks(mut self, n: usize) -> Self {
self.max_concurrent_tasks = Some(n);
self
}
#[must_use]
pub const fn executor_timeout(mut self, d: Duration) -> Self {
self.executor_timeout = Some(d);
self
}
#[must_use]
pub const fn event_queue_capacity(mut self, n: usize) -> Self {
self.event_queue_capacity = Some(n);
self
}
#[must_use]
#[deprecated(
note = "never enforced; use TenantAwareInMemoryTaskStore::with_tenant_override \
to give a tenant its own TaskStoreConfig::max_capacity"
)]
pub const fn max_stored_tasks(mut self, n: usize) -> Self {
self.max_stored_tasks = Some(n);
self
}
#[must_use]
pub const fn rate_limit_rps(mut self, rps: u32) -> Self {
self.rate_limit_rps = Some(rps);
self
}
#[must_use]
#[allow(deprecated)]
pub const fn build(self) -> TenantLimits {
TenantLimits {
max_concurrent_tasks: self.max_concurrent_tasks,
executor_timeout: self.executor_timeout,
event_queue_capacity: self.event_queue_capacity,
max_stored_tasks: self.max_stored_tasks,
rate_limit_rps: self.rate_limit_rps,
}
}
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct PerTenantConfig {
pub default: TenantLimits,
pub overrides: HashMap<String, TenantLimits>,
}
impl PerTenantConfig {
#[mutants::skip]
#[must_use]
pub fn builder() -> PerTenantConfigBuilder {
PerTenantConfigBuilder::default()
}
#[must_use]
pub const fn with_default(mut self, limits: TenantLimits) -> Self {
self.default = limits;
self
}
#[must_use]
pub fn with_overrides(mut self, overrides: HashMap<String, TenantLimits>) -> Self {
self.overrides = overrides;
self
}
#[must_use]
pub fn with_override(mut self, tenant_id: impl Into<String>, limits: TenantLimits) -> Self {
self.overrides.insert(tenant_id.into(), limits);
self
}
#[must_use]
pub fn get(&self, tenant_id: &str) -> &TenantLimits {
self.overrides.get(tenant_id).unwrap_or(&self.default)
}
}
#[derive(Debug, Clone, Default)]
pub struct PerTenantConfigBuilder {
default: TenantLimits,
overrides: HashMap<String, TenantLimits>,
}
impl PerTenantConfigBuilder {
#[must_use]
pub const fn default_limits(mut self, limits: TenantLimits) -> Self {
self.default = limits;
self
}
#[must_use]
pub fn with_override(mut self, tenant_id: impl Into<String>, limits: TenantLimits) -> Self {
self.overrides.insert(tenant_id.into(), limits);
self
}
#[must_use]
pub fn build(self) -> PerTenantConfig {
PerTenantConfig {
default: self.default,
overrides: self.overrides,
}
}
}
#[cfg(test)]
mod tests;