Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tonic): introduce async_interceptor #910

Closed
wants to merge 5 commits into from
Closed
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
6 changes: 6 additions & 0 deletions examples/src/tower/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.layer(MyMiddlewareLayer::default())
// Interceptors can be also be applied as middleware
.layer(tonic::service::interceptor(intercept))
.layer(tonic::service::async_interceptor(async_intercept))
.into_inner();

Server::builder()
Expand All @@ -65,6 +66,11 @@ fn intercept(req: Request<()>) -> Result<Request<()>, Status> {
Ok(req)
}

// An async interceptor function.
async fn async_intercept(req: Request<()>) -> Result<Request<()>, Status> {
Ok(req)
}

#[derive(Debug, Clone, Default)]
struct MyMiddlewareLayer;

Expand Down
95 changes: 95 additions & 0 deletions tests/integration_tests/tests/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
use tokio::sync::oneshot;
use tonic::{
body::BoxBody,
service::{async_interceptor, interceptor},
transport::{Endpoint, NamedService, Server},
Request, Response, Status,
};
Expand Down Expand Up @@ -60,6 +61,100 @@ async fn setting_extension_from_interceptor() {
jh.await.unwrap();
}

#[tokio::test]
async fn setting_extension_from_interceptor_layer() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
let value = req.extensions().get::<ExtensionValue>().unwrap();
assert_eq!(value.0, 42);

Ok(Response::new(Output {}))
}
}

let svc = test_server::TestServer::new(Svc);
let interceptor_layer = interceptor(|mut req: Request<()>| {
req.extensions_mut().insert(ExtensionValue(42));
Ok(req)
});

let (tx, rx) = oneshot::channel::<()>();

let jh = tokio::spawn(async move {
Server::builder()
.layer(interceptor_layer)
.add_service(svc)
.serve_with_shutdown("127.0.0.1:1325".parse().unwrap(), rx.map(drop))
.await
.unwrap();
});

tokio::time::sleep(Duration::from_millis(100)).await;

let channel = Endpoint::from_static("http://127.0.0.1:1325")
.connect()
.await
.unwrap();

let mut client = test_client::TestClient::new(channel);

client.unary_call(Input {}).await.unwrap();

tx.send(()).unwrap();

jh.await.unwrap();
}

#[tokio::test]
async fn setting_extension_from_async_interceptor_layer() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
let value = req.extensions().get::<ExtensionValue>().unwrap();
assert_eq!(value.0, 42);

Ok(Response::new(Output {}))
}
}

let svc = test_server::TestServer::new(Svc);
let interceptor_layer = async_interceptor(|mut req: Request<()>| {
req.extensions_mut().insert(ExtensionValue(42));
futures::future::ok(req)
});

let (tx, rx) = oneshot::channel::<()>();

let jh = tokio::spawn(async move {
Server::builder()
.layer(interceptor_layer)
.add_service(svc)
.serve_with_shutdown("127.0.0.1:1326".parse().unwrap(), rx.map(drop))
.await
.unwrap();
});

tokio::time::sleep(Duration::from_millis(100)).await;

let channel = Endpoint::from_static("http://127.0.0.1:1326")
.connect()
.await
.unwrap();

let mut client = test_client::TestClient::new(channel);

client.unary_call(Input {}).await.unwrap();

tx.send(()).unwrap();

jh.await.unwrap();
}

#[tokio::test]
async fn setting_extension_from_tower() {
struct Svc;
Expand Down
Loading