feat: Add gRPC interceptors (#232)

This change introduces proper gRPC interceptors that are avilable
regardless of the transport used. Each codegen service now produces an
additional method called `with_interceptor` that accepts a
`Interceptor`.

All examples have been updated to use this new style and interop has a
custom `tower::Service` middleware to echo the headers. There is also a
new `interceptor` example that shows basic usage.

BREAKING CHANGE: removed `interceptor_fn` and `intercep_headers_fn` from `transport` in favor of using `tonic::Interceptor`.
This commit is contained in:
Lucio Franco
2020-01-13 21:26:08 -05:00
committed by GitHub
parent 0fa2bf1cea
commit eba7ec7b32
25 changed files with 456 additions and 273 deletions
+1 -45
View File
@@ -9,13 +9,7 @@ pub mod pb {
include!(concat!(env!("OUT_DIR"), "/grpc.testing.rs"));
}
use http::header::{HeaderMap, HeaderName, HeaderValue};
use http_body::Body;
use std::{
default, fmt, iter,
pin::Pin,
task::{Context, Poll},
};
use std::{default, fmt, iter};
pub fn trace_init() {
let sub = tracing_subscriber::FmtSubscriber::builder()
@@ -147,41 +141,3 @@ macro_rules! test_assert {
}
};
}
pub struct MergeTrailers<B> {
inner: B,
trailer: Option<(HeaderName, HeaderValue)>,
}
impl<B> MergeTrailers<B> {
pub fn new(inner: B, trailer: Option<(HeaderName, HeaderValue)>) -> Self {
Self { inner, trailer }
}
}
impl<B: Body + Unpin> Body for MergeTrailers<B> {
type Data = B::Data;
type Error = B::Error;
fn poll_data(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
Pin::new(&mut self.inner).poll_data(cx)
}
fn poll_trailers(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
Pin::new(&mut self.inner).poll_trailers(cx).map_ok(|h| {
h.map(|mut headers| {
if let Some((key, value)) = &self.trailer {
headers.insert(key.clone(), value.clone());
}
headers
})
})
}
}