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

Commit

Permalink
Merge pull request #28 from radu-matei/spawn-blocking
Browse files Browse the repository at this point in the history
Send HTTP request on a new spawned executor
  • Loading branch information
Radu M authored Mar 8, 2021
2 parents 6f8c76e + c777c5e commit 886d88b
Showing 1 changed file with 26 additions and 15 deletions.
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

0 comments on commit 886d88b

Please sign in to comment.