impl ResourceAllocation {
fn new(
operation_id: String,
permit: tokio::sync::OwnedSemaphorePermit,
active_operations: Arc<RwLock<HashMap<String, OperationContext>>>,
) -> Self {
Self {
operation_id,
permit,
active_operations,
}
}
}
impl Drop for ResourceAllocation {
fn drop(&mut self) {
let operation_id = self.operation_id.clone();
let active_ops = self.active_operations.clone();
tokio::spawn(async move {
let mut ops = active_ops.write().await;
ops.remove(&operation_id);
});
}
}
impl ResourceEnforcementStats {
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn format_diagnostic(&self) -> String {
let success_rate = if self.total_requests > 0 {
(self.allowed_requests as f64 / self.total_requests as f64) * 100.0
} else {
100.0
};
format!(
"Resource Control Stats (5min window):\n\
- Total requests: {}\n\
- Success rate: {:.1}%\n\
- Allowed: {}, Throttled: {}, Queued: {}, Rejected: {}\n\
- Active operations: {}",
self.total_requests,
success_rate,
self.allowed_requests,
self.throttled_requests,
self.queued_requests,
self.rejected_requests,
self.current_active_operations
)
}
}
impl ResourceControllerFactory {
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn create_default() -> PlatformResourceController {
PlatformResourceController::new(ResourceLimits::default())
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn create_dev_optimized() -> PlatformResourceController {
let limits = ResourceLimits {
max_memory_mb: 512.0, max_concurrent_ops: 5, check_interval_secs: 2, ..Default::default()
};
PlatformResourceController::new(limits)
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn create_prod_optimized() -> PlatformResourceController {
let limits = ResourceLimits {
max_memory_mb: 2048.0, max_concurrent_ops: 50, check_interval_secs: 10, cpu_warning_threshold: 0.5, memory_warning_threshold: 0.6, ..Default::default()
};
PlatformResourceController::new(limits)
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn create_ci_optimized() -> PlatformResourceController {
let limits = ResourceLimits {
max_memory_mb: 1024.0,
max_cpu_utilization: 0.9, max_concurrent_ops: 10,
check_interval_secs: 5,
cpu_warning_threshold: 0.8,
memory_warning_threshold: 0.8,
};
PlatformResourceController::new(limits)
}
}