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

Commit

Permalink
Spawn new blocking executor for sending the HTTP request in Tokio
Browse files Browse the repository at this point in the history
Signed-off-by: Radu M <root@radu.sh>
  • Loading branch information
Radu M committed Mar 6, 2021
1 parent 29562e1 commit 7e5b82f
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 7e5b82f

Please sign in to comment.