use std::time::Duration;
use bytesize::ByteSize;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use crate::configuration::mode::Mode;
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields, default)]
pub(crate) struct CooperativeCancellation {
enabled: bool,
mode: Mode,
#[serde(deserialize_with = "humantime_serde::deserialize")]
#[serde(serialize_with = "humantime_serde::serialize")]
#[schemars(with = "Option<String>")]
timeout: Option<Duration>,
#[schemars(with = "Option<String>", default)]
memory_limit: Option<ByteSize>,
}
impl Default for CooperativeCancellation {
fn default() -> Self {
Self {
enabled: true,
mode: Mode::Measure,
timeout: None,
memory_limit: None,
}
}
}
impl CooperativeCancellation {
pub(crate) fn timeout(&self) -> Option<Duration> {
self.timeout
}
pub(crate) fn memory_limit(&self) -> Option<ByteSize> {
self.memory_limit
}
#[cfg(test)]
pub(crate) fn enabled() -> Self {
Self {
enabled: true,
mode: Mode::Enforce,
timeout: None,
memory_limit: None,
}
}
pub(crate) fn is_enabled(&self) -> bool {
self.enabled
}
pub(crate) fn mode(&self) -> Mode {
self.mode
}
#[cfg(test)]
pub(crate) fn enabled_with_timeout(timeout: Duration) -> Self {
Self {
enabled: true,
mode: Mode::Enforce,
timeout: Some(timeout),
memory_limit: None,
}
}
#[cfg(test)]
pub(crate) fn measure_with_timeout(timeout: Duration) -> Self {
Self {
enabled: true,
mode: Mode::Measure,
timeout: Some(timeout),
memory_limit: None,
}
}
#[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix, test))]
pub(crate) fn enforce_with_memory_limit(memory_limit: ByteSize) -> Self {
Self {
enabled: true,
mode: Mode::Enforce,
timeout: None,
memory_limit: Some(memory_limit),
}
}
#[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix, test))]
pub(crate) fn measure_with_memory_limit(memory_limit: ByteSize) -> Self {
Self {
enabled: true,
mode: Mode::Measure,
timeout: None,
memory_limit: Some(memory_limit),
}
}
#[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix, test))]
pub(crate) fn enforce_with_timeout_and_memory_limit(
timeout: Duration,
memory_limit: ByteSize,
) -> Self {
Self {
enabled: true,
mode: Mode::Enforce,
timeout: Some(timeout),
memory_limit: Some(memory_limit),
}
}
#[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix, test))]
pub(crate) fn measure_with_timeout_and_memory_limit(
timeout: Duration,
memory_limit: ByteSize,
) -> Self {
Self {
enabled: true,
mode: Mode::Measure,
timeout: Some(timeout),
memory_limit: Some(memory_limit),
}
}
}