Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Send HTTP request on a new spawned executor #28

Merged
merged 1 commit into from
Mar 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions crates/wasi-experimental-http-wasmtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,33 @@ fn request(
body: Vec<u8>,
) -> Result<(u16, HeaderMap<HeaderValue>, Bytes), Error> {
match Handle::try_current() {
Ok(_) => {
Ok(r) => {
// If running in a Tokio runtime, spawn a new blocking executor
// that will send the HTTP request, and block on its execution.
// This attempts to avoid any deadlocks from other operations
// already executing on the same executor (compared with just
// blocking on the current one).
//
// This should only be a temporary workaround, until we take
// advantage of async functions in Wasmtime.

println!("wasi_experimental_http::request: tokio runtime available");
let client = Client::builder().build().unwrap();
let res = block_on(
client
.request(method, &url)
.headers(headers)
.body(body)
.send(),
)?;
block_on(r.spawn_blocking(move || {
let client = Client::builder().build().unwrap();
let res = block_on(
client
.request(method, &url)
.headers(headers)
.body(body)
.send(),
)?;

return Ok((
res.status().as_u16(),
res.headers().clone(),
block_on(res.bytes())?,
));
return Ok((
res.status().as_u16(),
res.headers().clone(),
block_on(res.bytes())?,
));
}))?
}
Err(_) => {
println!("wasi_experimental_http::request: no Tokio runtime available");
Expand All @@ -200,7 +211,7 @@ fn request(
.send()?;
return Ok((res.status().as_u16(), res.headers().clone(), res.bytes()?));
}
};
}
}

/// Get the URL, method, request body, and headers from the
Expand Down