1use anyhow::Result;
3use ubl_types::{ActorId, Dim};
4use std::collections::HashMap;
5
6pub trait Middleware: Send + Sync {
8 fn before(&self, _dim: Dim, _actor: &ActorId, _in: &[u8], _ctx: &crate::AppCtx) -> Result<()> {
14 Ok(())
15 }
16 fn after(
22 &self,
23 _dim: Dim,
24 _actor: &ActorId,
25 _in: &[u8],
26 _out: &[u8],
27 _ctx: &crate::AppCtx,
28 ) -> Result<()> {
29 Ok(())
30 }
31}
32
33#[derive(Default)]
35pub struct Budgets {
36 map: HashMap<String, i64>,
37}
38impl Budgets {
39 pub fn set(&mut self, actor: &ActorId, quota: i64) {
41 self.map.insert(actor.0.clone(), quota);
42 }
43 pub fn consume(&mut self, actor: &ActorId, amount: i64) -> Option<i64> {
45 self.map.get_mut(&actor.0).map(|v| {
46 *v = (*v - amount).max(0);
47 *v
48 })
49 }
50}