use crate::extensions::flow_state::{CasOutcome as StoreCas, FlowStore, flow_result};
use std::sync::Arc;
use wasmtime::component::ResourceTable;
use wasmtime::{ResourceLimiter, StoreLimits};
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
pub(super) mod bindings {
wasmtime::component::bindgen!({
world: "mock",
path: "wit",
imports: { "rift:mock/state": async | trappable, default: trappable },
exports: { default: async },
});
}
use bindings::rift::mock::state::{CasOutcome, Host as StateHost};
pub(super) struct Limits {
pub(super) inner: StoreLimits,
pub(super) memory_cap: usize,
pub(super) memory_total: usize,
pub(super) memory_refused: bool,
}
impl ResourceLimiter for Limits {
fn memory_growing(
&mut self,
current: usize,
desired: usize,
_maximum: Option<usize>,
) -> wasmtime::Result<bool> {
let total = self
.memory_total
.saturating_sub(current)
.saturating_add(desired);
if total > self.memory_cap {
self.memory_refused = true;
return Err(wasmtime::Error::msg(format!(
"growing memory to {desired} bytes would bring the call's memories to {total} \
bytes, past {} bytes",
self.memory_cap
)));
}
self.memory_total = total;
Ok(true)
}
fn table_growing(
&mut self,
current: usize,
desired: usize,
maximum: Option<usize>,
) -> wasmtime::Result<bool> {
self.inner.table_growing(current, desired, maximum)
}
fn instances(&self) -> usize {
self.inner.instances()
}
fn tables(&self) -> usize {
self.inner.tables()
}
fn memories(&self) -> usize {
self.inner.memories()
}
}
pub(super) struct Host {
pub(super) wasi: WasiCtx,
pub(super) table: ResourceTable,
pub(super) http: wasmtime_wasi_http::WasiHttpCtx,
pub(super) http_hooks: NoOutgoing,
pub(super) limits: Limits,
pub(super) flow_store: Arc<dyn FlowStore>,
pub(super) flow_id: String,
pub(super) rng: crate::imposter::rng::RequestRng,
pub(super) clock: CallClock,
pub(super) deadline: Option<std::time::Instant>,
pub(super) read_only: bool,
}
pub(super) const READ_ONLY: &str = "read-only during snapshot";
pub(super) type CallClock = Arc<parking_lot::RwLock<Option<crate::scripting::ScriptClock>>>;
impl WasiView for Host {
fn ctx(&mut self) -> WasiCtxView<'_> {
WasiCtxView {
ctx: &mut self.wasi,
table: &mut self.table,
}
}
}
impl bindings::rift::mock::types::Host for Host {}
impl wasmtime_wasi_http::WasiHttpView for Host {
fn http(&mut self) -> wasmtime_wasi_http::WasiHttpCtxView<'_> {
wasmtime_wasi_http::WasiHttpCtxView {
ctx: &mut self.http,
table: &mut self.table,
hooks: &mut self.http_hooks,
}
}
}
pub(super) struct NoOutgoing;
pub(super) const BODY_CHUNK_BYTES: usize = 64 * 1024;
pub(super) const BODY_BUFFER_CHUNKS: usize = 1;
pub(super) const FIELD_SIZE_BYTES: usize = 32 * 1024;
type OutgoingFuture =
Box<dyn std::future::Future<Output = Result<(), wasmtime_wasi_http::Error>> + Send>;
impl wasmtime_wasi_http::WasiHttpHooks for NoOutgoing {
fn send_request(
&mut self,
_request: hyper::Request<wasmtime_wasi_http::WasiBody>,
_options: Option<wasmtime_wasi_http::RequestOptions>,
_fut: OutgoingFuture,
) -> Box<
dyn std::future::Future<
Output = Result<
(
hyper::Response<wasmtime_wasi_http::WasiBody>,
OutgoingFuture,
),
wasmtime_wasi_http::Error,
>,
> + Send,
> {
Box::new(async { Err(wasmtime_wasi_http::Error::HttpRequestDenied) })
}
fn p2_outgoing_body_buffer_chunks(&mut self) -> usize {
BODY_BUFFER_CHUNKS
}
fn p2_outgoing_body_chunk_size(&mut self) -> usize {
BODY_CHUNK_BYTES
}
}
fn from_text(op: &str, text: &str) -> Result<serde_json::Value, String> {
serde_json::from_str(text).map_err(|e| format!("{op}: value is not JSON: {e}"))
}
impl Host {
fn flow(&self, flow: Option<String>) -> String {
flow.unwrap_or_else(|| self.flow_id.clone())
}
}
async fn store_op<T, F>(
store: Arc<dyn FlowStore>,
name: &'static str,
op: F,
) -> wasmtime::Result<Result<T, String>>
where
T: Send + 'static,
F: FnOnce(&dyn FlowStore) -> anyhow::Result<T> + Send + 'static,
{
let result = if store.is_blocking() {
tokio::task::spawn_blocking(move || op(store.as_ref()))
.await
.map_err(|e| wasmtime::Error::msg(format!("flow store task failed: {e}")))?
} else {
op(store.as_ref())
};
Ok(flow_result(name, result))
}
impl StateHost for Host {
async fn get(
&mut self,
key: String,
flow: Option<String>,
) -> wasmtime::Result<Result<Option<String>, String>> {
let flow = self.flow(flow);
let got = store_op(Arc::clone(&self.flow_store), "get", move |s| {
s.get(&flow, &key)
})
.await?;
Ok(got.map(|v| v.as_ref().map(serde_json::Value::to_string)))
}
async fn set(
&mut self,
key: String,
value: String,
flow: Option<String>,
) -> wasmtime::Result<Result<(), String>> {
if self.read_only {
return Ok(Err(READ_ONLY.to_owned()));
}
let value = match from_text("set", &value) {
Ok(v) => v,
Err(e) => return Ok(Err(e)),
};
let flow = self.flow(flow);
store_op(Arc::clone(&self.flow_store), "set", move |s| {
s.set(&flow, &key, value)
})
.await
}
async fn incr_by(
&mut self,
key: String,
by: i64,
flow: Option<String>,
) -> wasmtime::Result<Result<i64, String>> {
if self.read_only {
return Ok(Err(READ_ONLY.to_owned()));
}
let flow = self.flow(flow);
store_op(Arc::clone(&self.flow_store), "incrementBy", move |s| {
s.increment_by(&flow, &key, by)
})
.await
}
async fn exists(
&mut self,
key: String,
flow: Option<String>,
) -> wasmtime::Result<Result<bool, String>> {
let flow = self.flow(flow);
store_op(Arc::clone(&self.flow_store), "exists", move |s| {
s.exists(&flow, &key)
})
.await
}
async fn delete(
&mut self,
key: String,
flow: Option<String>,
) -> wasmtime::Result<Result<(), String>> {
if self.read_only {
return Ok(Err(READ_ONLY.to_owned()));
}
let flow = self.flow(flow);
store_op(Arc::clone(&self.flow_store), "delete", move |s| {
s.delete(&flow, &key)
})
.await
}
async fn cas(
&mut self,
key: String,
expected: Option<String>,
new: String,
flow: Option<String>,
) -> wasmtime::Result<Result<CasOutcome, String>> {
if self.read_only {
return Ok(Err(READ_ONLY.to_owned()));
}
let expected = match expected.as_deref().map(|t| from_text("cas", t)).transpose() {
Ok(v) => v,
Err(e) => return Ok(Err(e)),
};
let new = match from_text("cas", &new) {
Ok(v) => v,
Err(e) => return Ok(Err(e)),
};
let flow = self.flow(flow);
let outcome = store_op(Arc::clone(&self.flow_store), "cas", move |s| {
s.compare_and_set(&flow, &key, expected.as_ref(), new)
})
.await?;
Ok(outcome.map(|o| match o {
StoreCas::Applied => CasOutcome::Applied,
StoreCas::Conflict(current) => {
CasOutcome::Conflict(current.as_ref().map(serde_json::Value::to_string))
}
}))
}
async fn ttl(
&mut self,
seconds: i64,
flow: Option<String>,
) -> wasmtime::Result<Result<(), String>> {
if self.read_only {
return Ok(Err(READ_ONLY.to_owned()));
}
let flow = self.flow(flow);
store_op(Arc::clone(&self.flow_store), "setTtl", move |s| {
s.set_ttl(&flow, seconds)
})
.await
}
async fn key_ttl(
&mut self,
key: String,
seconds: i64,
flow: Option<String>,
) -> wasmtime::Result<Result<bool, String>> {
if self.read_only {
return Ok(Err(READ_ONLY.to_owned()));
}
let flow = self.flow(flow);
store_op(Arc::clone(&self.flow_store), "setKeyTtl", move |s| {
s.set_key_ttl(&flow, &key, seconds)
})
.await
}
async fn clear(&mut self, flow: Option<String>) -> wasmtime::Result<Result<(), String>> {
if self.read_only {
return Ok(Err(READ_ONLY.to_owned()));
}
let flow = self.flow(flow);
store_op(Arc::clone(&self.flow_store), "clear", move |s| {
s.clear_flow(&flow)
})
.await
}
}