|
| 1 | +use std::{future::Future, marker::PhantomData}; |
| 2 | + |
| 3 | +use hyper::body::Frame; |
| 4 | +use std::pin::Pin; |
| 5 | +use std::task::Poll; |
| 6 | +use tower::Service; |
| 7 | + |
| 8 | +use hyper::{ |
| 9 | + body::{Body, Incoming}, |
| 10 | + Request, Response, |
| 11 | +}; |
| 12 | +use pin_project::pin_project; |
| 13 | + |
| 14 | +pub fn hybrid_once<Web, Grpc, WebBody, GrpcBody>( |
| 15 | + web: Web, |
| 16 | + grpc: Grpc, |
| 17 | +) -> HybridService<Web, Grpc, WebBody, GrpcBody> |
| 18 | +where |
| 19 | + Web: Service<Request<Incoming>, Response = Response<WebBody>>, |
| 20 | + Grpc: Service<Request<Incoming>, Response = Response<GrpcBody>>, |
| 21 | + Web::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 22 | + Grpc::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 23 | +{ |
| 24 | + HybridService { |
| 25 | + web, |
| 26 | + grpc, |
| 27 | + _phantom: PhantomData, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Clone)] |
| 32 | +pub struct HybridService<Web, Grpc, WebBody, GrpcBody> |
| 33 | +where |
| 34 | + Web: Service<Request<Incoming>, Response = Response<WebBody>>, |
| 35 | + Grpc: Service<Request<Incoming>, Response = Response<GrpcBody>>, |
| 36 | + Web::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 37 | + Grpc::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 38 | +{ |
| 39 | + web: Web, |
| 40 | + grpc: Grpc, |
| 41 | + _phantom: PhantomData<(WebBody, GrpcBody)>, |
| 42 | +} |
| 43 | + |
| 44 | +impl<Web, Grpc, WebBody, GrpcBody> Service<Request<Incoming>> |
| 45 | + for HybridService<Web, Grpc, WebBody, GrpcBody> |
| 46 | +where |
| 47 | + Web: Service<Request<Incoming>, Response = Response<WebBody>>, |
| 48 | + Grpc: Service<Request<Incoming>, Response = Response<GrpcBody>>, |
| 49 | + Web::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 50 | + Grpc::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 51 | +{ |
| 52 | + type Response = Response<HybridBody<WebBody, GrpcBody>>; |
| 53 | + type Error = Box<dyn std::error::Error + Send + Sync + 'static>; |
| 54 | + type Future = HybridFuture<Web::Future, Grpc::Future>; |
| 55 | + |
| 56 | + fn poll_ready( |
| 57 | + &mut self, |
| 58 | + cx: &mut std::task::Context<'_>, |
| 59 | + ) -> std::task::Poll<Result<(), Self::Error>> { |
| 60 | + match self.web.poll_ready(cx) { |
| 61 | + Poll::Ready(Ok(())) => match self.grpc.poll_ready(cx) { |
| 62 | + Poll::Ready(Ok(())) => Poll::Ready(Ok(())), |
| 63 | + Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())), |
| 64 | + Poll::Pending => Poll::Pending, |
| 65 | + }, |
| 66 | + Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())), |
| 67 | + Poll::Pending => Poll::Pending, |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + fn call(&mut self, req: Request<Incoming>) -> Self::Future { |
| 72 | + if req.headers().get("content-type").map(|x| x.as_bytes()) == Some(b"application/grpc") { |
| 73 | + HybridFuture::Grpc(self.grpc.call(req)) |
| 74 | + } else { |
| 75 | + HybridFuture::Web(self.web.call(req)) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[derive(Clone)] |
| 81 | +#[pin_project(project = HybridBodyProj)] |
| 82 | +pub enum HybridBody<WebBody, GrpcBody> { |
| 83 | + Web(#[pin] WebBody), |
| 84 | + Grpc(#[pin] GrpcBody), |
| 85 | +} |
| 86 | + |
| 87 | +impl<WebBody, GrpcBody> Body for HybridBody<WebBody, GrpcBody> |
| 88 | +where |
| 89 | + WebBody: Body + Send + Unpin, |
| 90 | + GrpcBody: Body<Data = WebBody::Data> + Send + Unpin, |
| 91 | + WebBody::Error: std::error::Error + Send + Sync + 'static, |
| 92 | + GrpcBody::Error: std::error::Error + Send + Sync + 'static, |
| 93 | +{ |
| 94 | + type Data = WebBody::Data; |
| 95 | + type Error = Box<dyn std::error::Error + Send + Sync + 'static>; |
| 96 | + |
| 97 | + fn is_end_stream(&self) -> bool { |
| 98 | + match self { |
| 99 | + HybridBody::Web(b) => b.is_end_stream(), |
| 100 | + HybridBody::Grpc(b) => b.is_end_stream(), |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + fn poll_frame( |
| 105 | + self: Pin<&mut Self>, |
| 106 | + cx: &mut std::task::Context, |
| 107 | + ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> { |
| 108 | + match self.project() { |
| 109 | + HybridBodyProj::Web(b) => b.poll_frame(cx).map_err(|e| e.into()), |
| 110 | + HybridBodyProj::Grpc(b) => b.poll_frame(cx).map_err(|e| e.into()), |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +#[pin_project(project = HybridFutureProj)] |
| 116 | +pub enum HybridFuture<WebFuture, GrpcFuture> { |
| 117 | + Web(#[pin] WebFuture), |
| 118 | + Grpc(#[pin] GrpcFuture), |
| 119 | +} |
| 120 | + |
| 121 | +impl<WebFuture, GrpcFuture, WebBody, GrpcBody, WebError, GrpcError> Future |
| 122 | + for HybridFuture<WebFuture, GrpcFuture> |
| 123 | +where |
| 124 | + WebFuture: Future<Output = Result<Response<WebBody>, WebError>>, |
| 125 | + GrpcFuture: Future<Output = Result<Response<GrpcBody>, GrpcError>>, |
| 126 | + WebError: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 127 | + GrpcError: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 128 | +{ |
| 129 | + type Output = Result< |
| 130 | + Response<HybridBody<WebBody, GrpcBody>>, |
| 131 | + Box<dyn std::error::Error + Send + Sync + 'static>, |
| 132 | + >; |
| 133 | + |
| 134 | + fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context) -> Poll<Self::Output> { |
| 135 | + match self.project() { |
| 136 | + HybridFutureProj::Web(a) => match a.poll(cx) { |
| 137 | + Poll::Ready(Ok(res)) => Poll::Ready(Ok(res.map(HybridBody::Web))), |
| 138 | + Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())), |
| 139 | + Poll::Pending => Poll::Pending, |
| 140 | + }, |
| 141 | + HybridFutureProj::Grpc(b) => match b.poll(cx) { |
| 142 | + Poll::Ready(Ok(res)) => Poll::Ready(Ok(res.map(HybridBody::Grpc))), |
| 143 | + Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())), |
| 144 | + Poll::Pending => Poll::Pending, |
| 145 | + }, |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments