use anyhow::Result;
use ubl_types::{ActorId, Dim};
use std::collections::HashMap;
pub trait Middleware: Send + Sync {
fn before(&self, _dim: Dim, _actor: &ActorId, _in: &[u8], _ctx: &crate::AppCtx) -> Result<()> {
Ok(())
}
fn after(
&self,
_dim: Dim,
_actor: &ActorId,
_in: &[u8],
_out: &[u8],
_ctx: &crate::AppCtx,
) -> Result<()> {
Ok(())
}
}
#[derive(Default)]
pub struct Budgets {
map: HashMap<String, i64>,
}
impl Budgets {
pub fn set(&mut self, actor: &ActorId, quota: i64) {
self.map.insert(actor.0.clone(), quota);
}
pub fn consume(&mut self, actor: &ActorId, amount: i64) -> Option<i64> {
self.map.get_mut(&actor.0).map(|v| {
*v = (*v - amount).max(0);
*v
})
}
}