Skip to content

Commit

Permalink
Cancel before receiving a response body
Browse files Browse the repository at this point in the history
  • Loading branch information
mayuki committed Jan 10, 2024
1 parent 0ecbcb5 commit 6fa47c3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions native/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"yaha_native",
]
25 changes: 17 additions & 8 deletions native/yaha_native/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,14 +460,23 @@ pub extern "C" fn yaha_request_begin(
}

// Send a request and wait for response status and headers.
let res = ctx.client.as_ref().unwrap().request(req).await;
if let Err(err) = res {
LAST_ERROR.with(|v| {
*v.borrow_mut() = Some(err.to_string());
});
(ctx.on_complete)(seq, state, CompletionReason::Error);
return;
}
let res = select! {
_ = cancellation_token.cancelled() => {
(ctx.on_complete)(seq, state, CompletionReason::Aborted);
return;
}
res = ctx.client.as_ref().unwrap().request(req) => {
if let Err(err) = res {
LAST_ERROR.with(|v| {
*v.borrow_mut() = Some(err.to_string());
});
(ctx.on_complete)(seq, state, CompletionReason::Error);
return;
} else {
res
}
}
};

// Status code and response headers are received.
let mut res = res.unwrap();
Expand Down

0 comments on commit 6fa47c3

Please sign in to comment.