-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Gustavo Inacio <[email protected]>
- Loading branch information
Showing
6 changed files
with
326 additions
and
30 deletions.
There are no files selected for viewing
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,4 @@ | ||
|
||
fn main() { | ||
println!("Hello world"); | ||
} |
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,148 @@ | ||
use std::{future::Future, marker::PhantomData}; | ||
|
||
use hyper::body::Frame; | ||
use std::pin::Pin; | ||
use std::task::Poll; | ||
use tower::Service; | ||
|
||
use hyper::{ | ||
body::{Body, Incoming}, | ||
Request, Response, | ||
}; | ||
use pin_project::pin_project; | ||
|
||
pub fn hybrid_once<Web, Grpc, WebBody, GrpcBody>( | ||
web: Web, | ||
grpc: Grpc, | ||
) -> HybridService<Web, Grpc, WebBody, GrpcBody> | ||
where | ||
Web: Service<Request<Incoming>, Response = Response<WebBody>>, | ||
Grpc: Service<Request<Incoming>, Response = Response<GrpcBody>>, | ||
Web::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, | ||
Grpc::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, | ||
{ | ||
HybridService { | ||
web, | ||
grpc, | ||
_phantom: PhantomData, | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct HybridService<Web, Grpc, WebBody, GrpcBody> | ||
where | ||
Web: Service<Request<Incoming>, Response = Response<WebBody>>, | ||
Grpc: Service<Request<Incoming>, Response = Response<GrpcBody>>, | ||
Web::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, | ||
Grpc::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, | ||
{ | ||
web: Web, | ||
grpc: Grpc, | ||
_phantom: PhantomData<(WebBody, GrpcBody)>, | ||
} | ||
|
||
impl<Web, Grpc, WebBody, GrpcBody> Service<Request<Incoming>> | ||
for HybridService<Web, Grpc, WebBody, GrpcBody> | ||
where | ||
Web: Service<Request<Incoming>, Response = Response<WebBody>>, | ||
Grpc: Service<Request<Incoming>, Response = Response<GrpcBody>>, | ||
Web::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, | ||
Grpc::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, | ||
{ | ||
type Response = Response<HybridBody<WebBody, GrpcBody>>; | ||
type Error = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
type Future = HybridFuture<Web::Future, Grpc::Future>; | ||
|
||
fn poll_ready( | ||
&mut self, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Result<(), Self::Error>> { | ||
match self.web.poll_ready(cx) { | ||
Poll::Ready(Ok(())) => match self.grpc.poll_ready(cx) { | ||
Poll::Ready(Ok(())) => Poll::Ready(Ok(())), | ||
Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())), | ||
Poll::Pending => Poll::Pending, | ||
}, | ||
Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())), | ||
Poll::Pending => Poll::Pending, | ||
} | ||
} | ||
|
||
fn call(&mut self, req: Request<Incoming>) -> Self::Future { | ||
if req.headers().get("content-type").map(|x| x.as_bytes()) == Some(b"application/grpc") { | ||
HybridFuture::Grpc(self.grpc.call(req)) | ||
} else { | ||
HybridFuture::Web(self.web.call(req)) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
#[pin_project(project = HybridBodyProj)] | ||
pub enum HybridBody<WebBody, GrpcBody> { | ||
Web(#[pin] WebBody), | ||
Grpc(#[pin] GrpcBody), | ||
} | ||
|
||
impl<WebBody, GrpcBody> Body for HybridBody<WebBody, GrpcBody> | ||
where | ||
WebBody: Body + Send + Unpin, | ||
GrpcBody: Body<Data = WebBody::Data> + Send + Unpin, | ||
WebBody::Error: std::error::Error + Send + Sync + 'static, | ||
GrpcBody::Error: std::error::Error + Send + Sync + 'static, | ||
{ | ||
type Data = WebBody::Data; | ||
type Error = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
|
||
fn is_end_stream(&self) -> bool { | ||
match self { | ||
HybridBody::Web(b) => b.is_end_stream(), | ||
HybridBody::Grpc(b) => b.is_end_stream(), | ||
} | ||
} | ||
|
||
fn poll_frame( | ||
self: Pin<&mut Self>, | ||
cx: &mut std::task::Context, | ||
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> { | ||
match self.project() { | ||
HybridBodyProj::Web(b) => b.poll_frame(cx).map_err(|e| e.into()), | ||
HybridBodyProj::Grpc(b) => b.poll_frame(cx).map_err(|e| e.into()), | ||
} | ||
} | ||
} | ||
|
||
#[pin_project(project = HybridFutureProj)] | ||
pub enum HybridFuture<WebFuture, GrpcFuture> { | ||
Web(#[pin] WebFuture), | ||
Grpc(#[pin] GrpcFuture), | ||
} | ||
|
||
impl<WebFuture, GrpcFuture, WebBody, GrpcBody, WebError, GrpcError> Future | ||
for HybridFuture<WebFuture, GrpcFuture> | ||
where | ||
WebFuture: Future<Output = Result<Response<WebBody>, WebError>>, | ||
GrpcFuture: Future<Output = Result<Response<GrpcBody>, GrpcError>>, | ||
WebError: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, | ||
GrpcError: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, | ||
{ | ||
type Output = Result< | ||
Response<HybridBody<WebBody, GrpcBody>>, | ||
Box<dyn std::error::Error + Send + Sync + 'static>, | ||
>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context) -> Poll<Self::Output> { | ||
match self.project() { | ||
HybridFutureProj::Web(a) => match a.poll(cx) { | ||
Poll::Ready(Ok(res)) => Poll::Ready(Ok(res.map(HybridBody::Web))), | ||
Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())), | ||
Poll::Pending => Poll::Pending, | ||
}, | ||
HybridFutureProj::Grpc(b) => match b.poll(cx) { | ||
Poll::Ready(Ok(res)) => Poll::Ready(Ok(res.map(HybridBody::Grpc))), | ||
Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())), | ||
Poll::Pending => Poll::Pending, | ||
}, | ||
} | ||
} | ||
} |
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.