-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support gRPC cancellation (#4092)
* feat: support cancellation * chore: add unit test for cancellation * chore: minor refactor * feat: we do not need to spawn in distributed mode --------- Co-authored-by: Ruihang Xia <[email protected]>
- Loading branch information
1 parent
b03cb38
commit 4719569
Showing
11 changed files
with
226 additions
and
68 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
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,90 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::future::Future; | ||
|
||
use tokio::select; | ||
use tokio_util::sync::CancellationToken; | ||
|
||
type Result<T> = std::result::Result<tonic::Response<T>, tonic::Status>; | ||
|
||
pub(crate) async fn with_cancellation_handler<Request, Cancellation, Response>( | ||
request: Request, | ||
cancellation: Cancellation, | ||
) -> Result<Response> | ||
where | ||
Request: Future<Output = Result<Response>> + Send + 'static, | ||
Cancellation: Future<Output = Result<Response>> + 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 => res, | ||
_ = token.cancelled() => cancellation.await, | ||
} | ||
}); | ||
|
||
select_task.await.unwrap() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::time::Duration; | ||
|
||
use tokio::sync::mpsc; | ||
use tokio::time; | ||
use tonic::Response; | ||
|
||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_request_completes_first() { | ||
let request = async { Ok(Response::new("Request Completed")) }; | ||
|
||
let cancellation = async { | ||
time::sleep(Duration::from_secs(1)).await; | ||
Ok(Response::new("Cancelled")) | ||
}; | ||
|
||
let result = with_cancellation_handler(request, cancellation).await; | ||
assert_eq!(result.unwrap().into_inner(), "Request Completed"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_cancellation_when_dropped() { | ||
let (tx, mut rx) = mpsc::channel(2); | ||
let tx_cloned = tx.clone(); | ||
let request = async move { | ||
time::sleep(Duration::from_secs(1)).await; | ||
tx_cloned.send("Request Completed").await.unwrap(); | ||
Ok(Response::new("Completed")) | ||
}; | ||
let cancellation = async move { | ||
tx.send("Request Cancelled").await.unwrap(); | ||
Ok(Response::new("Cancelled")) | ||
}; | ||
|
||
let response_future = with_cancellation_handler(request, cancellation); | ||
// It will drop the `response_future` and then call the `cancellation` future | ||
let result = time::timeout(Duration::from_millis(50), response_future).await; | ||
|
||
assert!(result.is_err(), "Expected timeout error"); | ||
assert_eq!("Request Cancelled", rx.recv().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
Oops, something went wrong.