Skip to content
Merged
Show file tree
Hide file tree
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
33 changes: 31 additions & 2 deletions crates/http-service/src/executor/wasi_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ where

let duration = Duration::from_millis(store.data().timeout);

/*
Channel to receive http status code for asynchronious response processing of WASI-HTTP.
*/
let (status_code_tx, status_code_rx) = tokio::sync::oneshot::channel();

let task = tokio::task::spawn(async move {
if let Err(e) = tokio::time::timeout(
duration,
Expand All @@ -142,16 +147,40 @@ where
return Err(e);
};
let elapsed = Instant::now().duration_since(start_);
/*
Used by WASI-HTTP to send status code prior response processing.
If there is no status code then default value is returned.
For synchronious HTTP processing the status_code parameter is set and no value in the channel.
*/
let status_code = status_code_rx.await.unwrap_or_else(|error| {
tracing::trace!(cause=?error, "unknown status code");
StatusCode::default()
});

on_response(
StatusCode::default(),
status_code,
ByteSize::b(store.memory_used() as u64),
elapsed,
);
Ok(())
});

match receiver.await {
Ok(Ok(response)) => Ok(response),
Ok(Ok(response)) => {
/*
Status code sender is closed if response handler processing was done.
*/
if !status_code_tx.is_closed() {
if let Err(error) = status_code_tx.send(response.status()) {
tracing::warn!(cause=?error, "sending status code")
}
tracing::debug!("returned status code: '{}'", response.status(),);
} else {
tracing::warn!("status code sender is closed");
}

Ok(response)
}
Ok(Err(e)) => Err(e.into()),
Err(_) => {
let e = match task.await {
Expand Down
28 changes: 0 additions & 28 deletions crates/http-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,6 @@ where
}
};

/*
Channel to receive http status code for asynchronious response processing of WASI-HTTP.
*/
let (status_code_tx, mut status_code_rx) = tokio::sync::oneshot::channel();
let start_ = std::time::Instant::now();

let response_handler = {
Expand All @@ -304,16 +300,6 @@ where
let context = self.context.clone();

move |status_code: StatusCode, mem_used: ByteSize, time_elapsed: Duration| {
tracing::trace!(?status_code, ?mem_used, ?time_elapsed, "response handler");
/*
Used by WASI-HTTP to send status code prior response processing.
If there is no status code then default value is returned.
For synchronious HTTP processing the status_code parameter is set and no value in the channel.
*/
let status_code = status_code_rx.try_recv().unwrap_or_else(|error| {
tracing::trace!(cause=?error, "unknown status code");
status_code
});
tracing::info!(
"'{}' completed with status code: '{}' in {:.0?} using {} of WebAssembly heap",
app_name,
Expand Down Expand Up @@ -350,20 +336,6 @@ where

let response = match executor.execute(request, response_handler).await {
Ok(mut response) => {
/*
Status code sender is closed if response handler processing was done.
*/
if !status_code_tx.is_closed() {
if let Err(error) = status_code_tx.send(response.status()) {
tracing::warn!(cause=?error, "sending status code")
}
tracing::info!(
"'{}' returned status code: '{}'",
app_name,
response.status(),
);
}

response.headers_mut().extend(app_res_headers(cfg));
response
}
Expand Down