Skip to content

Commit

Permalink
introduce memory limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Nov 4, 2024
1 parent f9d2b75 commit abeb10f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/kitsune-wasm-mrf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fred = { workspace = true }
futures-util = { version = "0.3.31", default-features = false, features = [
"alloc",
] }
human-size = "0.4.3"
kitsune-config = { workspace = true }
kitsune-derive = { workspace = true }
kitsune-error = { workspace = true }
Expand Down
81 changes: 69 additions & 12 deletions crates/kitsune-wasm-mrf/src/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{kv_storage, mrf_wit::v1::fep::mrf::keyvalue};
use crate::{
kv_storage,
mrf_wit::v1::fep::mrf::{http, keyvalue},
};
use slab::Slab;
use triomphe::Arc;
use wasmtime::{
Expand All @@ -7,6 +10,8 @@ use wasmtime::{
};
use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiView};

const TABLE_ELEMENT_SIZE: usize = std::mem::size_of::<usize>();

pub struct KvContext {
pub module_name: Option<String>,
pub storage: Arc<kv_storage::BackendDispatch>,
Expand All @@ -23,8 +28,49 @@ impl KvContext {
}
}

pub struct HttpContext {
pub client: kitsune_http_client::Client,
pub bodies: Slab<todo!()>,
}

impl HttpContext {
#[inline]
pub fn get_body(&self, rep: &Resource<http::ResponseBody>) -> &todo!() {
&self.bodies[rep.rep() as usize]
}
}

pub struct ResourceLimiter {
pub max_memory_size: human_size::Size,
pub used_memory: usize,
}

impl wasmtime::ResourceLimiter for ResourceLimiter {
fn memory_growing(
&mut self,
current: usize,
desired: usize,
_maximum: Option<usize>,
) -> wasmtime::Result<bool> {
self.used_memory += desired - current;
Ok(self.used_memory <= self.max_memory_size.to_bytes() as usize)
}

fn table_growing(
&mut self,
current: usize,
desired: usize,
_maximum: Option<usize>,
) -> wasmtime::Result<bool> {
self.used_memory += TABLE_ELEMENT_SIZE * (desired - current);
Ok(self.used_memory <= self.max_memory_size.to_bytes() as usize)
}
}

pub struct Context {
pub http_ctx: HttpContext,
pub kv_ctx: KvContext,
pub resource_limiter: ResourceLimiter,
pub resource_table: ResourceTable,
pub wasi_ctx: WasiCtx,
}
Expand All @@ -50,16 +96,27 @@ pub fn construct_store(
.allow_udp(false)
.build();

Store::new(
engine,
Context {
kv_ctx: KvContext {
module_name: None,
storage,
buckets: Slab::new(),
},
resource_table: ResourceTable::new(),
wasi_ctx,
let data = Context {
http_ctx: HttpContext {
client: kitsune_http_client::Client::builder()
.content_length_limit(None)
.build(),
bodies: Slab::new(),
},
kv_ctx: KvContext {
module_name: None,
storage,
buckets: Slab::new(),
},
)
resource_limiter: ResourceLimiter {
max_memory_size: human_size::Size::new(100, human_size::Any::Mebibyte).unwrap(),
used_memory: 0,
},
resource_table: ResourceTable::new(),
wasi_ctx,
};

let mut store = Store::new(engine, data);
store.limiter(|store| &mut store.resource_limiter);
store
}

0 comments on commit abeb10f

Please sign in to comment.