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 665036a
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions crates/kitsune-wasm-mrf/src/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
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::{
component::{Resource, ResourceTable},
Engine, Store,
Engine, Store, StoreLimits, StoreLimitsBuilder,
};
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,22 @@ 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 Context {
pub http_ctx: HttpContext,
pub kv_ctx: KvContext,
pub resource_limiter: StoreLimits,
pub resource_table: ResourceTable,
pub wasi_ctx: WasiCtx,
}
Expand All @@ -50,16 +69,26 @@ 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: StoreLimitsBuilder::new()
.memory_size(100 * 1024 * 1024)
.build(),
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 665036a

Please sign in to comment.