-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e5dd1e
commit 76f4ef3
Showing
6 changed files
with
126 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::future::Future; | ||
|
||
use tokio::select; | ||
use tokio_util::sync::CancellationToken; | ||
|
||
pub(crate) async fn with_cancellation_handler<RequestF, CancellationF, Response>( | ||
request_future: RequestF, | ||
cancellation_future: CancellationF, | ||
) -> Result<tonic::Response<Response>, tonic::Status> | ||
where | ||
RequestF: Future<Output = Result<tonic::Response<Response>, tonic::Status>> + Send + 'static, | ||
CancellationF: | ||
Future<Output = Result<tonic::Response<Response>, tonic::Status>> + Send + 'static, | ||
Response: Send + 'static, | ||
{ | ||
let token = CancellationToken::new(); | ||
// Will call token.cancel() when the future is dropped, such as when the client cancels the request | ||
let _drop_guard = token.clone().drop_guard(); | ||
let select_task = tokio::spawn(async move { | ||
// Can select on token cancellation on any cancellable future while handling the request, | ||
// allowing for custom cleanup code or monitoring | ||
select! { | ||
res = request_future => res, | ||
_ = token.cancelled() => cancellation_future.await, | ||
} | ||
}); | ||
|
||
select_task.await.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters